Swift Break/Continue
Swift Break/Continue
Use break to exit a loop early, and continue to skip to the next iteration.
break
Stop the loop immediately when a condition is met.
This example stops printing when i reaches 4.
continue
Skip the rest of the current iteration but keep looping.
Example
for i in 1...5 {
if i % 2 == 0 { continue }
print(i) // only odd numbers
}
This example prints only odd numbers using continue.
Tip: Prefer clear conditions and early exits to reduce nested branches.