You are currently viewing Java Break Statement with Example

Java Break Statement with Example

In this tutorial, we will learn about the break statement with an example.

What is break statement?

A break statement is a loop control statement that is used to end a loop. When the break statement is encountered within a loop, the loop iterations stop, and control returns from the loop to the first statement after the loop.

  • The break statement in Java immediately terminates the loop, and control of the program moves to the statement following the loop.
  • It is almost always used with decision-making statements (Java if- else Statement).

The break statement in Java has the following syntax:

Syntax:

break;

  • Break statements are generally used when we are unsure of the number of iterations for a loop or when we want to terminate the loop based on some condition.

Example: Break Statements

//Java Program to show the use of break statement.

public class Sample {  
public static void main(String[] args) {  
    //using for loop  
    for(int i=1;i<=10;i++){  
        if(i==4){  
            //break statement 
            break;  
        }  
        System.out.println(i);  
    }  
}  
}  

Output:

1
2
3

Example:  Break Statements

class Sample {
    public static void main(String[] args) {
      
        int n, sum = 0;

        // Object of Scanner--
        Scanner input = new Scanner(System.in);
      
        while (true) {
            System.out.print("Enter number: ");

            // takes input from user--
            n = input.nextInt();
        
            if (n < 0) {
                break;
            }
         
           sum += n;
        }
        System.out.println("Sum is = " + sum);
    }
}

Output:

Enter a number : 1
Enter a number: 2
Enter a number: 3
Enter a number: 4
Enter a number: -2
Sum = 10

The while loop  test expression is always true in the preceding program. Take note of the line here.

if (n < 0) {
break;
}

This means that the while loop is terminated when the user enters negative numbers.

Example: Break Statementswith Inner Loop

//Java Program to show the use of break statement    
public class Sample {  
public static void main(String[] args) {  
            for(int i=1;i<=3;i++){    
                    //inner loop  
                    for(int j=1;j<=3;j++){    
                        if(i==2&&j==2){    
                            //break statement inside inner loop  
                            break;    
                        }    
                        System.out.println(i+" "+j);    
                    }    
            }    
}  
}  

output:

1 1
1 2
1 3
2 1
3 1
3 2
3 3

Example: Break Statementswith Labeled For Loop

We can use the break Statementswith a label. Since JDK 1.5, the feature has been available. As a result, we can now break any loop in Java, whether it is an outer or inner loop. 

//Java Program to show the use of continue statement.
public class Sample {  
public static void main(String[] args) {  
            x:  
            for(int i=1;i<=3;i++){    
                    y:  
                    for(int j=1;j<=3;j++){    
                        if(i==2&&j==2){    
                            //using break statement with label  
                            break x;    
                        }    
                        System.out.println(i+" "+j);    
                    }    
            }    
}  
}  

Output:

1 1
1 2
1 3
2 1

Example: Break Statements in while loop

//Java Program to show the use of break statement  
public class Sample{  
public static void main(String[] args) {  
    //while loop with break statements
    int i=1;  
    while(i<=10){  
        if(i==6){  
            i++;  
            break;// break the loop  
        }
        System.out.println(i);  
        i++;  
    }  
}  
}  

Output:

1
2
3
4
5

Example: Break Statementsin do-while loop

//Java Program to demonstrate the use of break statement  
//inside the Java do-while loop.  
public class BreakDoWhileExample {  
public static void main(String[] args) {  
    //declaring variable  
    int i=1;  
    //do-while loop  
    do{  
        if(i==5){  
           //using break statement  
           i++;  
           break;//it will break the loop  
        }  
        System.out.println(i);  
        i++;  
    }while(i<=10);  
}  
}  

Output:

1
2
3
4

Example: Break statementsin switch case

If you don’t know what is switch case than please refer to Java switch Statement with Example | Java Tutorial

// using the switch...case statement

class Main {
  public static void main(String[] args) {

    int value = 35;
    String size;

    // switch statement to check size
    switch (value) {

      case 5:
        size = "Small";
        break;

      case 15:
        size = "Medium";
        break;

      case 25:
        size = "Large";
        break;

      case 35:
        size = "More than Large";
        break;
      
      default:
        size = "Unknown";
        break;

    }
    System.out.println("Size is: " + size);
  }
}

output:

Size is : More than Large

We hope that this article will assist you in understanding all about Java Break Statement. We have concentrated on making a basic, meaningful, and easy-to-learn guide to the concepts. Still, if you have any problems regarding this, please post them in the comment section, we will be glad to assist you.

This Post Has 2 Comments

Leave a Reply