Overview
A switch statement is a conditional statement, just like an if statement. It checks a value and runs the code for the first matching case.
The difference is mostly readability. if statements are great when you have one or two simple conditions. switch statements are better when one value can have several possible outcomes.
Basic Syntax
Here is the simplest shape of a Swift switch statement:
switch value {
case patternOne:
// code to run when value matches patternOne
case patternTwo:
// code to run when value matches patternTwo
default:
// code to run when nothing else matches
}The value after switch is compared against each case. When Swift finds a match, it runs that case's code block.
A Simple Example
Let's start with a small example that checks a single letter:
let letter = "a"
switch letter {
case "a":
print("the first letter of the alphabet")
case "z":
print("the last letter of the alphabet")
default:
print("a letter in between the alphabet")
}Because letter is "a", Swift runs the first case and prints:
the first letter of the alphabetThe default case is important. It gives Swift something to run when none of the specific cases match.
The Same Logic with If/Else
You could write the same example with if, else if, and else:
let letter = "a"
if letter == "a" {
print("the first letter of the alphabet")
} else if letter == "z" {
print("the last letter of the alphabet")
} else {
print("a letter in between the alphabet")
}Both versions work. But as the number of possible values grows, the switch version is usually easier to scan.
Matching Multiple Values
A single case can match more than one value. Separate the values with commas:
let direction = "left"
switch direction {
case "left", "right":
print("Move horizontally")
case "up", "down":
print("Move vertically")
default:
print("Unknown direction")
}This is useful when multiple inputs should trigger the same behavior.
Matching Ranges
Switch statements can also match ranges, which is handy for scores, ages, counts, and other number-based categories:
let score = 87
switch score {
case 90...100:
print("Excellent")
case 75...89:
print("Good")
case 60...74:
print("Keep practicing")
default:
print("Needs review")
}The ... operator creates a closed range, which includes both the starting and ending values.
Switch with Enums
switch becomes especially useful with enums. Enums describe a fixed set of possible values, and switch statements let you handle each value clearly.
enum LoadingState {
case idle
case loading
case success
case failure
}
let state = LoadingState.loading
switch state {
case .idle:
print("Waiting to start")
case .loading:
print("Loading...")
case .success:
print("Done")
case .failure:
print("Something went wrong")
}Notice that this switch does not need a default case because every enum case is handled. That is one of the best reasons to use switch with enums: Swift can help you catch missing cases.
Common Mistakes
Forgetting the default case
If your switch does not cover every possible value, Swift requires a default case. Without it, the code will not compile.
Using switch for one condition
If you only need to ask one yes-or-no question, an if statement is usually clearer. Use switch when one value can match several meaningful cases.
Expecting automatic fallthrough
In Swift, cases do not automatically continue into the next case. Once a case matches and runs, the switch is finished. This is different from languages like C and JavaScript.
Quick Reference
| Syntax | What It Does |
|---|---|
switch value | Starts a switch statement for the value you want to match |
case "a": | Runs when the switched value matches that case |
default: | Runs when no other case matches |
case 1, 2, 3: | Matches any of several listed values |
case 0...10: | Matches a value inside a closed range |
case .loading: | Matches an enum case |
Summary
- Use
switchwhen one value can have several possible matches. - Each
casedescribes a pattern that Swift can compare against the switched value. - Use
defaultwhen the switch does not handle every possible value explicitly. - Swift switch statements do not fall through automatically.
- Switch statements work especially well with enums because Swift can check whether every case has been handled.
If you want to keep going, the next useful step is learning how switch works with enums and associated values. That is where this syntax starts to feel less like a replacement for if and more like one of Swift's core tools.
You can also read Apple's official Swift documentation on switch statements for the full language reference.