Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

break — exit a loop or switch

The break statement exits the innermost enclosing loop or switch. It does not exit the function or every nested loop.

Version: C23; repository compiler flag, production runtime not attested

Syntax

break;

break; immediately terminates the innermost enclosing for, while, do, or switch statement. Execution continues after that statement. The semicolon is required.

break must occur in or as a loop or switch body. An if alone does not qualify. Nesting matters: break in a switch inside a loop exits the switch, not the loop. In switch, it prevents execution from continuing into subsequent case branches.

Unlike continue, which proceeds to loop continuation, break exits the loop. It does not replace return, which exits the function.

In the example, i == 4 triggers exit before sum += i. Only 1, 2, 3 are added, producing sum = 6.

Examples

#include <stdio.h>

int main(void)
{
    int sum = 0;

    for (int i = 1; i <= 5; ++i) {
        if (i == 4) {
            break;
        }
        sum += i;
    }

    printf("sum = %d\n", sum);
    return 0;
}

Result

sum = 6

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources