Conditionals
Swift's if/else reads like most other languages, but its switch statement is unusually powerful — it can match ranges, tuples, and patterns, not just exact values, and it doesn't fall through to the next case by default.
if / else if / else
Swift main.swift
let score = 82 if score >= 90 { print("A") } else if score >= 80 { print("B") } else { print("C or below") }
Output
B
Notice there's no parentheses required around the condition — score >= 90, not (score >= 90) — but the curly braces around each branch's body are mandatory even for a single statement.
switch: exhaustive by default
A Swift switch must cover every possible case (or include a default), and each case automatically stops after running — no break needed, no accidental fall-through:
Swift main.swift
let grade = "B" switch grade { case "A": print("Excellent") case "B", "C": print("Solid") default: print("Needs work") }
Output
Solid
Matching ranges
Swift main.swift
let temperature = 72 switch temperature { case ..<32: print("Freezing") case 32..<65: print("Cold") case 65..<85: print("Comfortable") default: print("Hot") }
Output
Comfortable
Note:
case "B", "C": matches either value in one case — that comma is "or," not a tuple. And because switch is exhaustive, leaving out default when your cases don't cover every possibility is a compile error, not a silent runtime gap the way an unhandled case can be in many other languages.