What is the role of Java continue statement?

The continue statement is used to skip the current iteration in the for and while loops in Java. In the case of Java for loop, the execution moves to the update counter as it meets the continue statement.

For the while loop, the execution moves to the condition / Boolean expression. In both cases, the statements next to the continue statement (within the loop) are ignored.

The scope of continue Java statement is the loop where it is used. For example, if you have an inner loop within the outer for loop and continue is used in the inner loop then the current iteration in the inner loop will be skipped.

The continue statement is generally used with the if statement.

This is demonstrated in the examples coming in the next part of this tutorial.

Note: If you require exiting the loop completely then use the break statement.

Syntax of using continue Java statement

continue;

An example of continue statement with for loop

In this simple example of showing how the continue statement works, an int type variable is created. The variable is used in a for loop with an initial value of 10. The loop increments the variable intx value by 10 in each iteration and displays the current value.

However, an if statement is used to check the value 30 and 50 for intx. As it is true, the continue statement will execute inside the if statement and see how it displays the result:

See online demo and code

contnue for loop

In the output, it can be seen that 30 and 50 are not displayed. The reason is, loop met the continue statement that resulted in passing the execution back to update part of for loop while print statement is skipped.

More real example – displaying only leap years

For this example, the Java continue statement is used for displaying the leap years from 1960 to 2020. See the program and a little description after this:

See online demo and code

Java continue

You see, the loop is started from 1960 and will go through 2020. In the if statement, the formula is used to establish if this was a leap year or not. If it was, the current value of the variable int_leap displayed. Otherwise, it skipped by using the continue statement.

How continue statement works with inner loops?

This example shows how to continue statement works if you have an inner for loop. See the example first and I will explain:

See online demo and code

continue inner loop

In the example, a Java string array with three elements is created. An outer for loop is used to display those array elements. Inside that loop, an inner loop is used to iterate through an int type variable from 1 to 3.

The current value of intx variable is also displayed, however, the continue statement is used and it skipped the value of intx = 2.

The inner loop executed three times (for each iteration of the outer loop) and every time it skipped the print statement as the value of intx = 2.

You might also notice, the continue statement only affected the inner loop, where it was used. The outer loop displayed all string elements.

A demo of using continue with while loop

Just like the for loop, the continue statement skips the current iteration and execution moves to the Boolean expression as using the while loop. See the example below for learning how it works:

The output:

0

10

20

40

50

Did you notice, the statement to increase the value of the variable is used twice; one after the if statement and the other after the System.out.println(inty).

Using continue in do while loop example

The final example of using the continue is using in a do while loop. Have a look in the code where two numbers are skipped by using the continue statement in do while:

The output of above code is:

intz = 1

intz = 2

intz = 3

intz = 5

intz = 7

intz = 8

intz = 9

The number 4 and 6 are skipped while iterating through the loop.