Compound Cases and Fallthrough
Combine cases and use fallthrough intentionally.
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")
}All lessons in this course
- Exhaustive Switch on Values
- Matching Ranges and Tuples
- Value Binding and where Clauses
- Compound Cases and Fallthrough