Concept Flow - No implicit fallthrough in switch
Start switch with value
Match case 1?
No→Match case 2?
Execute case 1
Exit switch
Swift checks each case one by one and runs only the matching case's code without automatically running the next cases.
let number = 2 switch number { case 1: print("One") case 2: print("Two") default: print("Other") }
| Step | Condition Checked | Result | Action | Output |
|---|---|---|---|---|
| 1 | number == 1 | False | Skip case 1 | |
| 2 | number == 2 | True | Execute case 2 | Two |
| 3 | Exit switch | N/A | Stop checking further cases |
| Variable | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| number | 2 | 2 | 2 | 2 |
Swift switch checks each case for a match. Only the matching case runs; no automatic fallthrough. Use 'default' to catch unmatched values. No need for break statements. Switch exits after running matched case.