Java – Break Statement

download sourcecode here

The Java break is used to break loop or switch statement. It breaks the current flow of the program at specified condition. In case of inner loop, it breaks only inner loop.

Syntax:

  1. jump-statement;
  2. break;

java break statement flowchart

Java Break Statement with Loop

Example:

public class BreakExample {
    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            if (i == 5) {
                break;
            }
            System.out.println(i);
        }
    }
}

Output:

1
2
3
4

 

Leave a Reply

Your email address will not be published. Required fields are marked *