C Break and Continue
The break statement immediately exits the nearest enclosing loop or switch statement. The continue statement skips the rest of the current iteration and jumps to the next one.
Both are useful for controlling loop flow precisely, such as stopping early when a target is found, or skipping invalid values without stopping the whole loop.
break;
continue;break
break stops loop execution entirely and jumps to the code right after the loop. It's often used to exit a loop early once a condition is satisfied, like finding a target value.
continue
continue skips the remaining code in the current loop iteration and moves directly to the next one, without exiting the loop entirely.
#include <stdio.h>
int main() {
for (int i = 0; i < 10; i++) {
if (i == 3) break;
printf("%d\n", i);
}
return 0;
}0
1
2The loop stops entirely as soon as i equals 3.
#include <stdio.h>
int main() {
for (int i = 0; i < 5; i++) {
if (i % 2 == 0) continue;
printf("%d\n", i);
}
return 0;
}1
3Even numbers are skipped with continue, so only odd numbers print.
Key points
- break exits the nearest loop or switch immediately.
- continue skips to the next iteration of the loop.
- Both work inside for, while and do...while loops.
- break also works inside switch statements to prevent fall-through.
