FREE E LEARNING PLATFORM
HOMEEXCEPTIONSOOPSJVMINTRO
 

Break statement in Java with example








The break statement is usually used in following two scenarios:

a) Use break statement to come out of the loop instantly. Whenever a break statement is encountered inside a loop, the control directly comes out of loop and the loop gets terminated for rest of the iterations. It is used along with if statement, whenever used inside loop so that the loop gets terminated for a particular condition.

The important point to note here is that when a break statement is used inside a nested loop, then only the inner loop gets terminated.

b) It is also used in switch case control. Generally all cases in switch case are followed by a break statement so that whenever the program control jumps to a case, it doesn't execute subsequent cases (see the example below). As soon as a break is encountered in switch-case block, the control comes out of the switch-case body.

Syntax of break statement:

"break" word followed by semi colon

break;

Example - Use of break in a while loop

In the example below, we have a while loop running from o to 100 but since we have a break statement that only occurs when the loop value reaches 2, the loop gets terminated and the control gets passed to the next statement in program after the loop body.

public class BreakExample1 {
   public static void main(String args[]){
      int num =0;
      while(num<=100)
      {
          System.out.println("Value of variable is: "+num);
          if (num==2)
          {
             break;
          }
          num++;
      }
      System.out.println("Out of while-loop");
  }
}

Output

Value of variable is: 0
Value of variable is: 1
Value of variable is: 2
Out of while-loop

Example - Use of break in a for loop

The same thing you can see here. As soon as the var value hits 99, the for loop gets terminated.

public class BreakExample2 {

   public static void main(String args[]){
	int var;
	for (var =100; var>=10; var --)
	{
	    System.out.println("var: "+var);
	    if (var==99)
	    {
	         break;
	    }
	 }
	 System.out.println("Out of for-loop");
   }
}

Output:

var: 100
var: 99
Out of for-loop

Example - Use of break statement in switch-case

public class BreakExample3 {

   public static void main(String args[]){
	int num=2;
	      
	switch (num)
	{
	    case 1:
	       System.out.println("Case 1 ");
	       break;
	    case 2:
	       System.out.println("Case 2 ");
	       break;
	    case 3:
	       System.out.println("Case 3 ");
	       break;
	    default:
	       System.out.println("Default ");
	}
   }
}

Output

Case 2

In this example, we have break statement after each Case block, this is because if we don't have it then the subsequent case block would also execute. The output of the same program without break would be Case 2 Case 3 Default.

You may also Find this interesting

JAVA Static Keyword

JAVA Inheritance

JAVA Types of Inheritance

JAVA Aggregation

JAVA Association







Leave Comment