The break Statement : Break Statement « Statement Control « Java Tutorial






  1. The break statement is used to break from an enclosing do, while, for, or switch statement.
  2. It is a compile error to use break anywhere else.
  3. 'break' breaks the loop without executing the rest of the statements in the block.

For example, consider the following code

public class MainClass {

  public static void main(String[] args) {
    int i = 0;
    while (true) {
        System.out.println(i);
        i++;
        if (i > 3) {
            break;
        }
    }
  }

}

The result is

0
1
2
3








4.8.Break Statement
4.8.1.The break Statement
4.8.2.Using the break Statement in a Loop: break out from a loop
4.8.3.Breaking Indefinite Loops
4.8.4.Labelled breaks breaks out of several levels of nested loops inside a pair of curly braces.
4.8.5.The Labeled break Statement