Break Statement in Java

What is Break Statement in java?

Break statement in Java is used to move to program control out of the loop when the break condition is met. The break condition is specified using an if statement and when its met, the break statement is executed and the loop containing it terminates its execution, and program control resumes at the next statement following the loop. If used inside an inner loop of a nested loop, then the inner loops breaks. It can be used with all the loops; for loop, while loop, and do-while loop, and with a Switch statement.

 

 

Break Statement in Java with Example

//Java Program with break statement using while loop

//importing Scanner class for input
import java.util.Scanner;

public class BreakWhile {
    public static void main(String[] args) {
        System.out.println("Enter your favorite food:");

        //instantiating Scanner object "sc"
        Scanner sc=new Scanner(System.in);

        String food=sc.nextLine();

        //breaks when user enters pasta
        while(true){
            if(food.equals("pasta")){
                System.out.println("Mine is too!");
                break;
            }
            System.out.println("Enter Again!");
            food=sc.nextLine();
        }

        //closing the "sc" object
        sc.close();

    }
}

In the program provided above, Scanner class is imported from the java.util package which is used for taking user input. The program prompts the user to enter his favorite food; if it is “pasta”, then the loop execution terminates, otherwise loop continues to iterate until the user enters “pasta”.

Output:

Enter your favorite food:
pizza
Enter Again!
pasta
Mine is too!

 

 

Break with Nested Loop

//Java Program with break statement using nested loop
public class BreakNested {
    public static void main(String[] args) {

        //Doesn't print "@" for fifth row when 3rd column is reached
        for(int i=1;i<=5;i++){ //rows
            for(int j=1;j<=i;j++){ //columns
                if(i==5 &&j==3){
                    break;
                }
                System.out.print("@");
            }
            System.out.println();
        }
    }
}

In the program provided above, a nested loop is used using for loop for both inner and outer loop. The break statement is used inside the inner loop. So, when the break condition is met, the inner loop breaks for the current iteration of the outer loop and program control is transferred to the next iteration of the outer loop. Here, inner loop breaks when fifth row and third column is encountered. The purpose of this program is to print a pyramid of “@” symbols.

Output:

@
@@  
@@@ 
@@@@
@@

 

 

Break with For Loop

//Java Program with break statement using for loop
public class BreakFor {
    public static void main(String[] args) {

        //prints until a number divisible by 5 is encountered
        for(int i=1;i<=10;i++){
            if(i%5==0){
                System.out.println("Number divisible by 5 encountered! Break!!");
                break;
            }
            System.out.println(i);
        }
    }
}

In the program provided above, a for loop is used to print numbers from 1 to 10 until a number divisible by 5 is obtained. This condition is the break condition and if met, breaks the loop and transfers the program control to statement immediately following the loop.

Output:

1
2
3
4
Number divisible by 5 encountered! Break!!

 

 

Break with Do While Loop

//Java Program with break statement using do while loop
public class BreakDoWhile {
    public static void main(String[] args) {

        //loop variable
        int i=1;

        //prints until a number divisible by 5 is encountered
        do{
            if(i%5==0){
                System.out.println("Number divisible by 5 encountered! Break!!");
                break;
            }
            System.out.println(i);
            i++;
        }while(i<=10);
    }
}

In the program provided above, a do-while loop is used to print numbers from 1 to 10 until a number divisible by 5 is obtained. This condition is the break condition and if met, breaks the loop and transfers the program control to statement immediately following the loop.

Output:

1
2
3
4
Number divisible by 5 encountered! Break!!

 

Check out our JAVA Tutorials.

Share this post

Leave a Reply

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