Compound Cases and Fallthrough
Combine cases and use fallthrough intentionally.
Compound Cases and Fallthrough is a free Swift Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Compound Cases
A single case can match several values by separating them with commas: case "a", "b":. All listed values run the same code.
let letter = "a"
switch letter {
case "a", "e", "i", "o", "u":
print("Vowel")
default:
print("Consonant")
}Why Compound Cases
Compound cases avoid duplicating identical bodies. They group values that should behave the same way.
let day = 6
switch day {
case 6, 7:
print("Weekend")
case 1, 2, 3, 4, 5:
print("Weekday")
default:
print("Invalid day")
}Compound With Ranges
You can mix ranges and individual values within one compound case.
let code = 204
switch code {
case 200, 201, 204:
print("Success")
case 400...499:
print("Client error")
default:
print("Other")
}Compound Cases and Binding
When binding in a compound case, every alternative must bind the same names with the same types.
let point = (0, 5)
switch point {
case (let v, 0), (0, let v):
print("On an axis at distance \(v)")
default:
print("Off the axes")
}Introducing fallthrough
Swift cases do not fall through by default. The fallthrough keyword forces execution to continue into the next case.
let n = 1
switch n {
case 1:
print("one")
fallthrough
case 2:
print("two")
default:
print("done")
}How fallthrough Works
fallthrough moves to the next case body unconditionally; it does not re-check that case condition. Use it deliberately.
let level = 3
switch level {
case 3:
print("Level 3 perks")
fallthrough
case 2:
print("Level 2 perks")
fallthrough
case 1:
print("Level 1 perks")
default:
break
}fallthrough Ignores Patterns
Because fallthrough skips the next pattern check, the next case must not rely on binding a value, or it will not compile.
let x = 10
switch x {
case 10:
print("ten")
fallthrough
default:
print("reached default")
}Compound vs fallthrough
To run the same code for multiple values, prefer a compound case. Reserve fallthrough for genuinely cascading behavior.
let grade = "B"
switch grade {
case "A", "B":
print("Passing")
default:
print("Other")
}Accumulating With fallthrough
Fallthrough is useful for cumulative effects, like listing all benefits from a tier down.
let tier = 2
var perks = [String]()
switch tier {
case 2:
perks.append("Premium")
fallthrough
case 1:
perks.append("Basic")
default:
break
}
print(perks)Mixing Both
You can use compound cases and fallthrough in the same switch where each fits best.
let n = 0
switch n {
case 0:
print("zero")
fallthrough
case 1, 2:
print("small")
default:
print("large")
}Readability Note
Fallthrough can surprise readers since most languages default to it. Comment your intent or prefer compound cases when possible.
let status = "warn"
switch status {
case "warn", "error":
print("Needs attention")
default:
print("OK")
}Quick Check
Test compound cases and fallthrough.
Recap: Compound Cases and Fallthrough
Use comma-separated compound cases to share one body across several values. Use the explicit fallthrough keyword only when you intentionally want execution to cascade into the next case.
let n = 2
switch n {
case 1, 2: print("small")
default: print("big")
}Frequently asked questions
Is the “Compound Cases and Fallthrough” lesson free?
Yes — the full text of “Compound Cases and Fallthrough” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
What will I learn in “Compound Cases and Fallthrough”?
Combine cases and use fallthrough intentionally. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Compound Cases and Fallthrough” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Exhaustive Switch on Values
- Matching Ranges and Tuples
- Value Binding and where Clauses
- Compound Cases and Fallthrough