Loop Control Statements
Introduction to Loop Control
C++ provides statements that alter normal loop execution:
- break: exits a loop (or a switch) immediately
- continue: skips to the next iteration of the nearest enclosing loop
- goto: jumps to a labeled statement (generally discouraged due to readability and safety issues)
Note: continue is valid only inside loops; break works in loops and switch.
break Statement
The break statement immediately ends the nearest enclosing loop or switch, transferring control to the statement following it.
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 10; i++) {
if (i == 5) {
break; // exit loop when i is 5
}
cout << i << " ";
}
cout << "\nDone\n";
return 0;
}0 1 2 3 4 Done
continue Statement
The continue statement skips the remaining code in the current iteration and starts the next loop iteration.
In a for loop, the increment expression runs after continue and before the next condition check. In while/do-while, you must ensure loop variables are updated before continue to avoid infinite loops.
#include <iostream>
using namespace std;
int main() {
// for: increment still runs on continue
for (int i = 0; i < 10; ++i) {
if (i % 2 == 0) continue; // skip even numbers
cout << i << ' ';
}
cout << "\n";
// while: ensure manual increment before continue
int j = 0;
while (j < 5) {
if (j == 2) { ++j; continue; } // increment before continue
cout << j << ' ';
++j;
}
}
1 3 5 7 9 0 1 3 4
Nested Loop Control
Loop control statements affect only the innermost loop in which they appear, unless additional logic is used to break outer loops.
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == 1 && j == 1) {
break; // exits only the inner loop
}
cout << "(" << i << "," << j << ") ";
}
cout << '\n';
}
return 0;
}(0,0) (0,1) (0,2) (1,0) (2,0) (2,1) (2,2)
Switch Inside Loops: break vs continue
Inside a loop, break breaks only the switch (not the loop). continue skips to the next loop iteration even if it appears inside a switch.
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 3; ++i) {
switch (i) {
case 1:
continue; // continues the for-loop
default:
break; // breaks only the switch
}
cout << "after switch (i=" << i << ")\n"; // not printed for i==1
}
}
after switch (i=0) after switch (i=2)
goto (Use Sparingly)
goto can be used for single-point cleanup in low-level code, but modern C++ prefers RAII (e.g., smart pointers, containers) to manage resources safely.
C++ forbids jumping into a scope in a way that skips initialization of variables with automatic storage.
#include <cstdio>
int main() {
FILE* f = std::fopen("data.bin", "rb");
if (!f) return 1;
// ... do work; on error, jump to common cleanup
bool error = false;
if (error) goto cleanup;
cleanup:
std::fclose(f);
return 0;
}
Best Practices
- Use break and continue only when they make control flow clearer.
- Avoid goto in C++; favor RAII and structured control flow.
- When continue appears in while/do-while, ensure loop variables are updated beforehand.
- Clearly document any non-obvious early exits to improve readability.
Common Pitfalls
- Assuming break inside a switch breaks the outer loop (it does not).
- Using continue inside a while without updating the loop variable first, causing an infinite loop.
- Confusing continue in a switch inside a loop: it continues the loop, not the switch.