Skip to content
Theory · Loops · lesson 4 of 5

break and continue

Let's go back to the running track. So far the runner stopped only when the laps ran out or the condition became false. But sometimes the goal is reached in the middle of the distance, and there is no point in running on. Then the runner leaves the track at once.

A loop that searches for something behaves the same way: once it has found what it needs, it should stop. The break statement ends the loop at once. This program looks for the first number above 50 that is divisible by 7:

SWIFT
for n in 51...100 {
    if n % 7 == 0 {
        print(n) // 56
        break
    }
}

Two ways to change the course of a loop

break and continue work in any loop: in for-in, while and repeat-while.

break

break is the runner leaving the race. The lines of the body below break do not run, there are no more iterations, and the program goes on from the line after the loop.

Let's go through the example iteration by iteration:

  1. For n from 51 to 55 the remainder of division by 7 is not zero, and the loop goes on.
  2. When n is 56, the condition is true, and the program prints 56.
  3. break ends the loop, and the numbers from 57 to 100 are never checked.

break sits inside the if, but it ends the whole loop, not only the if.

continue

continue is the runner cutting the rest of the current lap short and starting the next one right away. The loop does not end: for-in takes the next value, while checks its condition again, and the body starts over.

SWIFT
for n in 1...6 {
    if n % 3 == 0 {
        continue
    }
    print(n, terminator: " ") // 1 2 4 5
}

The numbers 3 and 6 are divisible by 3, so for them continue skips the print. If you replace continue with break, the program prints only 1 2: the loop ends when n is 3.

Here the runner picture is inaccurate: a runner decides for themselves when to leave the track. break and continue act only when execution reaches their line, which is why they usually sit inside an if.

Nested loops

A loop inside another loop is called nested. On the track it is a workout of several sets: in each set the runner runs their own laps. On each iteration of the outer loop, the inner loop runs all its iterations from the start.

This is how you go through pairs of numbers. The program below looks for pairs a and b from 1 to 3 whose sum is 5:

SWIFT
for a in 1...3 {
    for b in 1...3 {
        if a + b == 5 {
            print(a, b, terminator: "; ") // 2 3; 3 2;
            break
        }
    }
}

Let's see what break does:

  1. When a is 1, no sum equals 5, and the inner loop runs all three iterations.
  2. When a is 2, the sum 2 + 3 equals 5: the program prints the pair, and break ends the inner loop.
  3. The outer loop keeps going, and when a is 3, it finds the pair 3 2.

break ends only the loop whose body it is in—here, the inner one. The set is over, but the workout goes on.

A labeled break

What if you need only the first pair? Then you have to leave both loops at once. For that the outer loop gets a label—a name with a colon before the word for: search: for a in 1...3. You choose the label's name yourself: search, outer or any other. The statement break search ends the loop with that label together with all the loops inside it.

This is not the argument label from the print lesson. That one stands before a value in a function call, while a loop label stands before the loop itself and gives it a name.

Press "Next step" and watch the constants a and b: after break search both disappear, and there is no iteration with a equal to 3.

Livebreak with a labelstep 1 of 11 · frames: 1

The outer loop starts its first iteration, and the constant a is 1.

Watch when the constants a and b appear and disappear

A plain break ends the set, and a labeled break ends the whole workout. continue works with a label too: continue search goes straight to the next iteration of the outer loop.

Interviewers often ask how to leave two nested loops at once. The answer: put a label on the outer loop and write break with that label.

Common mistake

break written inside a switch that sits in a loop:

SWIFT
for n in 1...5 {
    switch n {
    case 3:
        break // ✗
    default:
        print(n, terminator: " ")
    }
}

The goal was to stop the loop at three, but the program prints 1 2 4 5. Inside a switch a plain break leaves only the switch, as in an empty case, and the loop goes on. Put a label on the loop and leave by it: with numbers: for n in 1...5 and break numbers the program prints 1 2.

Key points

  • break ends the loop at once, and continue skips the rest of the current iteration and moves on to the next one.
  • In nested loops a plain break ends only the loop whose body it is in.
  • A break with the outer loop's label, such as break search, leaves both loops at once.
  • Inside a switch a plain break leaves only the switch, and a labeled break ends the loop.