Statement Control Java Tutorial

The break statement is used to break from an enclosing do, while, for, or switch statement.
It is a compile error to use break anywhere else.
'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