Java Break and Continue

Java break and continue statements are jump statements that ignore some piece of code and jump to the next piece of code. These keywords are used inside loops like while, do while loops and switch statements.

Java Break

Java break statement is used in switch statements. The break keyword breaks the loop and jumps out of the switch statement. Java break statement can also be used to terminate the loops. In a loop, when the compiler reaches to the break statement, the loop iteration stops there. It does not matter how many iterations are left. It stops the compiler to go through all the conditions after desired conditioned is satisfied.

The syntax of using break statement is:

break condition is used in the following scenarios:

  • To break a loop
  • To end the sequence in switch statement

Break statement in loop

The below example shows how to use break statement in a loop:

// File name is Example.java
public class Example {
  public static void main(String[] args) {
    for (int i = 1; i < 8; i++) {
      if (i == 5) {
        break;
      }
      System.out.println(i);
    }  
  }
}

The output of the above code after compilation and execution is:

Here is another example, that shows how to use break statement in a while loop.

// File name is Example.java
public class Example {
  public static void main(String[] args) {
    int x = 1;
    while (x < 10) {
      System.out.println(x);
      x++;
      if (x == 5) {
        break;
      }
    }  
  }
}

The output of the above code after compilation and execution is:

Break statement in switch

The below example shows how to use break in switch statement. Here break statement is used in each case. When the case value matches, the switch statements breaks there and exits the block of code.

// File name is Example.java
public class Example {

   public static void main(String args[]) {
      char grade = 'C';

      switch(grade) {
         case 'A' :
            System.out.println("Marks above 90"); 
            break;
         case 'B' :
         case 'C' :
            System.out.println("Marks above 80");
            break;
         case 'D' :
            System.out.println("Marks above 60");
         case 'F' :
            System.out.println("Marks below 50");
            break;
         default :
            System.out.println("Invalid grade");
      }
      System.out.println("Your grade is " + grade);
   }
}

The output of the above code after compilation and execution is:

Java Continue

Java Continue statement breaks the current iteration of loop and jumps to next when the specified condition is met. The continue statement is also used in loops like for, while, do while loops. The syntax of using continue statement is:

Continue statement in loop

The example demonstrates the working of continue statement in a loop:

// File name is Example.java
public class Example {

   public static void main(String args[]) {
      int [] array = {10, 20, 30, 40, 50};

      for(int i : array ) {
         if( i == 30 ) {
            continue;
         }
         System.out.println( i );
      }
   }
   }

In the above code, when compiler satisfies the condition ( i = 30 ), it skips the next statements and moves to the next iteration. The output of the above code after compilation and execution is:

After encountering continue statement in a while loop, compiler jumps to the Boolean expression. Here is another example that shows how to use continue statement in while loop:

// File name is Example.java
public class Example {
  public static void main(String[] args) {
    int x = 1;
    while (x < 10) {
      if (x == 4) {
        x++;
        continue;
      }
      System.out.println(x);
      x++;
    }  
  }
}

The output of the above code after compilation and execution is:

Difference between Java Break and Continue statements

The major differences between java break and continue statements are given in the table below:

Java BreakJava Continue
It uses break keyword that indicates break in java program.It uses continue statement to indicate continuation in java program.
It breaks and terminates the loop.It skips the current loop iteration.
break statement is used inside a switch statement.continue statement is not used inside switch statement.
It terminates the loop execution.It does not terminate the loop execution.
It does not let the compiler to verify all loop conditions. The compiler verifies all loop conditions.
Java Break and Continue