Cppbreak & continue

break & continue

Sometimes you need to change a loop's normal flow before it finishes on its own: stop it entirely, or skip the rest of the current pass and move to the next one. C++ provides two keywords for exactly this: break and continue.
break: Exit the Loop Entirely
break immediately stops the innermost loop (or switch statement) it's inside, and execution resumes at the first line after that loop.

CPP
#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 10; i++) {
        if (i == 5) {
            break; // stop the loop entirely once i reaches 5
        }
        cout << i << " ";
    }
    cout << endl;
    // Output: 1 2 3 4

    return 0;
}

A very common use of break is searching for something and stopping as soon as it's found, instead of wastefully checking every remaining element.

CPP
#include <vector>
#include <iostream>
using namespace std;

int main() {
    vector<int> numbers = {4, 8, 15, 16, 23, 42};
    int target = 16;
    bool found = false;

    for (int n : numbers) {
        if (n == target) {
            found = true;
            break; // no need to keep looking
        }
    }

    cout << (found ? "Found it!" : "Not found.") << endl;
    return 0;
}
continue: Skip to the Next Iteration
continue skips the rest of the current iteration's body and jumps straight to the loop's next check (the condition in a while, or the increment step in a for). Unlike break, the loop keeps running — it just moves on early.

CPP
#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 10; i++) {
        if (i % 2 != 0) {
            continue; // skip odd numbers
        }
        cout << i << " ";
    }
    cout << endl;
    // Output: 2 4 6 8 10

    return 0;
}
break and continue in Nested Loops
When loops are nested, both break and continue only affect the innermost loop that contains them — the outer loop is completely unaffected and continues normally.

CPP
#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 3; i++) {
        for (int j = 1; j <= 3; j++) {
            if (j == 2) {
                break; // exits only the inner (j) loop
            }
            cout << "i=" << i << " j=" << j << endl;
        }
    }
    // Outer loop still runs all 3 times; inner loop stops early each time.

    return 0;
}
Note
C++ has no built-in "labeled break" like Java to exit multiple nested loops directly. The common, goto-free workarounds are: extracting the nested loops into their own function and using return, or setting a boolean flag that the outer loop checks. The goto statement technically can jump out of nested loops in one step, but it is almost universally discouraged in modern C++ because it makes control flow much harder to follow and reason about.

CPP
// A clean way to "break" out of nested loops: a flag checked by the outer loop.
bool stop = false;
for (int i = 0; i < 3 && !stop; i++) {
    for (int j = 0; j < 3; j++) {
        if (i == 1 && j == 1) {
            stop = true;
            break; // exits inner loop; outer loop exits on its next check
        }
        cout << i << "," << j << " ";
    }
}
Key Points
  • break exits the innermost loop (or switch) immediately; execution resumes after it.

  • continue skips the rest of the current iteration and moves on to the next one.

  • Both break and continue only affect the innermost enclosing loop in nested loops.

  • Use a boolean flag or a helper function with return to exit multiple nested loops cleanly.

  • goto can jump out of nested loops directly but is discouraged in idiomatic modern C++.