
Break Continue in C
In C, break
and continue
are loop control statements that help you manage how loops behave — great for skipping stuff or exiting early! ??
? break
Statement
break
is used to exit a loop (or switch
statement) immediately, regardless of loop condition.
? Example
int main() { for (int i = 1; i <= 10; i++) { if (i == 5) { break; // Exit loop when i is 5 } printf("%d ", i); } return 0;}
Output:
1 2 3 4
? Loop stops when i == 5
.
? continue
Statement
continue
skips the rest of the current iteration and jumps to the next one.
? Example
#include <stdio.h>int main() { for (int i = 1; i <= 5; i++) { if (i == 3) { continue; // Skip when i is 3 } printf("%d ", i); } return 0;}
Output:
1 2 4 5
? It skips i == 3
but keeps looping.
? With while
or do...while
? break
inside while
int i = 1;while (i <= 10) { if (i == 6) break; printf("%d ", i); i++;}
? continue
inside do...while
int i = 0;do { i++; if (i == 2) continue; printf("%d ", i);} while (i < 5);
Output:
1 3 4 5
? Skips printing when i == 2
? Summary
Statement | Meaning |
---|---|
break | Exit the loop immediately |
continue | Skip to next loop iteration |