Break Statement

 Please see below code for Break Statement – It is used for terminating the loop in between.


package practice;

public class BreakStatment {

    public static void main(String[] args) {

        for (int i = 0; i < 5; i++) {

            if (i == 3) {
                break;
            }

            System.out.println(i);
        }

        System.out.println("After break");
    }

}


Explanation –  

  • for (int i = 0; i < 5; i++): Loop starts from i = 0 to i < 5

  • Inside the loop:

    • If i == 3, the break statement is executed, and the loop stops immediately.

    • Otherwise, it prints the value of i.

Output – 

0
1
2
After break