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 continue statement is generally used with the if statement.
This is demonstrated in the examples coming in the next part of this tutorial.
Syntax of using continue Java statement
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 values 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:
public class loop_demos { public static void main(String []args) { int intx; for (intx=10; intx<100; intx=intx+10){ //as value - 30 or 50, the loop should omit if( intx == 30 || intx == 50) { continue; } System.out.println("intx = " + intx); } } }
Output:
intx = 20
intx = 40
intx = 60
intx = 70
intx = 80
intx = 90
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 the for loop while the print statement was 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:
public class loop_demos { public static void main(String []args) { int int_leap; for (int_leap=1960; int_leap<=2020; int_leap++){ //Formula for leap years if( int_leap % 4 != 0) { continue; } System.out.println("The leap Year: " + int_leap); } } }
You see, the loop 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.
f it was, the current value of the variable int_leap displayed. Otherwise, it skipped by using the continue statement.
How continue statement work 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:
public class loop_demos { public static void main(String []args) { int intx; String[] arrstr = new String[3]; arrstr[0] = "a"; arrstr[1] = "b"; arrstr[2] = "c"; for (String currElement : arrstr) { //Starting inner array for(intx=1;intx<=3;intx++){ //Should skip 2 if(intx==2){ continue; } System.out.println(intx); } System.out.println("The String array: " + currElement); } } }
Output:
3
The String array: a
1
3
The String array: b
1
3
The String array: c
- 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 the intx variable is also displayed, however, the continue statement is used and it skipped the value of intx = 2.
- The inner loop was 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 that the continue statement only affected the inner loop, where it was used. The outer loop displayed all string elements.
An example 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 to learn how it works:
public class loop_demos { public static void main(String []args) { int inty; inty=0; while (inty <= 50) { if(inty==30){ inty=inty+10; continue; } System.out.println(inty); inty=inty+10; } } }
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:
public class loop_demos { public static void main(String []args) { int intz; intz=1; do { if(intz==4 ||intz==6){ intz++; continue; } System.out.println("intz = " + intz); intz++; } while (intz<=9); } }
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.