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
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
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
Grade: BKey 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
1. Does a Swift switch case fall through into the next case by default?
2. How do you deliberately fall through to the next case in Swift?
3. What must a switch statement over a non-enum type include if it doesn't cover every possible value?
4. Which of these can a Swift switch case match?
Was this page helpful?
You May Also Like
if-else Statements in Swift
Learn how Swift's if-else statement branches execution using strictly typed Bool conditions and mandatory braces.
Control Transfer Statements in Swift
Master continue, break, fallthrough, return, and labeled statements for precise control over loops and switches.
Enumerations in Swift
Learn how Swift enums define related values, support associated and raw values, and integrate with switch statements.
Optional Binding in Swift
Optional binding safely unwraps an optional's value into a local constant only when a value is actually present.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics