break statement in java

Let’s learn break statement in java.

break statement in java

If break statement is encountered in a loop, that loop will terminate and the control will shift to next statement of the same loop.

Syntax:

break;

break statement is used to terminate case in switch case statement in java.

Java break statement is also used in all types of loops such as while loop, do-while loop and for loop.

Let’s see an example on break statement.

public class BreakStatementExample
{
   public static void main(String[] args) 
   {
      for(int a = 1; a <= 10; a++)
      {
         if(a == 3)
         {  
            // breaking loop  
            break;  
         }  
         System.out.println(a);  
      }
   }
}


Output:

1
2