Skip to content
Theory · Conditions and loops · lesson 5 of 13

break and continue

Let's go back to the runner at the stadium. The coach asked for a hundred laps, but sometimes the run has to stop early. The runner is looking for a lost key on the track: as soon as it turns up, the runner leaves the track, and the remaining laps are no longer needed. And on a slippery stretch, the runner may cut the rest of the lap short and start the next one right away.

A loop has two statements for these two cases:

  • break ends the whole loop: the runner leaves the track;
  • continue ends only the current pass: the runner drops the lap and starts the next one.

A for loop goes through every value its header sets, but the answer is sometimes found earlier. To find the first number from 50 to 100 that divides by 7, there is no need to check the remaining numbers. Once it is found, the loop is stopped with the break statement:

JavaScript
for (let i = 50; i <= 100; i++) {
    if (i % 7 === 0) {
        console.log(i); // 56
        break;
    }
}

console.log("done"); // done

Let's see what happens:

  1. For 50–55, the condition i % 7 === 0 is false, and the loop moves on.
  2. 56 divides by 7 with no remainder, so the program prints 56.
  3. break ends the loop, and the program prints done.

break leaves the loop

break ends the whole loop, not only the if braces it stands in. The remaining iterations do not run, neither does the step i++, and the program moves on to the code after the loop. That is why done is printed right after 56.

Without break, the loop would check every number up to 100 and also print 63, 70, 77, 84, 91 and 98.

continue skips an iteration

continue ends only the current iteration: the rest of the body is skipped, and the loop moves on to the next value. In a for loop, the step i++ runs first. Here the even numbers are skipped, and the odd ones are appended to a string:

JavaScript
let odd = "";

for (let i = 1; i <= 6; i++) {
    if (i % 2 === 0) {
        continue;
    }
    odd += i;
}

console.log(odd); // 135

odd += i appends the number to the end of the string: "" + 1 is "1", and "1" + 3 is "13". The loop does not stop after continue, so 3 and 5 also reach the line odd += i.

The same can be written without continue, with the condition i % 2 !== 0 around odd += i. continue helps with a long body: the check stands at the start, and the rest of the code needs no nested if.

Interviewers often ask for the difference between these two statements. The short answer: break ends the whole loop, and continue ends only the current iteration.

Nested loops and labels

Here the stadium picture ends: a runner has one track, but loops can be nested. When one loop stands inside another, break and continue affect only the inner one. To leave the outer loop, put a label before it, a name with a colon such as outer: for (…). Then write break outer:

JavaScript
outer: for (let i = 1; i <= 3; i++) {
    for (let j = 1; j <= 3; j++) {
        if (i * j === 4) {
            break outer;
        }
        console.log(i, j);
    }
}
// 1 1
// 1 2
// 1 3
// 2 1

When i is 2 and j is 2, the product is 4. break outer ends both loops at once, and the pair 2 2 is not printed. A plain break would end only the inner loop, and the outer one would move on to i equal to 3.

Labels are rare. More often, nested loops go into a function, and return leaves it.

Common mistake

In a while loop, continue comes before the variable changes:

JavaScript
let i = 0;

while (i < 5) {
    if (i === 2) {
        continue; // ✗
    }
    console.log(i);
    i++;
}

The program prints 0 and 1, then hangs. If you ran it yourself, stop it with Ctrl+C in the terminal.

When i is 2, continue skips the rest of the body together with i++, so i stays 2 on every following iteration. The runner cuts the lap short but forgets to make the mark, and the count never grows. A while loop has no step in its header, so change the variable before continue or rewrite the loop as a for.

Key points

  • break ends the whole loop, and the program moves on to the code after it.
  • continue skips the rest of the current iteration; in a for loop, the step runs before the next one.
  • In nested loops, break affects the inner loop, and break with a label affects the labeled one.
  • In a while loop, change the variable before continue, or the loop hangs.

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way