Java Continue Statement | Label | in While, For Loop, Outer Loop Examples

Java Continue Statement is used when in a loop needed a jump to the beginning of the loop for the next iteration(loop cycle). It’s skipping execution of statements(code) inside the loop’s body after “Continue Keyword” for the current iteration.

Java Continue Statement or Label in While, For Loop, Outer Loop Examples

This tutorial you will learn about Continue Label with an example of many type control statements like While Loop, For Loop if condition, etc.

Syntax

Here is Java Continue syntax.

Flow Diagram of Continue Statement

Flow diagram Java Continue Statement withExamples

Java continue example (for-each Loop)

Using a for-each loop and then checking the condition with an if statement. If in the Loop given if the condition is true then skip the current iteration.

Output: 0
1
3

How to Continue statement works:

Here is an explanation in the diagram of how an above continue program has worked.

How to java Continue statement works

Use Java continue in while loop

The above example was in the for-each loop (for each loop is enhanced version of for loop), now see the example of how it’s work with a while loop in java. Again if condition statement required in this example.

Continue Lable with For Loop

The first example is in for each loop, now look at the example in for loop.

Give Extra time to look at an example, this time we are checking condition in if condition with an Array index.

Output: 0
1
3

Continue in a do-While loop

Output: 5 6 7 9

QA: How to use Java continue in the outer loop?

A Java Continue label can be used in the nested loop program, Where it can skips the current execution of the inner loop and current iteration of the outer loop too.
Inner Loop example of Java Continue Statement, in program Will Skip print 2 2.

Output: 1 1
1 2
2 1

QA: What is the “continue” keyword and how does it work in Java?

A Continue label or Continue Keyword or Continue statement Or Continue Word in Java is the same, See the above example definition with how works.
Java Continue Statement with Labeled For Loop example – Using a continue label can continue any loop in Java.

Output: Parent Loop 1
child Loop 1
Parent Loop 2
child Loop 1

Sidenotes: It’s useful when you required to skip particular conditions in a loop like for example you want to skip an exception divide by zero. So that time you can define a Java continue Statement and program will skip work on this code.

Java continues in if a statement is used so many times in this tutorial, it’s required to get the particular condition.

Do You have any other example on this topic do comment below?

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1

Java version 11

All Examples Java Continue Statement with loops and control statement are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.


Leave a Reply

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