ZetCode

C break

last modified November 14, 2026

The break statement ends the nearest enclosing loop or switch statement and resumes execution after it. It is one of the four jump statements of C, together with continue, goto and return; a jump statement is a statement that transfers control to another place in the program instead of falling through to the next statement. This tutorial shows how break behaves in each kind of loop and in switch, what happens in nested loops, and why the compiler rejects a break that no loop or switch encloses.

The break statement

break may appear in the body of a while, do-while or for loop, and in the body of a switch statement. It ends the innermost such statement, skips whatever remains in its body, and continues with the first statement after the closing brace. The typical place for it is inside an if, so that the loop stops only when a condition becomes true.

while (condition) {

    ...

    if (finished) {
        break;
    }

    ...
}

When finished becomes true, control leaves the while statement at once. Any statements between the break and the closing brace of the loop are not executed, and the loop condition is not tested again. The same keyword works in a for loop, where the increment is skipped as well when the loop is left this way.

break in a while loop

The first example searches for the first multiple of seven above 50. The loop counts upward from 50 and stops as soon as the remainder of the division by seven is zero.

break_while.c
#include <stdio.h>

int main(void) {

    int n = 50;

    while (n < 100) {

        n++;

        if (n % 7 == 0) {
            break;
        }
    }

    printf("First multiple of 7 above 50: %d\n", n);

    return 0;
}

The condition n < 100 alone cannot express when the search is over, because that depends on the value of n % 7. The break adds the second exit: when it runs, control jumps to the printf call and the value of n is already 56.

$ gcc -std=c17 -Wall -Wextra -o break_while break_while.c
$ ./break_while
First multiple of 7 above 50: 56

break in a for loop

A for loop is often used to search an array. The next program walks through the elements until it finds 16, records the index, and leaves the loop with break. The index starts at -1 so that the program can tell a missing value from a match at position zero.

break_for.c
#include <stdio.h>

int main(void) {

    int vals[] = { 4, 8, 15, 16, 23, 42 };
    int length = (int) (sizeof vals / sizeof vals[0]);
    int target = 16;
    int index = -1;

    for (int i = 0; i < length; i++) {

        if (vals[i] == target) {
            index = i;
            break;
        }
    }

    if (index >= 0) {
        printf("Found %d at index %d\n", target, index);
    } else {
        printf("%d not found\n", target);
    }

    return 0;
}

Without break, the loop would keep testing the remaining elements after the match and would overwrite index if the value appeared more than once. With it, the loop ends at the first match, and the increment i++ is not performed for that last iteration.

$ gcc -std=c17 -Wall -Wextra -o break_for break_for.c
$ ./break_for
Found 16 at index 3

break in a do-while loop

A do-while loop tests its condition after the body, so the body runs at least once. break inside it ends the loop immediately, before the condition is evaluated. The example divides 100 by two until the number becomes odd.

break_do.c
#include <stdio.h>

int main(void) {

    int n = 100;
    int steps = 0;

    do {

        if (n % 2 != 0) {
            break;
        }

        n /= 2;
        steps++;

    } while (n > 1);

    printf("%d after %d divisions\n", n, steps);

    return 0;
}

The check for an odd value is at the top of the body, so the division is skipped for the final odd number and steps counts only the successful halvings. The loop reaches 25 after two divisions and stops there.

$ gcc -std=c17 -Wall -Wextra -o break_do break_do.c
$ ./break_do
25 after 2 divisions

break in a switch statement

In a switch, break ends the switch and not the surrounding program or loop. The example asks for a single character and prints one of three messages.

break_switch.c
#include <stdio.h>

int main(void) {

    printf("Continue? y/n: ");

    char c;

    if (scanf(" %c", &c) != 1) {
        return 1;
    }

    switch (c) {

        case 'y':
            printf("continuing\n");
            break;

        case 'n':
            printf("stopping\n");
            break;

        default:
            printf("unknown option\n");
    }

    return 0;
}

Each case ends with break, so exactly one message is printed. If the first break were removed, a 'y' would print both "continuing" and "stopping", because execution would fall through into the next label. The default branch needs no break: it is the last label, and the switch ends anyway.

$ gcc -std=c17 -Wall -Wextra -o break_switch break_switch.c
$ printf 'y\n' | ./break_switch
Continue? y/n: continuing

break in nested loops

break always belongs to the innermost loop or switch that contains it. In a nested loop it ends the inner loop only, and the outer loop continues with its next iteration. The next program prints a small multiplication table, but stops each row as soon as the product exceeds six.

break_nested.c
#include <stdio.h>

int main(void) {

    for (int i = 1; i <= 3; i++) {

        for (int j = 1; j <= 5; j++) {

            if (i * j > 6) {
                break;
            }

            printf("%d x %d = %d\n", i, j, i * j);
        }

        printf("row %d done\n", i);
    }

    return 0;
}

The inner loop stops at a different column in every row, yet "row 1 done", "row 2 done" and "row 3 done" are all printed. That shows the outer loop is unaffected: only the loop that directly contains the break is terminated.

$ gcc -std=c17 -Wall -Wextra -o break_nested break_nested.c
$ ./break_nested
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
row 1 done
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
row 2 done
3 x 1 = 3
3 x 2 = 6
row 3 done

Leaving both loops

When a match should end both loops, the search result can be stored in a variable and tested in the condition of the outer loop. The next program looks for a value in a two-dimensional array and records its coordinates.

break_flag.c
#include <stdio.h>

int main(void) {

    int grid[3][3] = {
        { 1, 2, 3 },
        { 4, 5, 6 },
        { 7, 8, 9 }
    };

    int target = 6;
    int row = -1;
    int col = -1;

    for (int i = 0; i < 3 && row == -1; i++) {

        for (int j = 0; j < 3; j++) {

            if (grid[i][j] == target) {
                row = i;
                col = j;
                break;
            }
        }
    }

    printf("%d at row %d, column %d\n", target, row, col);

    return 0;
}

The inner break sets the result and leaves the column loop. The outer loop then tests row == -1, sees that the value was found, and ends before starting another row. The coordinates are already stored, so no extra jump is needed.

$ gcc -std=c17 -Wall -Wextra -o break_flag break_flag.c
$ ./break_flag
6 at row 1, column 2

break outside a loop or switch

A break that is not enclosed by a loop or a switch is not valid C, and the compiler reports an error instead of a warning. The following program places one directly in main.

break_outside.c
#include <stdio.h>

int main(void) {

    printf("before\n");
    break;
    printf("after\n");

    return 0;
}

GCC rejects the file while parsing main. The same error appears if the break is inside an if, because an if is not a loop: the keywords that break can leave are while, do-while, for and switch. A function called from inside a loop cannot break the caller's loop either; it can only return to it.

$ gcc -std=c17 -Wall -Wextra -o break_outside break_outside.c
break_outside.c: In function ‘main’:
break_outside.c:6:5: error: break statement not within loop or switch
    6 |     break;
      |     ^~~~~

Guidelines

The clearest loop is one that stops because its condition became false. A while (1) with a break somewhere in the body hides the exit condition in the middle of the code, so prefer writing the condition in the header when it is short. For a search loop the condition usually cannot express the exit, and then break is the right tool.

Keep the number of break statements in one loop small, ideally one. When several conditions can end the loop, combine them into a flag or into the loop condition. For deep nesting, a goto to a single cleanup label is the conventional C alternative, because it states explicitly which loops are being left; a chain of flags is harder to follow.

Source

cppreference: break statement
cppreference: statements
GCC: warning options

This tutorial covered the break statement in C: how it ends while, do-while, for and switch blocks, how it affects only the innermost nested loop, and why it is a compile error outside them. For the statement that skips one iteration instead of the whole loop, see the C continue tutorial; the loops themselves are described in C for and C while, and the label-based alternative in C switch.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all C tutorials.