Monday 19 June 2017

java(break statement)

                                  Break statement   


  


Java break statement is used to break loop and switch statement. It breaks the current flow of program at specified condition.

In case of inner loop, its breaks only inner loop.

Syntax:
              jump-statement;    
                      break;   







                    Java break statement with loop

Example:

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


 Output:1
            2
            3
            4

              Java break statement with inner loop

It breaks only inner loop, if you apply the break statement inside the inner loop

Example:

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

Output:1 1
             1 2
             1 3
             2 1
             3 1
             3 2
             3 3








No comments:

Post a Comment