The C++ break Statement

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 simple ways to escape a loop. It is best to use the break statement in more complex loops, where it can be difficult to tell whether the loop should be terminated before several statements have been executed.

For an example of using the break statement within the body of a switch statement, see The switch Statement.