Website For Developers

Break and Continue in Java


Break keyword in Java


Syntax

break;


Example break keyword in Java

public class LoopBreak {

   public static void main(String[] args) {
      for (int count=0; count<5; count++) {
        if (count==4) {
            break;
         }
         System.out.println( "Count: "+count );
      }
   }
}

Output

Count: 0
Count: 1
Count: 2
Count: 3


Continue keyword in Java


Syntax

continue;


Example continue keyword in Java

public class LoopContinue {

   public static void main(String[] args) {
      for (int count=0; count<5; count++) {
        if (count==2) {
            continue;
         }
         System.out.println( "Count: "+count );
      }
   }
}

Output

Count: 0
Count: 1
Count: 3
Count: 4


Difference between "continue" and "break"