break Statement

The break statement, when executed in a while, for, do...while or switch, causes immediate exit from that statement. Execution continues with the first statement after the control statement. Common uses of the break statement are to escape early from a loop or to skip the remainder of a switch (as in Fig. 5.9). Figure 5.13 demonstrates a break statement exiting a for.

 1   // Fig. 5.13: BreakTest.java 2   // break statement exiting a for statement. 3   public class BreakTest 4   { 5      public static void main(String[] args) 6      { 7         int count; // control variable also used after loop terminates 8  9         for (count = 1; count <= 10; count++) // loop 10 times10         {11             ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.