Easy Tutorial
❮ Java String Split Java Print Parallelogram ❯

Java Example - Break Keyword Usage

Java Examples

The Java break statement can directly forcefully exit the current loop, ignoring any other statements and loop condition tests within the loop body.

The following example uses the break keyword to exit the current loop:

Main.java File

public class Main {
    public static void main(String[] args) {
        int[] intary = { 99,12,22,34,45,67,5678,8990 };
        int no = 5678;
        int i = 0;
        boolean found = false;
        for ( ; i < intary.length; i++) {
            if (intary[i] == no) {
                found = true;
                break;
            }
        }
        if (found) {
            System.out.println(no + " element index position is: " + i);
        } 
        else {
            System.out.println(no + " element is not in the array");
        }
    }
}

The output of the above code is:

5678 element index position is: 6

Java Examples

❮ Java String Split Java Print Parallelogram ❯