Skip to content
FollowtheDamnCode

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:

Find the first multiple of 7 above 50
#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 56

Run 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):

Predict: skip the multiples of 3
#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 10

Run 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:

Add numbers until a 0 comes in
#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 0
output
Total: 30

Run 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 a break — 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 0 above, that signals the end.

Check yourself

What does break do inside a loop?

What does continue do?

Which loop runs until a break?