100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Swift

switch Statement in Swift

Explore Swift's powerful switch statement, which requires exhaustiveness and never falls through by default.

Control FlowIntermediate8 min readJul 8, 2026
Analogies

Introduction

Swift's switch statement is far more powerful than its counterpart in C, Java, or JavaScript. It supports matching against ranges, tuples, and complex patterns, and it can attach extra conditions with where clauses. Crucially, each case in Swift automatically stops after executing — there is no implicit fallthrough into the next case, which removes one of the most notorious sources of bugs in C-style switch statements. Because of this safety, Swift also requires every switch statement to be exhaustive, meaning it must cover every possible value of the type being switched on, either explicitly or via a default case.

🏏

Cricket analogy: Swift's switch is like a DRS review system that automatically stops checking further once it finds a conclusive answer, unlike an older manual review process where an umpire might keep checking irrelevant angles unless told to stop.

Syntax

swift
switch someValue {
case pattern1:
    // code
case pattern2, pattern3:
    // code for multiple matches
case let value where condition:
    // code using bound value with extra condition
default:
    // code for anything else
}

Explanation

Each case can match a single value, a comma-separated list of values, a range (like 1...5), or a tuple pattern. A case finishes automatically once its body runs — there's no need for a break statement, and stray break doesn't cause fallthrough like it does in other languages. If you deliberately want to continue execution into the next case, Swift provides the explicit fallthrough keyword. The where clause lets a case add an additional Boolean condition, enabling expressive pattern matching, such as filtering values bound in a case let pattern. Because Swift enforces exhaustiveness, the compiler will reject a switch over an enum that omits a case unless a default is present.

🏏

Cricket analogy: A case matching a range like overs 1...10 (powerplay) is like a scorer grouping deliveries by phase of play automatically, and Swift's fallthrough is the rare deliberate choice to also apply powerplay rules to over 11 if a rule says so.

Example

swift
let score = 85

switch score {
case 90...100:
    print("Grade: A")
case 80..<90:
    print("Grade: B")
case let s where s < 0:
    print("Invalid score")
default:
    print("Grade: C or below")
}

Output

swift
Grade: B

Key Takeaways

  • Swift's switch never implicitly falls through — each case stops automatically after its body runs.
  • Use the explicit fallthrough keyword to opt into fall-through behavior when truly needed.
  • Every switch must be exhaustive, covering all cases or including a default clause.
  • Cases can match ranges, tuples, and use where clauses for extra conditions.
  • Multiple values can share one case body using a comma-separated list.

Practice what you learned

Was this page helpful?

Topics covered

#Swift#SwiftProgrammingStudyNotes#Programming#SwitchStatementInSwift#Switch#Statement#Syntax#Explanation#StudyNotes#SkillVeris