Table of Contents
Previous Section Next Section

break

break is used to exit from a do, for, or while loop, bypassing the normal loop condition. It is also used to exit from a switch statement.

An example of break in a loop is shown here:

do {
  x = getx();
  if(x < 0) break; // terminate if negative 
  process(x);
} while(!done);

Here, if x is negative, the loop is terminated.

In a switch statement, break keeps program execution from “falling through” to the next case. (Refer to the switch statement for details.)

A break terminates only the for, do, while, or switch that contains it. It will not break out of any nested loops or switch statements.


Table of Contents
Previous Section Next Section