0
0
Swiftprogramming~10 mins

Why Swift has no implicit fallthrough - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why Swift has no implicit fallthrough
Start switch statement
Match case?
NoCheck next case
Yes
Execute case body
Fallthrough?
NoExit switch
Yes
Execute next case body
Repeat fallthrough check
Swift switch checks each case and executes only the matched one unless 'fallthrough' is explicitly used to continue to the next case.
Execution Sample
Swift
let number = 2
switch number {
case 1:
    print("One")
case 2:
    print("Two")
    fallthrough
case 3:
    print("Three")
default:
    print("Other")
}
This code prints "Two" and then "Three" because of the explicit fallthrough after case 2.
Execution Table
StepMatched CaseActionOutput
1case 1Check if number == 1No
2case 2Check if number == 2Yes
3case 2Print "Two"Two
4case 2fallthrough to next case
5case 3Execute case 3 bodyThree
6Exit switchNo more fallthrough
💡 No implicit fallthrough, switch exits after case 3 because no fallthrough keyword is used.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
number2222
Key Moments - 2 Insights
Why doesn't Swift automatically continue to the next case after a match?
Swift requires explicit 'fallthrough' to continue to the next case, preventing accidental execution of multiple cases as shown in steps 4 and 5 of the execution_table.
What happens if 'fallthrough' is omitted after a matched case?
The switch statement exits immediately after executing the matched case, as seen after step 3 where no fallthrough means no further cases run.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is printed at step 3?
A"Two"
B"Three"
C"One"
DNothing
💡 Hint
Check the Output column at step 3 in the execution_table.
At which step does the switch statement use fallthrough?
AStep 2
BStep 4
CStep 6
DStep 7
💡 Hint
Look for the 'fallthrough' action in the Action column of the execution_table.
If the 'fallthrough' keyword was removed, what would be the output?
A"Two" and "Three"
B"Three" only
C"Two" only
D"One"
💡 Hint
Refer to the key_moments explanation about what happens without fallthrough.
Concept Snapshot
Swift switch statements do NOT fall through cases by default.
You must use the 'fallthrough' keyword to continue to the next case.
This prevents accidental execution of multiple cases.
Each case is exclusive unless fallthrough is explicit.
This makes code safer and clearer.
Full Transcript
In Swift, switch statements check each case one by one. When a case matches, Swift runs only that case's code and then exits the switch. Unlike some languages, Swift does not automatically continue to the next case. To run the next case, you must write the 'fallthrough' keyword explicitly. This behavior helps avoid bugs where multiple cases run by accident. For example, if number is 2, Swift runs case 2 and prints "Two". If 'fallthrough' is used, it also runs case 3 and prints "Three". Without 'fallthrough', it stops after printing "Two". This clear control flow makes Swift code safer and easier to understand.