Java break and continue statements

In Java, the break and continue statements are control flow statements and allow us to interrupt (break) the current flow or skip (continue) an iteration within a loop.

Java break Statement

The Java break statement is used to break the loop or switch statement.

Example of a break statement inside a loop:

class Test {
    
  public static void main(String[] args) {
    for (int i = 0; i < 10; i++) {
      System.out.println("Value:" + i);
      if (i == 3) {
        break; // terminate the loop if condition is met
      }
    }
        
    System.out.println("Code after the loop...");
  }
}
Output: Value:0 Value:1 Value:2 Value:3 Code after the loop…
 
In the above example, if the value equals 3, the break statement will be executed, and the loop will be terminated. See the output.

Java break statement and nested loop

The break statement is often used inside nested loops as well. 

To work correctly with break and nested loops, we must first label the loops to specify from which loop we want to break.

Take a look at this example:

class Test {
    
  public static void main(String[] args) {
    first:// label
      for (int i = 1; i < 3; i++) {
        second:// label
        for (int j = 1; j < 4; j++) {
          System.out.println("i: " + i + ", j:" + j);
          if (j == 3) {
            break first;
          }
        }
      }
        
      System.out.println("Code after the loop...");
  }
}
Output: i: 1, j:1 i: 1, j:2 i: 1, j:3 Code after the loop…
 
Here we label the loops with “first”, and “second” and then inside the nested loop, we set that if j is equal to 3, to exit the loop with the label first, which means that the whole flow is interrupted and the execution of code outside the loop continues.

Java break statement inside the switch statement

The break statement is widely used in switch statements. This ensures that code execution stops after the matching case’s code block has been executed.

Here is one example:

class Test {
 
  public static void main(String[] args) {
    int number = 10;
    int newNumber;
 
    switch (number) {
      case 5: // if the number is equal to 5, execute this block of code
        newNumber = number;
        break;
      case 10: // if the number is equal 10, execute this block of code
        newNumber = number;
        break;
      default: // None of the above is true? Then run the default block
        newNumber = 0;
    }
       
    // outside the switch statement
    System.out.println(newNumber);
  }
}
Output: 10
 
If the break statement is not used, all the cases after the matching case get executed:
 
class Test {
 
  public static void main(String[] args) {
    String color = "yellow"; 
 
    switch (color) {
      case "white":
        System.out.println("The color is white.");
      case "yellow":
        System.out.println("The color is yellow.");
      case "blue":
        System.out.println("The color is blue.");
      case "green":
        System.out.println("The color is green.");
      default:
        System.out.println("Unrecognized color!");
    }
  }
}
Output: The color is yellow. The color is blue. The color is green. Unrecognized color!

Java continue statement

The continue statement skips the current iteration of a loop. 

Example of the continue statement inside a loop:

class Test {
    
  public static void main(String[] args) {
    for (int i = 1; i < 5; i++) {
      if (i == 3) {
        continue;
      }
        System.out.println("i: " + i);
    }
        
    System.out.println("Code after the loop...");
  }
}
Output: i: 1 i: 2 i: 4 Code after the loop…
 
Here, when the value of i becomes equal to number 3, the continue statement is executed, and the program moves to the end of the loop, and the next iteration starts. Therefore, the printing of the value when it was equal to 3 was skipped.

Java continue statement and nested loop

The continue statement can be used inside nested loops as well. As in the case of break statements, we need to label loops to control the flow more properly.

class Test {
    
  public static void main(String[] args) {
    first: // label
    for (int i = 1; i < 3; i++) {
      second: // label
      for (int j = 1; j < 4; j++) {
        if (j == 3) {
          continue second;
        }
        System.out.println("i: " + i + ", j:" + j);
      }
    }
        
    System.out.println("Code after the loop...");
  }
}
Output: i: 1, j:1 i: 1, j:2 i: 2, j:1 i: 2, j:2 Code after the loop…
 
Here we skipped the execution of the println() method when the value of j was equal to 3, which can be seen in the output.
 
That was all about the break and continue statements in Java. Proceed to the next lesson.

Happy Learning!

Leave a Reply

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