Java Break statement with examples

Statements in the Java programming language are used in the break following two ways −
  • When a break statement is encountered inside the loop, the loop terminates immediately and program control resumes at the next statement after the loop body.
  • It can be used switch to terminate a statement case (described in the next chapter).

Syntax:

The syntax is a single statement inside a loop −

break;

break statement flowchart

Java break statement

 

Example 1. Simple Java break statement example

public class Test {

   public static void main(String args[]) {
      int [] numbers = {10, 20, 30, 40, 50};

      for(int x : numbers ) {
         if( x == 30 ) {
            break;
         }
         System.out.print( x );
         System.out.print("\n");
      }
   }
}

Execute the above sample code and get the following result −

10
20

Example 2. Break statement example

In this example, demonstrate how to use break statements in java's for for loop, while loop and loop, do-while 




public class JavaBreak {

    public static void main(String[] args) {
        String[] arr = { "S", "I", "I", "K", "A", "I" };

        
        for (int len = 0; len < arr.length; len++) {
            if (arr[len].equals("I")) {
                System.out.println("Array contains 'I' at index: " + len);
               
                break;
            }
        }

        
        int len = 0;
        while (len < arr.length) {
            if (arr[len].equals("K")) {
                System.out.println("Array contains 'K' at index: " + len);
                
                break;
            }
            len++;
        }

        len = 0;

        do {
            if (arr[len].equals("A")) {
                System.out.println("Array contains 'A' at index: " + len);
                
                break;
            }
            len++;
        } while (len < arr.length);
    }

}
 

Execute the above sample code and get the following results:

Array contains 'I' at index: 1
Array contains 'B' at index: 3
Array contains 'A' at index: 4

Note that if we remove the break statement, there will be no difference in the output of the program. For the small iterations in this example, there are no performance issues. But if the iterator count is large, then it can save a lot of processing time.

 

Example 3. Java break labeled
break statement Labeling is used to terminate the outer loop, the loop should be labeled for it to work. Here is an example demonstrating the usage of java break label statements.



public class JavaBreakLabel {

    public static void main(String[] args) {
        int[][] arr = { { 1, 2 }, { 3, 4 }, { 9, 10 }, { 11, 12 } };
        boolean found = false;
        int row = 0;
        int col = 0;
     
        searchint:

        for (row = 0; row < arr.length; row++) {
            for (col = 0; col < arr[row].length; col++) {
                if (arr[row][col] > 10) {
                    found = true;
                    
                    break searchint;
                }
            }
        }
        if (found)
            System.out.println("First int greater than 10 is found at index: [" + row + "," + col + "]");
    }

}

Execute the above sample code and get the following results:

First int greater than 10 is found at index: [3,0]

You can also use the break statement in the switch statement block, which will be explained in detail in the next chapter.