break statement

From cppreference.com
< c‎ | language

Causes the enclosing for, while or do-while loop or switch statement to terminate.

Used when it is otherwise awkward to terminate the loop using the condition expression and conditional statements.

Contents

[edit] Syntax

break ;

[edit] Explanation

After this statement the control is transferred to the statement immediately following the enclosing loop. All automatic storage objects declared in enclosing loop are destroyed before the execution of the first line following the enclosing loop.

[edit] Keywords

break

[edit] Example

#include <stdio.h>
 
int main()
{
    int i = 2;
    switch (i) {
        case 1: printf("1");
        case 2: printf("2");   /* execution starts at this case label */
        case 3: printf("3");
        case 4:
        case 5: printf("45");
                break;         /* execution of subsequent cases is terminated */
        case 6: printf("6");
    }
 
    printf("\n");
 
    /* Compare outputs from these two double for loops. */
    int j,k;
    for (j = 0; j < 2; j++) {
        for (k = 0; k < 5; k++) {
            printf("%d%d ", j,k);
        }
    }
    printf("\n");
    for (j = 0; j < 2; j++) {
        for (k = 0; k < 5; k++) {
            if (k == 2) break;
            printf("%d%d ", j,k);
        }
    }
}

Output:

2345
00 01 02 03 04 10 11 12 13 14 
00 01 10 11