Break and Continue Statements in Java

Break Statement is a loop control statement that is used to terminate the loop.The continue statement skips the current iteration of a loop(in the loop), if a specified condition occurs, and continues with the next iteration in the loop. In this tutorial, we will learn about the Break and Continue statement in Java with the help of examples.


Java has some jump statements, which shift the program flow to another part of the program. Normally, after the body of a loop has been entered, all the statements in the body are executed before doing the next loop test. The break and continue statements are used to skip part of a loop or even terminate it before the predetermined condition, depending on tests made on the body of the loop. It can be also be used to handle errors or some exceptional conditions.

1. Break Statement in Java:

When we use the break statement, the program control resumes at the next statement following the loop. If the break statement is inside nested loops, it brings the control out of the loop containing it.

java
break;

Flow chart for Break Statement

break statement



Example for Break Statement

java
// Java Program to

import java.io.*;
public class pattern {
  public static void main(String[] args) {
    for (int i = 0; i < 20; i++) {
      if (i == 8) {
        break;
      }
      System.out.println("Number is : " + i);
    }
  }
}

Output :

Number is : 0 Number is : 1 Number is : 2 Number is : 3 Number is : 4 Number is : 5 Number is : 6 Number is : 7

In the above example, the loop is terminated when the value of i is 8. Otherwise, Its value is further processed in the loop.



2. Continue Statement in Java:

Continue statement is a branching statement. It is used to continue the loop. When encountered, it causes the rest of the iteration to be skipped and the next iteration starts. The continue statement is almost always used in decision-making statements. We can use Java continue statement in all types of loops such as for loop, while loop, and do-while loop.

Java Continue Statement Syntax :

java
continue;

Continue Statement



Example for Continue Statement in Java:

java
// Java Program to demonstrate the continue statement in for loop.
import java.io.*;
public class pattern {
  public static void main(String[] args) {
    int i;
    for (i = 0; i <= 5; i++) {
      if (i == 3) {
        continue;
      }
      System.out.println(i);
    }
  }
}

Output :

0 1 2 4 5

The value 4 is missing in the output, because when the value of variable i is 3, then the program executes a continue statement, which makes it jump at the beginning of for loop for the next iteration, skipping the statements for the current iteration.




Example for Continue Statement with Inner Loop:

java
import java.util.Scanner;
public class pattern {
  public static void main(String[] args) {
    int i = 1, j = 1;
    while (i <= 5) // outer while loop
    {
      System.out.println("Outer Loop: " + i);

      while (j <= 5) //inner while loop
      {
        if (j == 3) {
          j++;
          continue;
        }

        System.out.println("Inner Loop: " + j);
        j++;
      }
      i++;
    }
  }
}

Output :

Outer_Loop: 1 Inner_Loop: 1 Inner_Loop: 2 Inner_Loop: 4 Inner_Loop: 5 Outer_Loop: 2 Outer_Loop: 3 Outer_Loop: 4 Outer_Loop: 5

  • Here, the First Outer Loop condition will be checked, if " i " is greater than or equal to 5 then the loop will terminate and come out of the loop.
  • If the value of " i " is less than equal to 5 then control goes inside the loop and print the " i " value after that check for the condition in 2nd while loop if it is true then goes inside the loop and execute statements otherwise come out of the loop and continue until 1st while loop becomes false.
  • In the 2nd while loop we used the "if" condition inside that we used the continue statement. In case of an inner loop, it continues the inner loop only and then outer loop. So when the value of "j == 3" then the value of "j" is increased and the continue statement is executed and skips the iteration of the inner loop. Hence, the text Inner_Loop: 3 is skipped from the output.

3. Labeled Continue Statement:

  • This is another form of continue statement in Java known as labeled continue.
  • It includes the label of the loop along with the continue keyword.
  • The continue statement skips the current iteration of the loop specified by the label.

Syntax :

java
continue label;



Example for Labeled Continue Statement:

java
// Java program to perform Labeled continue Statement.

import java.util.Scanner;
public class pattern {
  public static void main(String[] args) {
    hashcode: for (int i = 1; i <= 4; i++) {
      // inner loop
      for (int j = 1; j <= 3; j++) {
        if (i == 2 && j == 2) {
          //using continue statement with label
          continue hashcode;
        }
        System.out.println("i = " + i + " j = " + j);
      }
    }

  }
}

Output :

i = 1 j = 1 i = 1 j = 2 i = 1 j = 3 i = 2 j = 1 i = 3 j = 1 i = 3 j = 2 i = 3 j = 3 i = 4 j = 1 i = 4 j = 2 i = 4 j = 3