Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Keywords and Statements

The break and continue statements

Reference for C++ break and continue: leaving or skipping loop iterations, break inside switch, escaping nested loops, and where each one jumps.

break ends the innermost enclosing loop (for, range-for, while, do) or switch statement immediately. Execution continues with the statement after it. continue skips the rest of the current iteration of the innermost loop and goes on to the next one.

Both only affect the nearest enclosing construct. C++ has no labeled break, so leaving several nested loops at once needs a different tool: a function with return, a flag, or goto.

C++
for (int i = 0; i < 10; ++i) {
    if (i == 3) continue;   // skip 3
    if (i == 6) break;      // stop at 6
}

Try it

C++ Skipping and stopping

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Nested loops and switch

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
break;
continue;
Statement Effect
break; Valid inside a loop body or a switch. Control moves to the statement following the innermost loop or switch.
continue; Valid inside a loop body only. In a for the step expression runs next; in while and do-while the condition is tested next.

Usage notes

  • break outside a loop or switch is a compile error, and so is continue outside a loop (a switch alone does not count).
  • Inside a switch that sits within a loop, continue applies to the loop and break to the switch. This catches many people out.
  • Destructors of local objects declared inside the loop body run when you break or continue, so RAII cleanup is safe.
  • In a while loop, put the update before a continue; otherwise the loop may never finish.
  • Extracting the loop into a function and using return is usually clearer than a flag variable or goto for leaving nested loops.
  • Standard algorithms such as std::find_if or std::ranges::any_of often replace a loop that exists only to break at the first match.
  • A lambda cannot break the loop that calls it; return a value and let the caller decide.