Recall & Review
beginner
What does 'No implicit fallthrough in switch' mean in Swift?
In Swift, each case in a switch statement must explicitly end with a break or return. Unlike some languages, Swift does not automatically continue to the next case unless you explicitly tell it to with the 'fallthrough' keyword.
Click to reveal answer
beginner
How do you make a switch case continue to the next case in Swift?
You use the 'fallthrough' keyword at the end of a case to continue execution to the next case. Without 'fallthrough', Swift stops after the current case.
Click to reveal answer
intermediate
Why does Swift require explicit fallthrough instead of implicit?
Swift's design avoids bugs by preventing accidental fallthrough. This makes code clearer and safer because you must clearly state when you want to continue to the next case.
Click to reveal answer
beginner
What happens if you forget to add 'break' or 'fallthrough' in a Swift switch case?
Swift automatically ends the case after its code runs. You don't need to add 'break' explicitly because Swift does not fall through by default.
Click to reveal answer
beginner
Show a simple Swift switch example that uses 'fallthrough'.
Example:
let number = 2
switch number {
case 1:
print("One")
case 2:
print("Two")
fallthrough
case 3:
print("Three")
default:
print("Other")
}
Output:
Two
ThreeClick to reveal answer
In Swift, what happens after a switch case finishes executing if there is no 'fallthrough'?
✗ Incorrect
Swift switch cases do not fall through by default. Execution stops after the current case unless 'fallthrough' is used.
Which keyword allows a Swift switch case to continue to the next case?
✗ Incorrect
The 'fallthrough' keyword explicitly tells Swift to continue to the next case.
Why does Swift avoid implicit fallthrough in switch statements?
✗ Incorrect
Explicit fallthrough prevents accidental execution of multiple cases, making code safer and clearer.
What will this Swift code print?
let x = 1
switch x {
case 1:
print("One")
case 2:
print("Two")
fallthrough
case 3:
print("Three")
default:
print("Other")
}
✗ Incorrect
Since x is 1, only the first case runs and stops. No fallthrough is used, so it prints 'One' only.
Is the 'break' keyword required at the end of each Swift switch case?
✗ Incorrect
Swift automatically ends each case without needing 'break'.
Explain how Swift handles switch case execution and how it differs from languages with implicit fallthrough.
Think about what happens after a case runs in Swift.
You got /4 concepts.
Write a Swift switch statement that prints 'Low', 'Medium', or 'High' for values 1, 2, and 3, and uses 'fallthrough' to print 'Check again' after 'Medium'.
Use 'fallthrough' only where you want to continue to the next case.
You got /4 concepts.