break and continue Statements
break — exits the loop immediately (stops the loop).continue — skips the rest of the current iteration and goes to next.
break Statement — Exit Loop Early
c
#include <stdio.h>
int main() {
// break: stop when we find first even number
int i;
printf("Finding first even: ");
for (i = 1; i <= 10; i++) {
if (i % 2 == 0) {
printf("%d (found!)\n", i);
break; // EXIT the loop immediately
}
}
// break in while: stop at number 5
printf("Until 5: ");
i = 1;
while (1) { // seemingly infinite loop
printf("%d ", i);
if (i == 5) break; // exit when i reaches 5
i++;
}
printf("\n");
return 0;
}▶ Output
Finding first even: 2 (found!) Until 5: 1 2 3 4 5
continue Statement — Skip Current Iteration
c
#include <stdio.h>
int main() {
// continue: skip odd numbers, print only even
printf("Even numbers 1-10: ");
for (int i = 1; i <= 10; i++) {
if (i % 2 != 0) continue; // skip odd, go to next i
printf("%d ", i); // only even numbers reach here
}
printf("\n");
// continue: skip multiples of 3
printf("Skip 3s (1-15): ");
for (int i = 1; i <= 15; i++) {
if (i % 3 == 0) continue; // skip 3, 6, 9, 12, 15
printf("%d ", i);
}
printf("\n");
return 0;
}▶ Output
Even numbers 1-10: 2 4 6 8 10 Skip 3s (1-15): 1 2 4 5 7 8 10 11 13 14
| Statement | Effect | Where Used |
|---|---|---|
| break | Immediately exits loop or switch | for, while, do-while, switch |
| continue | Skips remaining body, goes to next iteration | for, while, do-while only |