break statement in java

It is one of the most important statements is the break statement in java and let’s see how to use the java break label. The break statement in java is very commonly used by the programmer to break the sequence of execution. The break statement is used in loops and switch statement.

The break is a keyword used in two scenarios:
1. break the loop in java
2. Switch statement

In java break for loop

If a break statement is encountered inside a loop, the loop is immediately terminated, and the control of the program goes to the next statement following the loop. It is used along with if statement in loops. If the condition is true, then the break statement terminates the loop immediately.

break;

We can use break statement in for, while, do-while loop.

Flowchart of break statement in java
class ExampleBreakStaementInForLoop 
{
    public static void main(String args[])
    {
        for(int i = 1 ; i <= 10 ; i++)
        {
            System.out.println(i);
            if(i == 5)
            break;
        }
	System.out.println(“After the for loop”);
    }
}

Output:
1
2
3
4
5
After the for loop

In the example, For loop is running from 0 to 10 but we placed a break statement that only occurs when the loop value reaches 5, the loop gets terminated and the control gets passed to the next statement in the program after the loop body and print the statement.

break statement in nested for loop

We can use break statements in the nested loop. When a break statement is used inside a nested loop, then only the inner loop gets terminated.

class ExampleOfBreak
{
    public static void main(String args[])
    {
        for(int i = 1 ; i <= 3 ; i++)
        {
           System.out.println("Before start of inner loop and value of i = "+ i);
           for(int j = 1; j <= 3 ; j++)
           {
         	System.out.println("Inside the for loop and value of j ="+ j);
        	if(j == 2)
            	break;
           }
        }
        System.out.println("After the for loop");
    }
}

Output:
Before start of inner loop and value of i = 1
Inside the for loop and value of j =1
Inside the for loop and value of j =2
Before start of inner loop and value of i = 2
Inside the for loop and value of j =1
Inside the for loop and value of j =2
Before start of inner loop and value of i = 3
Inside the for loop and value of j =1
Inside the for loop and value of j =2
After the for loop

Explanation: In the example, there are two for loops are running. We placed a break statement inside the inner for loop that only occurs when the inner loop value reaches 2, the inner loop gets terminated and the control gets passed to the outer loop.

break statement with Labeled in for loop

By use of break statement with a label, you can terminate any loop whether it is the outer loop or inner. This feature is introduced since JDK 1.5

public class BreakWithLabledExample3 
{  
   public static void main(String[] args) 
   {  
      outer:  
      for(int i = 1 ; i <= 3 ; i++)
      {    
    	System.out.println("Before start of the inner loop");  
    	inner:
        for(int j = 1; j <= 3; j++)
        {    
           System.out.println("inside the inner loop and Value of j = "+j);
           if( j ==2)
           {    
	           //using break statement with label  
	           break outer;    
           } 
        }    
      }    
   }  
}  

Output:
Before start of the inner loop
inside the inner loop and Value of j = 1
inside the inner loop and Value of j = 2

Break in a switch statement

To understand the example of a break with a switch statement, please visit here

1. Quiz, Read the below code and do answer.

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

Click on anyone to know the answer.

2. Quiz, Read the below code and do answer.

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

Click on anyone to know the answer.

3. Quiz, Read the below code and do answer.

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

Click on anyone to know the answer.

4. Quiz, Read the below code and do answer.

public class BreakStatementExample
{
   public static void main(String args[])
   {
      for(int i = 1; i <= 3; i++)
      {
    	  for(int j = 1; j <= 3; j++)
    	  {
    		  if(i == 2)
    			  break;
    		  System.out.println(i);
    	  }
      }
   }
}

Click on anyone to know the answer.

5. Quiz, Read the below code and do answer.

public class BreakStatementExample
{
   public static void main(String args[])
   {
      break;
   }
}

Click on anyone to know the answer.

Leave a Comment