Getting Started with App Development [Section 5: Control Flow] Flashcards
code that makes decisions about what lines of code should be executed depending on results of previously executed code is called _________ ________
control flow
Operator:
==
Type: Comparison
Description: Two items must be equal
Operator:
!=
Type: Comparison
Description: The values must not be equal to each other
Operator:
>
Type: Comparison
Description: Value on the left must be greater than the value on the right
Operator:
> =
Type: Comparison
Description: Value on the left must be greater than or equal to the value on the right
Operator:
<
Type: Comparison
Description: Value on the left must be less than the value on the right
Operator:
<=
Type: Comparison
Description: Value on the left must be less than or equal to the value on the right
Operator:
&&
Type: Logical
Description: AND — the conditional statement on the left AND right must be true
Operator:
||
Type: Logical
Description: OR — the conditional statement on the left OR right must be true
Operator:
!
Type: Logical
Description: NOT — returns the logical opposite of the conditional statement immediately following the operator
An ____ ________ states that “if this condition is true, then run this block of code”
if statement
let temperature = 32
if temperature >= 32 {
print(”The water is boiling.”)
}
Console Output:
The water is boiling.
By adding an ______ _________ to an if statement, you can specify a block of code to execute if the condition is not true
else clause
e.g.,
let temperature = 32
if temperature >= 32 {
print(”The water is boiling.”)
} else {
print(”The water is not boiling.”)
}
Via using ______ _______, you can declare more blocks of code to run based on any number of conditions
else if
e.g.,
var finishPosition = 2
if finishPosition == 1 {
print(”Congratulations, you won the gold medal!”)
} else if finishPosition == 2 {
print(”You came in second place, you won a silver medal!”)
} else {
print(”You did not win a gold or silver medal.”)
}
It’s possible to invert a ______ value using the logical NOT operator, which is represented with “!”
Bool
e.g.,
var isSnowing = false
if !isSnowing {
print(”It is not snowing.”)
}
Console Output:
It is not snowing.
You can also use the logical AND operator, represented by ____, to cheque if two or more conditions are true
&&
e.g.,
let temperature = 70
if temperature >= 65 && temperature <= 75 {
print(”The temperature is just right.”)
} else if temperature < 65 {
print(”It is too cold.”)
} else {
print(”It is too hot.”)
}
Console Output:
The temperature is just right.
You’ve yet another option to check if either one of two conditions is true: the ______ operator [represented by ||]
logical OR
var isPluggedIn = false
var hasBatteryPower = true
if isPluggedIn || hasBatteryPower {
print(”You can use your laptop.”)
} else {
print(”😱”)
}
Console Output:
You can use your laptop.
Switch contains another tool for control flow known as a _______ statement that’s optimal for working with many potential conditions
switch
A basic switch statement takes a value with multiple options and allows you to run separate code based on each option, or _______
case
You can also provide a _______ case to specify a block of code that will run in all the cases you haven’t specifically defined
default
e.g.,
let numberOfWheels = 2
switch numberOfWheels {
case 0:
print(“Missing something?”)
case 1:
print(”Unicycle”)
case 2:
print(”Bicycle”)
case 3:
print(”Tricycle”)
case 4:
print(”Quadcycle”)
default:
print(”That’s a lot of wheels!”)
}
Any given ____ statement can also evaluate multiple conditions at once
case
e.g.,
The following code, for example, checks whether a character is a vowel or not:
let character = “z”
switch character {
case “a”, “e”, “i”, “o”, “u”:
print(”This character is a vowel.”)
default:
print(”This character is a consonant.”)
}
When working with numbers, you can use ________ matching to cheque for inclusion in a range
interval
e.g.,
switch distance {
case 0…9:
print(”Your destination is close.”)
case 10…99:
print(”Your destination is a medium distance from here.”)
case 100…999:
print(”Your destination is far from here.”)
default:
print(”Are you sure you want to travel this far?”)
}
The switch ________ is the right tool for control flow when you want to run different code based on many different conditions
operator
If a certain condition is _____, you want to set a variable to one value
true
If the condition is _______, you want to set the variable to a different value
false
e.g.,
var largest: Int
let a = 15
let b = 4
if a > b {
largest = a
} else {
largest = b
}
many programming languages include a special operator, known as a ______ ________ operator (?:), for writing more concise code
ternary conditional
The aforementioned term is also commonly referred to as the “__________ __________”
ternary operator
The ternary operator has three parts
- A question with a true or false answer.
- A value if the answer to the question is true.
- A value if the answer to the question is false.
Example of a ternary operator
var largest: Int
let a = 15
let b = 4
largest = a > b ? a : b
in layman’s terms: “if a > b, assign a to the largest variable; otherwise, assign b”
Swift provides a global function to find the largest value in an easier-to-read and more concise expression
largest = max(a, b)