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
// 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
#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
Using break: 1 2 3
Using continue: 1 2 4 5 6. Key Takeaways
breakimmediately terminates the nearest enclosing loop (orswitch) — no more iterations run.continueskips only the rest of the current iteration and moves on to the next one.- In a
forloop,continuestill runs theupdateexpression before re-testing the condition. breakinside a nested loop only exits the innermost loop it is written in, not outer loops.
Practice what you learned
1. What does `break` do when executed inside a loop?
2. What is the output of: `for (int i = 1; i <= 4; i++) { if (i == 2) continue; cout << i; }`?
3. In a `for` loop, does `continue` skip the update expression (e.g. `i++`)?
4. If a `break` statement is inside the inner loop of two nested `for` loops, what does it exit?
5. What is a risk of using `continue` carelessly in a `while` loop?
Was this page helpful?
You May Also Like
for Loop in C++
Learn the C++ `for` loop syntax, how init/condition/increment control iteration, and when it is the best choice for a known number of repetitions.
Nested Loops in C++
See how placing one loop inside another lets you process grids and patterns in C++, with a full trace of outer × inner iteration counts.
switch Statement in C++
Master the C++ `switch` statement — matching an expression against multiple case labels, the role of break, fall-through behavior, and default.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics