Beginner 9 min

Break and Continue

Master loop control with break and continue statements to exit loops early or skip iterations

Learn how to control loop execution with break and continue statements for early exits and skipping iterations.

A Simple Example

#include <iostream>

int main() {
    // Break: Exit loop early when found
    std::cout << "Using break:" << "\n";
    for (int i{1}; i <= 100; i++) {
        if (i % 7 == 0) {
            std::cout << "First multiple of 7: " << i << "\n";
            break;  // Found it! Exit the loop
        }
    }

    // Continue: Skip certain iterations
    std::cout << "\nUsing continue (print odd numbers):" << "\n";
    for (int i{1}; i <= 10; i++) {
        if (i % 2 == 0) {  // If even
            continue;       // Skip printing this iteration
        }
        std::cout << i << " ";  // Only prints odd numbers
    }
    std::cout << "\n";

    return 0;
}

Breaking It Down

Break Statement

  • What it does: Immediately exits the current loop (for, while, do-while)
  • Use when: You found what you're searching for or need to stop early
  • Scope: Only affects the innermost loop it's inside
  • Remember: Execution continues after the loop, not inside it

Continue Statement

  • What it does: Skips the rest of the current iteration and jumps to the next one
  • Use when: You want to filter out certain values or skip invalid data
  • Behavior: For loops jump to the increment step, while loops to the condition
  • Remember: Only skips the current iteration, doesn't exit the loop

Break vs Continue

  • Break: "I'm done with this entire loop" - exits completely
  • Continue: "Skip this one, show me the next" - stays in loop
  • Break example: Finding first match in search
  • Continue example: Processing only valid entries, skipping invalid ones

Nested Loops Behavior

  • Break only exits the immediate loop it's in, not all nested loops
  • Continue only affects the current loop iteration, not outer loops
  • To exit multiple loops: Use a flag variable or restructure with functions
  • Remember: Each break/continue only controls its own loop level

Why This Matters

  • Sometimes you need to stop a loop early when you've found what you're looking for, or skip certain iterations that don't meet your criteria.
  • These control statements make your loops more efficient and your code more expressive. A search can stop immediately when found instead of checking all remaining items.

Critical Insight

Think of break as an emergency exit and continue as a skip button:

- **Break** = "I found what I need, let's get out of here!" - **Continue** = "This one doesn't interest me, show me the next one!"

Use break when you want to stop searching (like finding the first match). Use continue when you want to filter (like processing only positive numbers).

// Without continue (nested if)
for (int i{1}; i <= 10; i++) {
    if (i % 2 != 0) {  // If odd
        std::cout << i << "\n";
    }
}

// With continue (cleaner!)
for (int i{1}; i <= 10; i++) {
    if (i % 2 == 0) {  // If even
        continue;       // Skip it
    }
    std::cout << i << "\n";
}

Both print the same result, but continue makes the intent clearer: "skip evens, process odds."

Best Practices

Use break for early exit when found: Perfect for search operations where you stop after finding the first match.

Use continue to skip invalid data: Better than nested ifs when filtering. Put the skip condition first, then process valid data.

Keep break/continue close to their condition: Don't bury them deep in nested logic where they're hard to spot.

Consider using functions: If you need to exit multiple nested loops, extract the code into a function and use return instead.

Common Mistakes

Breaking from the wrong loop: In nested loops, break only exits the innermost loop, not all of them. Use a flag variable if you need to exit outer loops too.

Continue in switch statements: Continue doesn't work in switch - it only works in loops. Use break in switch cases instead.

Unreachable code after break: Any code after break in the same block will never execute. The compiler may warn you about this.

Using break when you meant continue: Break exits the entire loop, continue just skips one iteration. They're not interchangeable!

Debug Challenge

This program tries to find numbers divisible by both 3 and 5, but uses the wrong control statement. Click the highlighted line to fix it:

1 #include <iostream>
2
3 int main() {
4 for (int i{1}; i <= 50; i++) {
5 if (i % 3 != 0 || i % 5 != 0) {
6 break; // Skip numbers not divisible by both
7 }
8 std::cout << i << " ";
9 }
10 return 0;
11 }

Quick Quiz

  1. What does this code print? for (int i{0}; i < 5; i++) { if (i == 3) break; std::cout << i << " "; }
0 1 2
0 1 2 3
0 1 2 3 4
  1. What does this code print? for (int i{0}; i < 5; i++) { if (i == 2) continue; std::cout << i << " "; }
0 1 3 4
0 1 2 3 4
0 1
  1. In nested loops, what does break affect?
Only the innermost loop
All loops
Only the outermost loop

Practice Playground

Time to try out what you just learned! Play with the example code below, experiment by making changes and running the code to deepen your understanding.

Lesson Progress

  • Fix This Code
  • Quick Quiz
  • Practice Playground - run once