AppDividend
Latest Code Tutorials

Java Continue Statement Example | Continue Statement Tutorial

0

Java Continue Statement Example | Continue Statement Tutorial is today’s topic. The continue statement can be used in any of the loop structures. It causes a loop to jump to the next iteration of the loop immediately. In the for loop, the continue keyword causes control to jump to the update statement quickly. In the while loop or do/while loop, control immediately jumps to the Boolean expression.

Java Continue Statement

You have already seen a break statement used in the earlier chapter of this tutorial. It was used to “jump out” of the switch statement.

The break statement can also be used to jump out of the loop.

Continue statement is mostly used inside loops. Whenever it is encountered inside the loop, control directly jumps to the beginning of the loop for the next iteration, skipping the execution of statements inside the loop’s body for the current iteration.

See the following syntax.

continue;

#Use of the continue statement inside the for loop

See the following code example.

public class Conditionals {
  public static void main(String[] args) {
    for (int j = 17; j <= 21; j++) {
      if (j == 19) {
        continue;
      }
      System.out.print(j + " ");
    }
  }
}

See the following output.

➜  java javac Conditionals.java
➜  java java Conditionals
17 18 20 21                        
➜  java

#Use of the continue in While loop

See the following programming example.

public class Conditionals {
  public static void main(String[] args) {
    int counter = 21;
    while (counter >= 17) {
      if (counter == 19) {
        counter--;
        continue;
      }
      System.out.print(counter + " ");
      counter--;
    }
  }
}

See the following output.

➜  java javac Conditionals.java
➜  java java Conditionals
21 20 18 17                                                                     
➜  java

In the above code, we are iterating the while loop from 21 and count one negative.

When the counter goes to 19, then we need to skip and then again it will print the count until the while condition is failed.

That is why in the above output, only 19 is skipped, and it will print the other numbers.

#Java Continue Statement with Labeled For Loop

We can use continue statement with a label. This feature is introduced since JDK 1.5. So, we can continue any loop in Java now whether it is outer loop or inner. See the following code example.

public class Conditionals {
  public static void main(String[] args) {
    kl: for (int i = 18; i <= 21; i++) {
      kb: for (int j = 18; j <= 21; j++) {
        if (i == 20 && j == 20) {
          continue kl;
        }
        System.out.println(i + " " + j);
      }
    }
  }
}

See the following output.

➜  java javac Conditionals.java
➜  java java Conditionals
18 18
18 19
18 20
18 21
19 18
19 19
19 20
19 21
20 18
20 19
21 18
21 19
21 20
21 21
➜  java

#Labeled continue Statement

The continue statement we have discussed until now is an unlabeled form of continue, which skips execution of remaining statement(s) of innermost for, while, and do-while loop.

There is another form of the continue statement, labeled form, that can be used to skip an execution of statement(s) that lies inside an outer loop.

Finally, Java Continue Statement Example | Continue Statement Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.