Control Flow
Swift provides several ways to control the flow of execution, including conditionals and loops.
Syntax Rules
Across all control flow structures in Swift:
- Braces
{}: Braces are mandatory for all blocks (if, for, while, switch, etc.), including single-line blocks. - Parentheses
(): Parentheses are optional and usually omitted for all conditions (like iniforwhile). - Semicolons
;: Semicolons are optional (at the end of statements) everywhere in Swift, unless you're packing multiple statements onto one line.
Comparison and Logical Operators
Control flow often relies on comparing values or combining conditions.
Comparison Operators
| Operator | Description |
|---|---|
== | Equal to |
!= | Not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
Logical Operators
| Operator | Description |
|---|---|
&& | Logical AND (both conditions must be true) |
|| | Logical OR (at least one condition must be true) |
! | Logical NOT (inverts a boolean value) |
Conditionals
If Statement
Standard if, else if, and else blocks.
let score = 85
if score >= 90 {
print("Grade: A")
} else if score >= 80 {
print("Grade: B")
} else {
print("Grade: C")
}Ternary Operator
The ternary conditional operator is a concise alternative to an if-else statement. It takes the form question ? answerIfTrue : answerIfFalse.
let score = 85
let result = score >= 50 ? "Pass" : "Fail"
print(result) // PassSwitch Statement
Swift switch statements are powerful and must be exhaustive (covering all possibilities or using a default case).
- No Implicit Fallthrough: Swift automatically exits the
switchonce a match is found. You don't need to writebreakat the end of every case. - Flexible Matching: Cases can match multiple values (separated by commas), tuples, and ranges (e.g.,
1...3for numbers 1 through 3).
let count = 5
switch count {
case 0:
print("None")
case 1...3: // Range matching
print("A few")
case 4, 5, 6: // Multiple value matching
print("Several")
default:
print("Many")
}Cases can also use a where clause to check for additional conditions.
let number = 10
switch number {
case let x where x % 2 == 0:
print("\(x) is even")
default:
print("It's odd")
}Loops
For-In Loop
Used to iterate over ranges, arrays, dictionaries, or sets.
// Iterate over a range
for i in 1...5 {
print(i) // 1, 2, 3, 4, 5
}
// Half-open range (excludes the upper bound)
for i in 1..<5 {
print(i) // 1, 2, 3, 4
}If you do not need each value from a sequence, use an underscore _ in place of a variable name to ignore the values.
for _ in 1...3 {
print("Hello")
}To skip values or count backwards, use the stride(from:to:by:) or stride(from:through:by:) functions instead of a standard range.
for i in stride(from: 0, to: 10, by: 2) {
print(i) // 0, 2, 4, 6, 8
}While Loops
while: Evaluates the condition before each pass.repeat-while: Evaluates the condition after each pass (guarantees at least one execution).
var count = 5
while count > 0 {
print(count)
count -= 1
}
repeat {
print("Executed once")
} while falseControl Transfer
Control transfer statements change the order of execution. The break statement ends a loop immediately, and continue skips to the next iteration of the loop.
When dealing with nested loops, you can use statement labels to specify exactly which loop to break or continue. Prefix a loop with a label name and use that same label with your break statement.
outerLoop: for i in 1...3 {
for j in 1...3 {
if i == 2 && j == 2 {
break outerLoop
}
print("i:\(i), j:\(j)")
}
}