Name | Description | Notes | Source | Availability | |||||||
---|---|---|---|---|---|---|---|---|---|---|---|
break |
Exit from loop or switch statement |
L | Keyword | C89 | C90 | C95 | C99 | C11 |
The keyword break
forms a break statement, which follows the syntax
of jump-statement:
- jump-statement
break ;
It's used to exit unconditionally from loops:
for (int i = 0; i < 10; i++) { if (i == 5) break; printf("Line: %d\n", i); }
Line: 0 Line: 1 Line: 2 Line: 3 Line: 4
…and to exit from a switch statement, so that execution of the selected statements doesn't fall through to the next label:
int i = 3; switch (i) { default: printf("Foo\n"); break; case 2: printf("Bar\n"); break; case 4: printf("Baz\n"); break; }
Foo
When a break statement occurs inside nested loops and
switch statements, it applies to the innermost one. Future
versions of C might support the syntax break
identifier
;
, allowing a jump out of any enclosing construct, not
just the innermost one.