break, continue & goto in C++
break exits its nearest loop or switch immediately; continue skips the rest of the current iteration and starts the next one. Together they express early exits and skips far more readably than flag variables — search loops (break on found) and filters (continue on bad items) are the canonical shapes.
goto jumps to a label anywhere in the function. It is shunned in application code but survives in one respectable niche: breaking out of nested loops and unified cleanup in C-style error handling, where the alternatives (flags, extra functions) genuinely read worse.
break exits now
The moment break executes, control jumps past the loop's closing brace — remaining iterations never happen.
continue skips ahead
continue abandons only the current pass: for loops still run their step expression, while loops jump straight to the test.
Only the innermost loop
In nested loops, break/continue affect the closest enclosing loop — exiting outer loops needs a flag, a function return, or goto.
goto's narrow license
Forward jumps to shared cleanup or out of deep nesting are goto's defensible uses; backward jumps that build spaghetti loops are not.
#include <iostream>#include <vector> int main() { std::vector<int> v{3, -1, 4, -2, 5}; for (int x : v) { if (x < 0) continue; if (x == 4) break; std::cout << x << " "; } std::cout << std::endl; return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C++ yourself — building beats watching.