Break Statement in java

Java break statement has three uses :

1) break statement is used in switch to terminate (exit)case statement sequence .
2) break statement is used to exit a loop at specified condition.
3) break statement is used as ' civilized ' form of goto . That means used to terminate a block of code.
Let us see all three uses one by one :-


1) Break statement is used in switch

break statement is used in switch to terminate (exit) case statement sequence .

//example of break statement in switch
class TestingSwitch
{
public static void main(String[] args) {
 int n=3;
 switch(n)
 {
    case 0:
    System.out.println("Zero");
    break;

    case 1:
    System.out.println("One");
    break;

    case 2:
    System.out.println("Two");
    break;

    case 3:
    System.out.println("Three");
    break;

    default :
    System.out.println("Default case"); 
 }   
}
}
Three

  Note : if we don't use break in switch then there will be a fall-through.

2) Using break to Exit a Loop

break statement is used to exit a loop at specified condition. When a break statement is encountered inside a loop, the loop is terminated and program control resumes at the next statement following the loop. Here is a simple example:


//using break for exit for loop
class Example {
    public static void main(String arr[]) {
        for (int i = 1; i <= 10; i++)
        {
            if (i == 5) 
            {
                break;//terminate the loop if i==5
            }
            System.out.println("Value of i =" + i);
        }
        System.out.println("Loop Completed...");
    }
}
Value of i =1
Value of i =2
Value of i =3
Value of i =4
Loop Completed...

Basically break statement is used to terminate the infinite loops. for example


//using break for exit infinite loop
class Example {
    public static void main(String arr[]) { 
            int i=1;              
            //creating infinite  while loop
            while(true)
            {
                if(i==10)
                {
                break;//terminating loop if i==10
                }
                System.out.println("i = "+i);
                i++; //incrementing i

            }       
        System.out.println("Loop Completed...");
    }
}
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9
Loop Completed...

3) Using break as a Form of Goto

The break statement can also be used as ' civilized ' form of the goto statement. By using this form of break, we can break out of one or more blocks of code. These blocks need not be part of a loop or a switch. They can be any block.
We can specify where execution will resume , because this form of break works with a label.
The general form of the labeled break statement is shown here:

 break label ; 
Let us see the example :


//using break for exit labeled block
class Example {
    public static void main(String arr[]) { 
            boolean f=true;
             first : {
                second :{
                    third :{

                         System.out.println("Third Block");
                         if(f)
                         {
                            break second;//exiting second block also
                         }
                    }
                     System.out.println("Second Block");
                }
                 System.out.println("First Block");
             }

        System.out.println("Completed...");
    }
}
Third Block
First Block
Completed...
One of the most common uses for a labeled break statement is to exit from nested loops
//example of terminating outer loop
class Example {
    public static void main(String arr[]) { 
           Outer : for (int i=1;i<=10 ;i++ ) {
                Inner:  for (int j=1;j<=10 ;j++ ) {
                    if(j==5){
                        break Outer;
                    }
                    System.out.println(i+ " , "+j);
                    }
                    
                }             
        System.out.println("Loop Completed...");
    }
}
1 , 1
1 , 2
1 , 3
1 , 4
Loop Completed...