Skip to content
Laviyx Learn

Try

C++ course menu

C++ break and continue: Controlling Loops

Learn how break exits a loop early and continue skips to the next iteration in C++, with search examples, while(true) loops, breaking out of nested loops with a flag or a function, and when goto is (and isn't) acceptable.

Beginner9 min readUpdated
On this page

Sometimes you need to leave a loop early, as soon as you have found what you were looking for, or skip one value and carry on with the next. break and continue do exactly that.

break: leave the loop now#

break immediately ends the innermost loop (or switch) and continues after it:

C++
#include <iostream>
#include <vector>

int main() {
    std::vector<int> readings = {12, 7, 0, -3, 8, -1};

    for (std::size_t i = 0; i < readings.size(); ++i) {
        if (readings[i] < 0) {
            std::cout << "First negative: " << readings[i] << " at index " << i << '\n';
            break;
        }
    }
    std::cout << "Search finished\n";
}
Output
First negative: -3 at index 3
Search finished

Without break, the loop would continue and also report -1. Stopping early saves time, especially on large data.

Knowing whether break happened#

C++ has no for ... else (unlike Python). Use a flag variable to remember whether you found something:

C++
#include <iostream>
#include <vector>

int main() {
    std::vector<int> ids = {104, 221, 357};
    int wanted = 500;
    bool found = false;

    for (int id : ids) {
        if (id == wanted) {
            found = true;
            break;
        }
    }
    std::cout << (found ? "Found" : "Not found") << '\n';
}
Output
Not found

continue: skip to the next iteration#

continue skips the rest of the current iteration and jumps to the next one. In a for loop, the update (++i) still runs:

C++
#include <iostream>

int main() {
    for (int i = 1; i <= 10; ++i) {
        if (i % 3 == 0) {
            continue;   // skip multiples of 3
        }
        std::cout << i << ' ';
    }
    std::cout << '\n';
}
Output
1 2 4 5 7 8 10

continue is handy for skipping invalid data at the top of a loop, which keeps the main logic un-nested:

C++
#include <iostream>
#include <string>
#include <vector>

int main() {
    std::vector<std::string> names = {"Asha", "", "Ravi", "", "Meera"};

    for (const auto& name : names) {
        if (name.empty()) continue;
        std::cout << "Hello, " << name << "!\n";
    }
}
Output
Hello, Asha!
Hello, Ravi!
Hello, Meera!

while (true) with break#

Some loops have their exit condition in the middle of the body. Writing while (true) and leaving with break is clearer than contorting the condition:

C++
#include <iostream>
#include <string>

int main() {
    int total = 0;
    std::string input;

    while (true) {
        std::cout << "Number (or 'done'): ";
        std::cin >> input;
        if (input == "done") break;
        total += std::stoi(input);
    }
    std::cout << "Total: " << total << '\n';
}
Output
Number (or 'done'): 3
Number (or 'done'): 5
Number (or 'done'): done
Total: 8

Breaking out of nested loops#

break exits only the innermost loop. To stop both loops, a clean solution is to put the search in a function and return when found:

C++
#include <iostream>
#include <vector>

bool contains(const std::vector<std::vector<int>>& grid, int target) {
    for (const auto& row : grid) {
        for (int value : row) {
            if (value == target) {
                return true;   // leaves both loops at once
            }
        }
    }
    return false;
}

int main() {
    std::vector<std::vector<int>> grid = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    std::cout << std::boolalpha << contains(grid, 5) << ' ' << contains(grid, 12) << '\n';
}
Output
true false

Alternatives are a found flag checked by the outer loop, or (rarely) goto. goto jumps to a label and makes code hard to follow, so most style guides allow it only for exactly this case, if at all. The function approach is almost always better.

Common mistakes#

Practice#

Practice exercise

Print the first 5 prime numbers greater than 50. Use break to stop checking divisors as soon as one is found, and to stop the outer loop once you have 5 primes.

Show hint

A number n is prime if no number from 2 up to d * d <= n divides it.

Show solution
C++
#include <iostream>

int main() {
    int found = 0;
    for (int n = 51; ; ++n) {
        bool prime = true;
        for (int d = 2; d * d <= n; ++d) {
            if (n % d == 0) {
                prime = false;
                break;
            }
        }
        if (!prime) continue;

        std::cout << n << ' ';
        ++found;
        if (found == 5) break;
    }
    std::cout << '\n';
}
Output
53 59 61 67 71

The outer for has no condition, so it would run forever without the final break.

Key takeaway#

Finished this lesson?