break, continue and Labeled Statements
Controlling loop flow with break, continue and labeled loops in Swift.
Welcome
`break` and `continue` give you fine-grained control over loop execution. Labeled statements extend this power to nested loops. Let's learn when each is appropriate.
break — Exit the Loop Early
`break` immediately terminates the current loop:
```swift
for i in 1...10 {
if i == 5 { break }
print(i)
}
// 1, 2, 3, 4
```
Execution continues with the first statement after the loop.
All lessons in this course
- for-in Loops with Ranges
- while and repeat-while Loops
- Iterating Collections with for-in
- break, continue and Labeled Statements