Break & Continue

break

  • Totally exit from loop(for,while,do~while) or from switch.

Example…

publc class example{
   public static void main(String[] args){
      int count = 1;
      while(true){
         if(count>3){ 
            break;
         }

         System.out.println(Integer.toString(count)+"th String printed lol");
         count++;
      }
   }
}

Output…

1th String Printed lol
2th String Printed lol
3th String Printed lol

continue

  • exit only current turn from loop(for,while,do~while), move onto next step in the loop.
publc class example{
   public static void main(String[] args){
      int count = 1;
      while(true){
         if(count%2==0){ 
            continue;
         }

         System.out.println(Integer.toString(count)+"th String printed lol");
         count++;
      }
   }
}

Output…

1th String Printed lol
3th String Printed lol
5th String Printed lol
...

Leave A Comment

Your email address will not be published. Required fields are marked *