Exhaustive Switch on Values
Cover all cases or use a default branch.
Exhaustive Switch on Values is a free Swift Academy lesson on CoddyKit — lesson 1 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.
What switch Does
A switch compares a value against several cases and runs the first matching one. It is a clear alternative to long if/else-if chains.
let day = 3
switch day {
case 1:
print("Monday")
case 2:
print("Tuesday")
case 3:
print("Wednesday")
default:
print("Other")
}No Implicit Fallthrough
Unlike C, Swift cases do not fall through automatically. After a matching case runs, the switch ends. No break needed.
let n = 1
switch n {
case 1:
print("one")
case 2:
print("two")
default:
print("many")
}Switches Must Be Exhaustive
A switch must cover every possible value. For an Int, you cannot list them all, so you provide a default case.
let code = 500
switch code {
case 200:
print("OK")
case 404:
print("Not Found")
default:
print("Unhandled: \(code)")
}The default Case
The default case catches anything not matched above it. It must come last.
let grade = "X"
switch grade {
case "A":
print("Excellent")
case "B":
print("Good")
default:
print("Needs review")
}Switching on Strings
Switch works on any equatable value, including strings.
let command = "start"
switch command {
case "start":
print("Starting")
case "stop":
print("Stopping")
default:
print("Unknown command")
}No Empty Cases
Each case must contain at least one statement. If you truly want a case to do nothing, write break.
let n = 2
switch n {
case 1:
print("one")
case 2:
break
default:
print("other")
}
print("done")Switch as a Statement
A switch runs statements; it does not itself return a value. To produce a value, assign inside each case or use it to call functions.
let level = 2
var label: String
switch level {
case 1: label = "low"
case 2: label = "mid"
default: label = "high"
}
print(label)Switching on Bool
You can switch on a Bool with cases true and false. No default is needed because both cases are covered.
let isReady = true
switch isReady {
case true:
print("Go")
case false:
print("Wait")
}Enums Need No default
When switching over an enum and you handle every case, the switch is already exhaustive and a default is unnecessary.
enum Direction { case north, south }
let d = Direction.north
switch d {
case .north: print("Up")
case .south: print("Down")
}Readable Multi-Way Logic
Switch keeps multi-way decisions tidy. Each outcome is on its own labeled case, easy to scan and extend.
let score = 72
var result: String
switch score {
case 90...100: result = "A"
case 80..<90: result = "B"
case 70..<80: result = "C"
default: result = "F"
}
print(result)Default Placement
The default must be the final case. Placing it earlier would make later cases unreachable, which Swift forbids.
let x = 7
switch x {
case 0:
print("zero")
case 7:
print("seven")
default:
print("something else")
}Quick Check
Test exhaustive switch.
Recap: Exhaustive switch on Values
A Swift switch matches the first case, never falls through implicitly, and must be exhaustive. Use default for open-ended types like Int or String; enums covered fully need no default.
let n = 3
switch n {
case 1: print("a")
default: print("b")
}Frequently asked questions
Is the “Exhaustive Switch on Values” lesson free?
Yes — the full text of “Exhaustive Switch on Values” 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 “Exhaustive Switch on Values”?
Cover all cases or use a default branch. 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 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Exhaustive Switch on Values” 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