Break Statement

The Break Statement in C++

Learn what the break statement does in C++, how it ends a for, while, do-while or switch early, and how it behaves in nested loops, with examples.

On this page
  1. What the break statement does
  2. Basic syntax
  3. Using break in a for loop
  4. Using break in while and do-while loops
  5. Using break in a switch statement
  6. Break inside a switch inside a loop
  7. Breaking out of nested loops
  8. Ending a loop that would otherwise run forever
  9. break, continue and return compared
  10. Common mistakes
  11. Frequently asked questions

The break statement in C++ ends the loop or switch statement that contains it. When break runs, the program leaves that statement right away and continues with the first line after it. Most beginners meet break in their first for or while loop, and it is one of the simplest tools for stopping early. This guide covers what the break statement does, its syntax, and how it works in each loop type and in a switch, with examples you can read line by line.

What the break statement does

A break jumps out of the innermost loop or switch statement around it. It works with for loops (including range-based for), while loops, do-while loops and switch statements. Execution then continues with the first statement after that loop or switch.

break does not apply to if statements. An if is neither a loop nor a switch, so a break inside an if that sits in a loop ends the loop. Keep that in mind when you read code with several levels of braces.

Basic syntax

The statement is the keyword followed by a semicolon:

break;

In practice it sits inside an if that checks whether to stop:

int i = 0;
while (i < 10) {
    if (i == 5) {
        break;
    }
    ++i;
}

The loop stops when i reaches 5. The ++i line does not run for that value, and the loop ends there.

Using break in a for loop

#include <iostream>

int main() {
    for (int i = 1; i <= 10; ++i) {
        if (i == 4) {
            break;
        }
        std::cout << i << " ";
    }
    std::cout << "\n";
    return 0;
}

Output:

1 2 3

The loop is written to count to 10, but it stops at 4. Only 1, 2 and 3 are printed. The update ++i in the loop header also does not run after a break, because the loop has already ended.

Using break in while and do-while loops

If the C while loop syntax feels unfamiliar, remember that a while loop checks its condition before each pass. When the condition alone cannot express the stopping point, break helps. This example searches an array and stops at the first match:

#include <iostream>

int main() {
    int numbers[] = {4, 8, 15, 16, 23, 42};
    int size = 6;
    int target = 16;
    int index = -1;

    int i = 0;
    while (i < size) {
        if (numbers[i] == target) {
            index = i;
            break;
        }
        ++i;
    }

    if (index != -1) {
        std::cout << "Found " << target << " at index " << index << "\n";
    } else {
        std::cout << target << " was not found\n";
    }
    return 0;
}

Output:

Found 16 at index 3

Once numbers[3] matches the target, the loop stores the index and breaks. Without the break, the loop would keep going through the rest of the array.

A do-while loop runs its body at least once and checks the condition after each pass. A break still leaves the whole loop, so that check is skipped:

#include <iostream>

int main() {
    int count = 0;
    do {
        ++count;
        if (count == 3) {
            break;
        }
        std::cout << "count is " << count << "\n";
    } while (count < 10);

    std::cout << "stopped at " << count << "\n";
    return 0;
}

Output:

count is 1
count is 2
stopped at 3

When count reaches 3, the break runs and the program never reaches the while (count < 10) check. The same behavior applies in a do while loop in C example program, so comparing the two forms side by side makes the difference clear.

Using break in a switch statement

A break is most often used with a switch case statement in C, and the same rule applies in C++. Each case label is an entry point. Without a break, control keeps going into the next case. A break at the end of a case stops that and leaves the switch.

#include <iostream>

int main() {
    int day = 2;

    switch (day) {
        case 1:
            std::cout << "Monday\n";
            break;
        case 2:
            std::cout << "Tuesday\n";
            break;
        case 3:
            std::cout << "Wednesday\n";
            break;
        default:
            std::cout << "Another day\n";
    }
    return 0;
}

Output:

Tuesday

Without the break after case 2, control would fall through into case 3 and also print “Wednesday”. Here is that version, using the same day variable:

switch (day) {
    case 2:
        std::cout << "Tuesday\n";
    case 3:
        std::cout << "Wednesday\n";
        break;
}

With day set to 2, the output is:

Tuesday
Wednesday

This is called fall-through. Some code uses it on purpose, such as when several case labels share one block. Most of the time, though, a missing break is a bug.

Break inside a switch inside a loop

A break ends the nearest loop or switch around it. When a switch sits inside a loop, a break inside the switch leaves only the switch, and the loop keeps running:

#include <iostream>

int main() {
    for (int i = 1; i <= 3; ++i) {
        switch (i) {
            case 2:
                break;
            default:
                std::cout << i << "\n";
        }
    }
    return 0;
}

Output:

1
3

For i equal to 2, the break leaves the switch and nothing is printed. The loop then moves on to i equal to 3.

Breaking out of nested loops

A break only leaves the innermost loop. The outer loop continues with its next pass:

#include <iostream>

int main() {
    for (int row = 1; row <= 3; ++row) {
        for (int col = 1; col <= 3; ++col) {
            if (col == 2) {
                break;
            }
            std::cout << "row " << row << ", col " << col << "\n";
        }
    }
    return 0;
}

Output:

row 1, col 1
row 2, col 1
row 3, col 1

C++ has no labeled break, so one break cannot exit two loops at once. These are the usual options:

  • Return from a function. Put the loops in a function and use return when you find what you need. This is often the cleanest choice.
  • Use a flag variable. Set a bool to true before the inner break. After the inner loop, check the flag in the outer loop and break there.
  • Use goto. It can jump out of nested loops, but many C++ style guides discourage it.

Here is the return approach:

#include <iostream>

void printFirstPair(int limit) {
    for (int a = 1; a <= limit; ++a) {
        for (int b = 1; b <= limit; ++b) {
            if (a * b == 6) {
                std::cout << a << " x " << b << " = 6\n";
                return;
            }
        }
    }
}

int main() {
    printFirstPair(6);
    return 0;
}

Output:

1 x 6 = 6

The first match is found in the inner loop. The return ends the whole function, so both loops stop at once.

Ending a loop that would otherwise run forever

while (true) never stops on its own, so the loop needs a break to end. This pattern suits cases where the stopping test fits better inside the body:

#include <iostream>

int main() {
    int count = 0;

    while (true) {
        ++count;
        if (count * count > 50) {
            break;
        }
    }

    std::cout << "First count with a square over 50: " << count << "\n";
    return 0;
}

Output:

First count with a square over 50: 8

The loop checks the square on each pass and stops as soon as it is over 50. The first count that meets that test is 8, because 8 times 8 is 64.

break, continue and return compared

Statement What it does
break Ends the innermost loop or switch statement
continue Skips the rest of the current loop pass and starts the next one
return Ends the current function, including every loop inside it

continue applies to loops only, so it does not end a switch. Use break when the whole loop is finished, continue when only the current pass is.

Common mistakes

  • Using break outside a loop or switch. C++ only allows break inside a loop or switch. Placing it elsewhere, such as directly in a function body, is a compile error.
  • Expecting break to leave an if block. An if is not a loop, so the break ends the enclosing loop instead. If there is no enclosing loop, it is a compile error.
  • Forgetting the break in a switch case. The next case runs too, as shown above.
  • Expecting one break to exit several loops. It exits only the innermost one. Use one of the approaches in the nested loops section.

Frequently asked questions

Does break work in a range-based for loop?

Yes. A break inside for (int value : values) ends that loop the same way it ends a classic for loop.

Can break exit an if statement?

No. Because an if is not a loop or switch, a break always targets the nearest loop or switch around it. If there is none, the code does not compile.

Is there a labeled break in C++?

No. Unlike Java, C++ has no labeled break. For nested loops, use a function with return or a flag variable.

break is a small statement with a clear job: stop the innermost loop or switch, then continue after it. Keep it in mind for three cases: stopping a loop early, ending a switch case, and leaving a loop that would otherwise never stop. Run each example yourself, then change the target value or the loop bounds to see how the output changes.

Keep exploring

Guides from other topics

All topics →