Skip to content
C++ Better Explained
Go back
C++ break and continue: Control Your Loops (with Examples)
Edit page

C++ break and continue

Loops normally run start to finish, but real programs need to bail out early or skip certain items. That’s what break and continue are for. They look similar but do opposite-feeling jobs, and mixing them up is a classic beginner bug. Here’s the clear version.


break: Leave the Loop Now

break stops the loop immediately and jumps to the code after it, no matter how many iterations were left:

#include <iostream>

int main() {
    for (int i = 1; i <= 10; ++i) {
        if (i == 5)
            break;          // leave the loop entirely
        std::cout << i << " ";
    }
    std::cout << "\n";      // 1 2 3 4
    return 0;
}

The loop was set up to count to 10, but break ends it the moment i hits 5. Think of break as an emergency exit out of the loop.


continue: Skip Just This Round

continue skips the rest of the current iteration and jumps straight to the next one. The loop itself keeps running:

#include <iostream>

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

When i is odd, continue jumps past the cout and moves on. Only the even numbers get printed. Nothing is exited — one lap is just cut short.

If you're looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you're a complete beginner or looking to solidify your understanding. Just $19.

break vs continue at a Glance

StatementWhat it doesLoop keeps running?
breakExits the loop entirelyNo
continueSkips to the next iterationYes

A simple way to remember it: break ends the whole loop, continue ends only the current pass.


A Practical Use: Stop When You Find It

break shines when searching — once you’ve found what you want, there’s no reason to keep looping:

#include <iostream>
#include <vector>

int main() {
    std::vector<int> data = {4, 8, 15, 16, 23, 42};
    int target = 15;
    for (std::size_t i = 0; i < data.size(); ++i) {
        if (data[i] == target) {
            std::cout << "Found at index " << i << "\n";
            break;   // no need to keep looking
        }
    }
    return 0;
}

Without break, the loop would needlessly scan every remaining element. Stopping early is both faster and clearer.


break in Nested Loops

A break only exits the innermost loop it sits in — the outer loop carries on:

#include <iostream>

int main() {
    for (int i = 1; i <= 3; ++i) {
        for (int j = 1; j <= 3; ++j) {
            if (j == 2)
                break;     // only breaks the inner loop
            std::cout << i << "," << j << "  ";
        }
    }
    std::cout << "\n";     // 1,1  2,1  3,1
    return 0;
}

For each value of i, the inner loop prints j = 1 and then breaks at j = 2. The outer loop keeps going, which is why all three i values appear.


A Gotcha: break Inside switch

Inside a loop, a break in a switch ends the switch, not the loop:

#include <iostream>

int main() {
    for (int i = 1; i <= 3; ++i) {
        switch (i) {
            case 1:
                std::cout << "one\n";
                break;     // breaks the switch, not the loop
            case 2:
                std::cout << "two\n";
                break;
            default:
                std::cout << "other\n";
        }
    }
    return 0;
}

All three numbers print, because each break only leaves the switch. The loop runs to completion as normal. Keep this in mind when you have a switch inside a loop.



Take Your C++ Further

If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.

👉 Get the C++ Better Explained Ebook — $19

📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Edit page
Share this post on:

Next Post
C++ char to int Conversion: The Right Way (and the Common Trap)