Java Continue


JavaViews 2022

We use Java continue statement inside the loops like for, while, and do-while which we have learned in the previous tutorials. When the control points to the continue statement, it skips the execution of the remaining code within the loop and continues with the next iteration. We use the Java continue statement mainly when we want to skip a particular code for certain iteration.

Continue Syntax

The syntax is as below where continue is the keyword and followed by a semicolon(;).

continue;

Flowchart of continue

Java continue

 

Java Continue in for loop

In the below example, we use java continue in for loop to print only odd numbers between 0 and 10. Within for loop, we have an if condition which checks if the remainder of the division of numbers is 0 which means if the number is an even number, then it will execute the continue statement. When it executes the continue statement, then it skips all the code within the for loop of that particular iteration and then continues with the next iteration.

public class ContinueForLoop {

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

  }

}
i: 1
i: 3
i: 5
i: 7
i: 9

Let’s understand this in detail. When i=0, the condition returns true and executes the java continue statement and does not print the value within the i:0 for loop and directly executes the next iteration.

Now, i=1 and the condition returns false, and hence it prints the i value as 1. When i=2, again condition returns a true value and executes the continue statement and skips the next line within the for loop. Hence it does not print the value i:2.

In this way, it completes the execution of for loop and finally prints all odd numbers.

Java continue

Java continue in while loop

Below is an example of java continue in while loop to print numbers between 1 and 10 based on a condition. When i values are greater than 5 and less than 10, it does not print the i values and instead executes the continue statement and continues with the next iteration of the while loop. This is why values i:6, i:7, i:8 and i:9 is not displayed in the output.

public class ContinueWhileLoop {

  public static void main(String[] args) {
    int i = 0;
    while(i<=10) {
      if((i>5) && (i<10)) {
        i++;
        continue;
      }
      System.out.println("i: " + i);
      i++;
    }

  }

}
i: 0
i: 1
i: 2
i: 3
i: 4
i: 5
i: 10

continue in while loop

Java continue in do-while loop

In the below example, we have used java continue in the do-while loop. It executes the continue statement when i=2 or i=4. Hence, it does not display these 2 values in the output. When it executes the continue statement, it skips the remaining statements of the current iteration and continues with the next iteration.

public class ContinueDoWhile {

  public static void main(String[] args) {
    int i=0;
    do {
      if((i==2) || (i==4)) {
        i++;
        continue;
      }
      System.out.println("i: " + i);
      i++;
    }while(i<=10);

  }

}
i: 0
i: 1
i: 3
i: 5
i: 6
i: 7
i: 8
i: 9
i: 10

java continue in do-while loop

Java continue in for-each loop

In the below example, we have used java continue in the for-each loop. It executes the continue statement if the string name contains “C”. In this case, it skips the remaining statements in the loop and continues with the next iteration. Hence the values “C” and “C++” are not displayed in the output.

public class ContinueForEach {

  public static void main(String[] args) {
    String[] lang = {"Java","C","C++","PHP","VBScript","Javascript"};
    for(String names: lang) {
      
      if(names.contains("C")) {
        continue;
      }
      System.out.println(names);
    }

  }

}
Java
PHP
VBScript
Javascript

java continue in for-each loop

Java continue in Labeled for loop

When we use java continue in the labeled for loop, it skips the execution of the inner loop for the current iteration and continues with the execution of the next iteration of the outer loop. This is because here we mention the label of the outer loop along with the keyword continue to show that it continues the execution of the outer loop. (Eg: continue loop1). In the below example, when i=2 and j=2, it executes the continue statement and hence skips the remaining statements in the inner loop. Hence the values i:2 j:2 and i:2 j:3 are not displayed in the output. The control goes to the outer loop and continues the execution with the next value which is i=1.

public class ContinueLabeledFor {

  public static void main(String[] args) {
      loop1:
      for(int i=3;i>=0;i--) {
        loop2:
        for(int j=1;j<=3;j++) {
          
          if(i==2 && j ==2)
            continue loop1;
          System.out.println("i:" + i + " " + "j:" + j);
        }
      }

  }

}
i:3 j:1
i:3 j:2
i:3 j:3
i:2 j:1
i:1 j:1
i:1 j:2
i:1 j:3
i:0 j:1
i:0 j:2
i:0 j:3

Java continue in nested for loop

When we use java continue in the nested for loop especially in the inner loop, it skips the execution of the current iteration of the inner loop and continues with the next iteration of the inner loop. In the below example, it executes continue statement when i=1 and j=2, this is why the value i:1 and j:2 are not displayed in the output. But it proceeds with the iteration of i=1 and j=3 and so on.

public class ContinueNestedFor {

  public static void main(String[] args) {
    for(int i=1;i<3;i++) {
      
      for(int j=1;j<=3;j++) {
        if(i==1 && j==2) {
          continue;
        }
        System.out.println("i:" + i + " " + "j:" + j);
      }
    }

  }

}
i:1 j:1
i:1 j:3
i:2 j:1
i:2 j:2
i:2 j:3

Reference

Translate ยป