TExcelTAD
Home › C › C Break/Continue

C Break and Continue

Normally a loop runs until its condition becomes false. Two statements let you change that from inside the body:

  • break leaves the loop immediately.
  • continue skips the rest of the current pass and moves on to the next one.

break

You have already seen break end a switch. Inside a loop it does the same: execution jumps to the first statement after the loop. This is useful when you have found what you were looking for and further passes would be wasted work.

c · try it yourself
#include <stdio.h>

int main(void) {
    // Find the first multiple of 7 that is greater than 50
    for (int n = 51; n < 100; n++) {
        if (n % 7 == 0) {
            printf("Found it: %d\n", n);
            break;
        }
        printf("%d is not a multiple of 7\n", n);
    }
    printf("Search over.\n");
    return 0;
}
51 is not a multiple of 7
52 is not a multiple of 7
53 is not a multiple of 7
54 is not a multiple of 7
55 is not a multiple of 7
Found it: 56
Search over.

continue

continue jumps straight to the next pass. In a for loop, the update part (i++) still runs; in a while loop, control goes directly back to the condition.

c · try it yourself
#include <stdio.h>

int main(void) {
    // Print 1..15 but skip multiples of 3
    for (int i = 1; i <= 15; i++) {
        if (i % 3 == 0) {
            continue;
        }
        printf("%d ", i);
    }
    printf("\n");
    return 0;
}
1 2 4 5 7 8 10 11 13 14 

continue is a good way to filter out cases early and keep the main logic of the loop un-nested.

Watch out: In a while loop, make sure the update happens before the continue. Otherwise the loop variable never changes and you get an infinite loop:

c
int i = 0;
while (i < 10) {
    if (i == 5) continue;   // i stays 5 forever!
    i++;
}

Using both in a while loop

Here a loop processes a list of readings. Negative readings are invalid and skipped; a reading of -999 is a stop marker that ends the loop:

c · try it yourself
#include <stdio.h>

int main(void) {
    int readings[] = {12, 15, -3, 20, -1, 18, -999, 50};
    int i = 0;
    int total = 0, used = 0;

    while (1) {
        int r = readings[i];
        i++;
        if (r == -999) {
            break;            // stop marker
        }
        if (r < 0) {
            continue;         // ignore invalid values
        }
        total += r;
        used++;
    }
    printf("Used %d readings, total %d, average %.2f\n",
           used, total, (double) total / used);
    return 0;
}
Used 4 readings, total 65, average 16.25

while (1) loops forever on its own; the break is the only way out. This pattern is common when the exit condition is easiest to check in the middle of the body.

break only exits one level

In nested loops, break and continue affect only the innermost loop that contains them:

c · try it yourself
#include <stdio.h>

int main(void) {
    for (int row = 1; row <= 3; row++) {
        for (int col = 1; col <= 5; col++) {
            if (col > row) {
                break;            // ends the inner loop only
            }
            printf("(%d,%d) ", row, col);
        }
        printf("\n");             // the outer loop keeps going
    }
    return 0;
}
(1,1) 
(2,1) (2,2) 
(3,1) (3,2) (3,3) 

Each row prints one more pair than the previous one: as soon as col grows larger than row, the inner loop breaks, but the outer loop simply moves on to the next row.

To leave several loops at once, you can set a flag variable that the outer loop checks, move the loops into a function and return, or — sparingly — use goto to a label after the loops.

Summary

Statement In a loop it… In a switch it…
break exits the innermost loop exits the switch
continue jumps to the next pass of the innermost loop not allowed (applies to an enclosing loop)

Tip: Use break and continue to make loops simpler. If you need several of them in one loop, consider rewriting the condition instead.

Test yourself

What does continue do inside a for loop?

  1. Exits the loop
  2. Skips to the update and the next condition check
  3. Restarts the loop from the initialisation
  4. Pauses the program

What does for (int i = 0; i < 10; i++) { if (i == 4) break; printf("%d", i); } print?

  1. 0123
  2. 01234
  3. 0123456789
  4. 4

In nested loops, which loop does break exit?

  1. All loops
  2. The outermost loop
  3. The innermost loop containing it
  4. None, it only works in switch