break and continue Basics
Exit or skip iterations cleanly.
What break Does
The break statement immediately exits the innermost loop, skipping any remaining iterations.
for n in 1...10 {
if n == 4 {
break
}
print(n)
}
print("done")What continue Does
The continue statement skips the rest of the current iteration and jumps to the next one.
for n in 1...6 {
if n % 2 == 0 {
continue
}
print(n)
}All lessons in this course
- break and continue Basics
- Labeled Statements
- Breaking Out of Nested Loops
- Loop Control in switch Context