next up previous
Next: Exercise: a class to Up: Unit 06 Previous: The continue statement (optional)

Statements to exit from blocks and labels for statements (optional)

Normally, a break or a continue statement causes the exit from a single level of nesting of switch or loop statements in which it appears. However, such statements allow also for exiting from more than one level of nesting of a switch or loop.

To do so, the statements that define a block can have a label:

label: loop-statement;

A label must be a constant integer expression (analogous to those used in the ) cases of a switch statement).

The statement

break label;
interrupts the loop that has the label specified in the break statement. If there is no loop with the specified label that surrounds the break label statement, then a compile-time error is signaled.

Note: The use of labels and of break and continue statements that refer to labels is considered a bad programming habit, and has to be used only in very particular cases. In this course we will not make use of it.


next up previous
Next: Exercise: a class to Up: Unit 06 Previous: The continue statement (optional)