break and continue
Leave a loop early, or skip one round.
4 minute read
Big idea
break leaves a loop immediately. continue skips the rest of this round and moves on to the next one.
break: stop early
Searching? Stop the moment you find it:
#include <stdio.h>
int main(void) {
for (int n = 51; n <= 100; n++) {
if (n % 7 == 0) {
printf("Found %d\n", n);
break;
}
}
return 0;
}output
Found 56Run and edit it in the app.
Without break, the loop would keep going and print 63, 70, 77…
continue: skip this one
continue jumps straight to the next round (in a for loop, the change step n++ still runs):
#include <stdio.h>
int main(void) {
for (int i = 1; i <= 10; i++) {
if (i % 3 == 0) {
continue;
}
printf("%d ", i);
}
printf("\n");
return 0;
}What does it print? Show the answer
1 2 4 5 7 8 10Run and edit it in the app.
Loops that end with break
Sometimes the stopping point is easiest to find in the middle of the loop. while (1) loops forever — until a break:
#include <stdio.h>
int main(void) {
int total = 0, n;
while (1) {
scanf("%d", &n);
if (n == 0) {
break;
}
total += n;
}
printf("Total: %d\n", total);
return 0;
}input
4 9 12 5 0output
Total: 30Run and edit it in the app.
Inside a for loop, which statement ends the loop entirely?
Watch it
Think of it like
Reading a list of flats to rent. continue: "no pets allowed — skip to the next listing". break: "found the perfect one — stop reading".
🕷️ Spidey-sense: On patrol, a false alarm is a
continue— next block. A real emergency is abreak— patrol's over, I'm going.
Key terms
- break — Leaves the nearest loop (or
switch) immediately. - continue — Skips the rest of the current round of the nearest loop.
- Sentinel value — A special input, like
0above, that signals the end.
Check yourself
What does break do inside a loop?
What does continue do?
Which loop runs until a break?