100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

break and continue in C++

Learn the difference between `break`, which exits a loop immediately, and `continue`, which skips to the next iteration, with clear C++ examples.

LoopsBeginner6 min readJul 7, 2026
Analogies

1. Intro

break and continue are jump statements that alter a loop's normal flow from inside its body. break immediately terminates the nearest enclosing loop (or switch), jumping to the first statement after it. continue does not exit the loop — it skips the rest of the current iteration's body and jumps straight to the loop's next condition check (and update, for a for loop). Mixing these two up is one of the most common beginner errors.

🏏

Cricket analogy: An umpire calling 'over' immediately ends the current over and moves play on, like break exiting a loop entirely, while calling a 'dead ball' just skips that single delivery and continues the same over, like continue.

2. Syntax

cpp
// break
for (int i = 0; i < n; i++) {
    if (condition) break;   // exits the loop entirely
}

// continue
for (int i = 0; i < n; i++) {
    if (condition) continue; // skips to next iteration
}

3. Explanation

break stops the loop cold: no further iterations occur, and control passes to the statement right after the loop's closing brace. It only affects the single nearest enclosing loop or switch — it does not exit outer loops in a nested structure. continue, by contrast, keeps the loop alive; it simply abandons the remaining code in the current pass. In a for loop, continue still triggers the update expression before re-checking the condition, so counters keep advancing correctly. In a while or do-while loop, you must make sure the variable that affects the condition is updated before any continue, or you risk an infinite loop.

🏏

Cricket analogy: When rain stops a match cold, no more overs are bowled at all — like break halting only the current innings, not a whole tournament of nested matches; but a bowler switching ends after one bad over still lets the innings continue counting overs, like continue still advancing the over count in a for-loop-style structure.

4. Example

cpp
#include <iostream>
using namespace std;

int main() {
    cout << "Using break: ";
    for (int i = 1; i <= 5; i++) {
        if (i == 4) break;
        cout << i << " ";
    }
    cout << endl;

    cout << "Using continue: ";
    for (int i = 1; i <= 5; i++) {
        if (i == 3) continue;
        cout << i << " ";
    }
    cout << endl;

    return 0;
}

5. Output

text
Using break: 1 2 3 
Using continue: 1 2 4 5 

6. Key Takeaways

  • break immediately terminates the nearest enclosing loop (or switch) — no more iterations run.
  • continue skips only the rest of the current iteration and moves on to the next one.
  • In a for loop, continue still runs the update expression before re-testing the condition.
  • break inside a nested loop only exits the innermost loop it is written in, not outer loops.

Practice what you learned

Was this page helpful?

Topics covered

#CStudyNotes#Programming#BreakAndContinueInC#Break#Continue#Syntax#Explanation#StudyNotes#SkillVeris#ExamPrep