Skip to content
Theory · Loops · lesson 4 of 8

range-for, break and continue

This lesson has two topics. The first is a loop that needs no counter. The second is two statements that change the course of any loop: break and continue.

A loop with a counter is handy when you need positions: 0, 1, 2 and so on. But to add up the numbers 3, 1, 4, 1 and 5, you need the values themselves, not their positions. For this, there is the range-for loop: it puts each value from a list into a variable, one after another.

C++
#include <initializer_list>
#include <iostream>

int main() {
    int sum = 0;
    for (int x : {3, 1, 4, 1, 5}) {
        sum = sum + x;
    }
    std::cout << sum << '\n';  // 14
}

Looping over a range

The header of such a loop has three parts:

  • the variable declaration int x;
  • a colon;
  • the range — here, a list of values in curly braces.

The body runs once for each value: first x is 3, then 1, and so on. On every iteration, x is a new variable that holds a copy of the next value. The sum grows like this: 3, 4, 8, 9, 14.

There is no counter, condition or step, so there is no room for an off-by-one error. The compiler turns a list in curly braces into a value of the standard type std::initializer_list. The line #include <initializer_list> brings in the declaration of this type.

break

The break statement ends the loop at once: the rest of the body and all later iterations are skipped. This loop looks for the first number above 50 that is divisible by 7 and stops there:

C++
for (int i = 51; i <= 100; ++i) {
    if (i % 7 == 0) {
        std::cout << i << '\n';  // 56
        break;
    }
}

For 51 to 55, the condition i % 7 == 0 is false, and the loop moves on. At 56, the condition is true: the program prints 56, and break ends the loop. Without break, the loop would print every such number up to 100: 56, 63, 70 and so on up to 98.

continue

The continue statement skips the rest of the body and moves on to the next iteration. With it, even numbers are left out of a sum:

C++
int sum = 0;
for (int x : {3, 8, 1, 6, 5}) {
    if (x % 2 == 0) {
        continue;
    }
    sum = sum + x;
}
std::cout << sum << '\n';  // 9

For 8 and 6, the condition x % 2 == 0 is true, and the addition is never reached. Only 3, 1 and 5 get into the sum.

In a for loop with a counter, the step still runs after continue, and then the condition is checked. Both statements, break and continue, work in any loop: for, while, do-while and range-for.

Common mistake

continue in a while loop whose counter is incremented at the end of the body:

C++
int i = 0;
while (i < 5) {
    if (i == 2) {
        std::cout << "skip\n";
        continue;  // ✗
    }
    std::cout << i << '\n';
    ++i;
}

A while loop has no step in its header, so continue jumps over ++i as well. Once i is 2, nothing changes it anymore: i < 5 stays true, and the program prints skip forever. Increment i before continue, or rewrite the loop as a for.

Key points

  • A range-for loop copies each value of a list into a variable in turn and needs no counter.
  • break ends the loop at once, and continue skips the rest of the body and moves on to the next iteration.
  • In a for loop with a counter, the step runs even after continue; in a while loop, only if it comes before continue.

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