01 / 21

Break & Continue
in C++

Master the two key loop control statements that let you exit early or skip iterations for cleaner, more efficient code.

C++

What Are Loop Control Statements?

Loops repeat code, but sometimes you need to change the normal flow inside a loop. C++ gives you two powerful keywords to do this:

break
Immediately exits the entire loop. No more iterations run.
continue
Skips the rest of the current iteration and jumps to the next one.

Why Do We Need Them?

Without break and continue, you'd have to use complex boolean flags and deeply nested if statements to control loop behavior. These keywords make code:

1
Cleaner: Fewer nested conditionals means easier-to-read code.
2
More efficient: Exit early when the answer is found, instead of looping unnecessarily.
3
Expressive: Your intent is immediately clear to anyone reading the code.

Where They Work

Both break and continue can be used inside any of C++'s loop constructs:

for loop
Classic counted loop. Both keywords work here.
while loop
Condition-checked first. Both keywords work here.
do-while loop
Runs at least once. Both keywords work here.

How break Works

Loop starts Condition? true Execute body break? yes EXIT loop no false End loop

How continue Works

Loop starts Condition? true Execute body continue? yes SKIP to top no Rest of body... false End loop

break vs continue: Key Distinction

break continue
What it does Exits the loop entirely Skips to next iteration
Code after it Never runs (in the loop) Skipped for this iteration only
Remaining iterations All cancelled Continue as normal
Works in switch? Yes No
Common use Search found, error detected Skip invalid data, filter items

The Rules to Remember

1
break exits only the innermost loop or switch it appears in: not outer loops.
2
continue skips only the innermost loop's current iteration.
3
In a for loop, continue still executes the increment expression (\( i\text{++} \)) before checking the condition.
4
In a while loop, continue jumps directly to the condition check: make sure your loop variable updates before continue, or you risk an infinite loop.

How to Apply Them

1
Identify the goal: Are you trying to stop early or skip certain items?
2
Choose the keyword: Stop early = break. Skip items = continue.
3
Write the condition: Place an if statement with your condition, then the keyword inside it.
4
Check for side effects: In while loops, ensure your counter updates before continue to avoid infinite loops.
5
Test edge cases: What if the condition is met on the first iteration? The last? Never?

Example 1: break for Searching

Find the first negative number in an array and stop:

int arr[] = {3, 7, -2, 5, -8}; int found = -1; for (int i = 0; i < 5; i++) { if (arr[i] < 0) { found = i; break; // stop searching } } // found == 2 (index of -2)

Example 2: continue for Filtering

Print only the even numbers from 1 to 10:

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

Example 3: Nested Loops

break only exits the innermost loop. Watch carefully:

for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (j == 1) { break; // exits inner loop only } cout << "(" << i << "," << j << ") "; } } // Output: (0,0) (1,0) (2,0)

Example 4: Danger in while Loops

Using continue in a while loop requires extra care:

Buggy
int i = 0;
while (i < 5) {
  if (i == 3)
    continue;
  cout << i;
  i++;
}
// Infinite loop at \(i = 3\)!
Fixed
int i = -1;
while (i < 4) {
  i++;
  if (i == 3)
    continue;
  cout << i << " ";
}
// Outputs: 0 1 2 4

Execution Trace: break vs continue

Looping \( i = 0 \) to \( 4 \), with the condition triggered at \( i = 2 \):

break at \(i=2\)
\(i=0\) ✓
\(i=1\) ✓
\(i=2\) EXIT
\(i=3\)
\(i=4\)
skipped entirely
continue at \(i=2\)
\(i=0\) ✓
\(i=1\) ✓
\(i=2\) SKIP
\(i=3\) ✓
\(i=4\) ✓
body skipped for \(i=2\) only, loop continues

Common Mistakes

✕ Infinite loop with continue
Placing continue before the loop variable update in a while loop causes it to never increment.
✕ Expecting break to exit all loops
In nested loops, break only exits the innermost loop. Outer loops keep running.
✕ Using continue in switch
continue is not valid inside a switch statement unless the switch is inside a loop.

Summary Cheat Sheet

Scenario Use break? Use continue?
Stop when a target is found ✓ Yes ✕ No
Skip invalid input values ✕ No ✓ Yes
Exit on error condition ✓ Yes ✕ No
Skip even/odd numbers ✕ No ✓ Yes
Process until a sentinel ✓ Yes ✕ No
Ignore blank lines in input ✕ No ✓ Yes

Question 1 of 5

What does the break statement do when executed inside a loop?

Question 2 of 5

What is the output of this code?

Question 3 of 5

What happens when this code runs?

Question 4 of 5

What is the output?

Question 5 of 5

You are reading user input in a loop and want to skip blank lines but keep processing non-blank lines until the user types "quit". Which combination is correct?