Description |
 |
A break statement terminates the execution of the most
tightly enclosing “switch ” statement or “for ”, “while ”
, “do...while ”
loop.
Control passes to the statement following the switch or iteration statement. You cannot use a break statement unless it is enclosed in a switch or loop statement. Further, a break only exits out of one level of switch or loop statement. To exit from more than one
level, you must use a goto statement.
When used in the switch statement, break normally terminates each case statement. If you do not use break (or other unconditional transfer of control),
each statement labeled with case flows into the next. Although not required, a break is usually placed at the end of the last case statement. This reduces the possibility of errors
when inserting additional cases at a later time.
Example |
 |
The following example uses break to exit from the for loop after executing the loop three times:
for (i=0; i<=6; i++)
if(i==3) break;
else printf ("%d\n",i);
This example prints:
0
1
2