← Back to Swift Course | Chapter 3: Control Flow | Lesson 6 of 8

break and continue

break stops a loop completely, while continue just skips the rest of the current lap and moves to the next one.

Using break to Exit Early

break immediately stops the loop entirely, skipping any remaining iterations.

Example: Using break to Exit Early

markup
for number in 1...10 {
    if number == 4 {
        break
    }
    print("Number: \(number)")
}

Using continue to Skip an Iteration

continue skips the rest of the current iteration's code and jumps straight to the next one, without stopping the loop entirely.

Example: Using continue to Skip an Iteration

markup
for number in 1...6 {
    if number % 2 == 0 {
        continue
    }
    print("Odd number: \(number)")
}

Labeled Loops

A label placed before a loop lets break or continue target that specific loop even from inside a nested loop.

Example: Labeled Loops

markup
outer: for i in 1...3 {
    for j in 1...3 {
        if j == 2 {
            continue outer
        }
        print("i=\(i), j=\(j)")
    }
}
Common Mistakes
  1. Using break when the intent was only to skip the current iteration; that requires continue, not break.
  2. Forgetting that break inside a switch exits the switch itself, not an enclosing loop -- use a labeled break for that case.
  3. Placing code after a continue in the same iteration expecting it to still run; nothing after continue executes for that pass.
Chapter Summary
  • break immediately exits the nearest enclosing loop (or switch).
  • continue skips the rest of the current iteration and moves to the next one.
  • Labeled loops (outerLoop: for ...) let break or continue target a specific outer loop.
  • Both are commonly combined with an if condition to control loop flow.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.