Sometimes a loop needs to exit early, skip just one iteration, or a program needs to jump directly to another labeled point. C provides three jump statements for exactly this: break, continue, and goto. This is Post 2 of the Unit II series — Post 1 covered while, for, and do-while loops.
break: Exit the Loop Immediately
break stops the nearest enclosing loop (or switch) instantly, with no further iterations.
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 6) {
break; // exit the loop entirely once i reaches 6
}
printf("%d ", i);
}
printf("\n");
return 0;
}
Sample Output
1 2 3 4 5
continue: Skip to the Next Iteration
continue skips the rest of the current iteration’s body and jumps straight to the loop’s next condition check (for a for loop, this includes running the update step first).
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // skip even numbers, move to next i
}
printf("%d ", i);
}
printf("\n");
return 0;
}
Sample Output
1 3 5 7 9
break vs. continue in one line: break says “stop the whole loop right now.” continue says “stop just this one iteration, but keep looping.”
goto: Unconditional Jump to a Label
goto label;
...
label:
// code
#include <stdio.h>
int main() {
int i = 1;
start:
if (i <= 5) {
printf("%d ", i);
i++;
goto start;
}
printf("\n");
return 0;
}
Sample Output
1 2 3 4 5
Why goto has a bad reputation: goto can jump anywhere within the same function, forward or backward, which makes program flow very hard to trace in anything beyond a trivial example — this is the classic “spaghetti code” problem. Modern C style strongly prefers loops (while/for/do-while) and break/continue for the same effects. goto is mostly seen today for one specific accepted use: jumping to a single cleanup/error-handling label near the end of a function to avoid duplicating cleanup code across multiple early-exit points.
A More Defensible goto Example: Error Cleanup
#include <stdio.h>
int main() {
int n;
printf("Enter a positive number: ");
scanf("%d", &n);
if (n <= 0) {
printf("Error: number must be positive.\n");
goto end;
}
printf("You entered: %d\n", n);
printf("Square: %d\n", n * n);
end:
printf("Program finished.\n");
return 0;
}
break and continue with Nested Loops
A common misconception: break and continue only affect the SINGLE innermost loop they’re written inside — they cannot directly exit multiple nested loops at once. To break out of nested loops entirely, a common pattern is a flag variable checked by the outer loop (as seen in the 2D array search program) or, in more advanced code, goto to a label right after the outer loop.
#include <stdio.h>
int main() {
int found = 0;
for (int i = 1; i <= 3 && !found; i++) {
for (int j = 1; j <= 3; j++) {
if (i == 2 && j == 2) {
found = 1;
break; // only exits the INNER loop
}
printf("(%d,%d) ", i, j);
}
}
printf("\n");
return 0;
}
Sample Output
(1,1) (1,2) (1,3) (2,1)
Summary
breakexits the nearest enclosing loop orswitchimmediately.continueskips the rest of the current iteration and moves to the next one.gotojumps unconditionally to a labeled point — powerful but generally discouraged except for narrow, well-established patterns like centralized error cleanup.- Neither
breaknorcontinuecan reach past their immediately enclosing loop — nested loops need a flag (or, rarely,goto) to exit multiple levels at once.
Further Reading
- Jump Statements in C Programming: break, continue, goto, return (video embedded above)
- GeeksforGeeks: break Statement in C
Next in this series: one-dimensional arrays.