Break and Continue

Break Statement

The break statement is used to exit a loop or switch statement. It stops the loop immediately, and the program’s control moves outside the loop.

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

Continue Statement

The continue statement skips the current iteration of a loop. It will bypass the statement of the loop.

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