C Break Statement

The break statement is used to immediately exit a loop (for, while, do-while) or a switch statement, even if the loop’s condition has not been fully satisfied.

Syntax:


break;

1. It exits the innermost loop or switch in which it is used.

2. After the break statement is executed, the program control moves to the next statement after the loop or switch block.


#include <stdio.h>

int main() {
    for (int i = 1; i <= 5; i++) {
        if (i == 4) {
            break;  // Exit the loop when i is 4
        }
        printf("%d\n", i);
    }
    
    printf("Loop terminated.\n");
    
    return 0;
}

Output:

1
2
3
Loop terminated.

Explanation:

  1. The loop prints numbers 1, 2 and 3.
  2. When i becomes 4, the break statement exits the loop, and the program continues with the statement after the loop.