0Pricing
Swift Academy · Lesson

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

  1. Exhaustive Switch on Values
  2. Matching Ranges and Tuples
  3. Value Binding and where Clauses
  4. Compound Cases and Fallthrough
← Back to Swift Academy