The break statement is used to exit an iteration or switch statement. It transfers control to the statement immediately following the iteration substatement or switch statement.
The break statement terminates only the most tightly enclosing loop or switch statement. In loops, break is used to terminate before the termination criteria evaluate to 0. In the switch statement, break is used to terminate sections of code—normally before a case label. The following example illustrates the use of the break statement in a for loop:
for( ; ; ) // No termination condition.
{
if( List->AtEnd() )
break;
List->Next();
}
cout << "Control transfers to here.\n";
Note :
There are other ways to escape a loop as simply as the one above. In more complex loops, where it can be difficult to tell if the loop should be terminated before several statements have been executed, using the break statement makes more sense.
For an example of using the break statement within the body of a switch statement, see “The switch Statement”.