Matching Ranges and Tuples
Switch on numeric ranges and tuple shapes.
Matching Ranges and Tuples is a free Swift Academy lesson on CoddyKit — lesson 2 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.
Matching a Range
A case can match a whole range of values using range operators like 1...5. If the value falls in the range, the case runs.
let score = 85
switch score {
case 0..<50:
print("Fail")
case 50...100:
print("Pass")
default:
print("Invalid")
}Closed vs Half-Open
Use closed ranges (...) to include the upper bound and half-open ranges (..<) to exclude it. Pick boundaries that do not overlap.
let age = 18
switch age {
case 0..<13:
print("Child")
case 13..<20:
print("Teen")
default:
print("Adult")
}Order of Range Cases
Swift checks cases top to bottom and runs the first match, so non-overlapping ranges keep behavior predictable.
let temp = 30
switch temp {
case ..<0:
print("Freezing")
case 0..<20:
print("Cool")
case 20...:
print("Warm")
default:
print("?")
}Switching on Tuples
You can switch on a tuple to match several values at once. Each position can be a literal, a range, or a wildcard _.
let point = (1, 1)
switch point {
case (0, 0):
print("Origin")
case (_, 0):
print("On x-axis")
case (0, _):
print("On y-axis")
default:
print("Elsewhere")
}Wildcards in Tuples
The underscore _ matches any value in that position, letting you focus on the parts you care about.
let result = (200, "OK")
switch result {
case (200, _):
print("Success")
case (_, "Error"):
print("Has error")
default:
print("Other")
}Ranges Inside Tuples
Tuple patterns can use ranges in each position for powerful 2D matching.
let coord = (3, 8)
switch coord {
case (0...5, 0...5):
print("Bottom-left quadrant")
case (0...5, 6...10):
print("Top-left quadrant")
default:
print("Far")
}Matching Exact Tuples
You can match a fully specified tuple to detect an exact combination.
let move = ("up", 2)
switch move {
case ("up", 2):
print("Jump twice up")
case ("down", _):
print("Going down")
default:
print("Other move")
}Classifying Numbers
Range matching makes classifying a number into buckets clean and direct.
let n = -4
switch n {
case ..<0:
print("negative")
case 0:
print("zero")
case 1...:
print("positive")
default:
print("?")
}Character Ranges
Because characters are comparable, you can even match ranges of characters.
let ch: Character = "k"
switch ch {
case "a"..."m":
print("First half")
case "n"..."z":
print("Second half")
default:
print("Not a lowercase letter")
}Combining Tuple Positions
Tuple matching evaluates all positions together; every position must match for the case to fire.
let weather = (sunny: true, temp: 25)
switch (weather.sunny, weather.temp) {
case (true, 20...):
print("Nice and warm")
case (true, _):
print("Sunny but cool")
default:
print("Not sunny")
}Why This Is Powerful
Range and tuple patterns let one switch express logic that would need many nested if statements, all in a flat, readable layout.
let (x, y) = (0, 5)
switch (x, y) {
case (0, 0): print("origin")
case (0, _): print("y-axis")
case (_, 0): print("x-axis")
default: print("plane")
}Quick Check
Test range and tuple matching.
Recap: Matching Ranges and Tuples
Cases can match ranges like 1...5 and 0..<n, and tuple patterns let you match multiple values at once using literals, ranges, and the wildcard _.
let p = (0, 4)
switch p {
case (0, _): print("y-axis")
default: print("other")
}Frequently asked questions
Is the “Matching Ranges and Tuples” lesson free?
Yes — the full text of “Matching Ranges and Tuples” 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 “Matching Ranges and Tuples”?
Switch on numeric ranges and tuple shapes. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Matching Ranges and Tuples” 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