Recall & Review
beginner
What is the purpose of an
if statement in Swift?An
if statement lets your program make decisions by running code only when a condition is true.Click to reveal answer
beginner
How does an
if-else statement work?It runs one block of code if the condition is true, and a different block if the condition is false.
Click to reveal answer
beginner
What will this Swift code print?<br><pre>let age = 18
if age >= 18 {
print("Adult")
} else {
print("Minor")
}</pre>It will print
Adult because the age is 18, which meets the condition age >= 18.Click to reveal answer
beginner
Can an
if statement run without an else?Yes. You can use
if alone to run code only when a condition is true, and do nothing if it is false.Click to reveal answer
intermediate
What is the syntax to check multiple conditions in Swift?
You can use
else if between if and else to check more than one condition.Click to reveal answer
What does this Swift code do?<br>
if temperature > 30 {
print("Hot")
} else {
print("Cool")
}✗ Incorrect
The
if checks if temperature is greater than 30. If true, it prints "Hot"; otherwise, it prints "Cool".Which keyword starts a condition check in Swift?
✗ Incorrect
The keyword
if is used to start a condition check.What happens if the
if condition is false and there is no else?✗ Incorrect
If the condition is false and there is no
else, the program skips the if block and continues.How do you check multiple conditions in Swift?
✗ Incorrect
You use
else if to check multiple conditions in sequence.What will this code print?<br>
let score = 75
if score >= 90 {
print("A")
} else if score >= 80 {
print("B")
} else {
print("C")
}✗ Incorrect
Since 75 is less than 80, it does not meet the first two conditions, so it prints "C".
Explain how an
if-else statement helps a program make decisions.Think about choosing between two paths based on a yes/no question.
You got /4 concepts.
Write a simple Swift
if statement that prints "Good morning" if a variable hour is less than 12.Use the <code>if</code> keyword and a comparison operator.
You got /3 concepts.