0
0
Swiftprogramming~5 mins

If and if-else statements in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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")
}
ADoes nothing
BAlways prints "Hot"
CPrints "Cool" if temperature is more than 30
DPrints "Hot" if temperature is more than 30, otherwise prints "Cool"
Which keyword starts a condition check in Swift?
Awhen
Bcheck
Cif
Dcondition
What happens if the if condition is false and there is no else?
ANothing happens, the program skips the <code>if</code> block
BThe code inside <code>if</code> runs anyway
CThe program crashes
DThe program asks for input
How do you check multiple conditions in Swift?
AUsing multiple <code>if</code> statements only
BUsing <code>else if</code> between <code>if</code> and <code>else</code>
CUsing <code>switch</code> only
DUsing <code>for</code> loops
What will this code print?<br>
let score = 75
if score >= 90 {
  print("A")
} else if score >= 80 {
  print("B")
} else {
  print("C")
}
A"C"
B"A"
C"B"
DNothing
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.