Continue Statement in Java | Use, Example

Continue statement in Java is another similar statement, like break statement that is used inside a loop to repeat the next iteration of the loop.

In other words, continue statement stops the current iteration of loop and begins a new iteration of loop.

When the continue statement encounters, subsequent statements in the loop skips (i.e. not executed) at a specified condition, and the control of execution continues with the next repetition of the loop.

Continue statement in Java do not break out of the loop entirely. It just jumps back to the beginning of the loop by skipping the rest of code in the loop body for the next iteration.

Look at the below figure to understand better.

Java continue statement example

Syntax of Continue Statement in Java


The general syntax of using the continue statement in Java is as follows:

Syntax:
   continue;

Here, continue is a keyword. We can also use continue statement with label to interrupt the loop. The syntax is as:

continue labelname;

Just like break statement, we write continue statement inside the statement block that the loop executes, preceded by a conditional test.

When to use Continue Statement in Java?


Java continue statement is another jump statement used in loop control structure that is used when we need to jump to the next iteration of the loop immediately.

We can use continue statement in all the types of loops like for loop, while loop, and do-while loop. In the case of inner loop, it continues with inner loop only.

The use of continue statement in loops has shown in the below figure.

Continue statement in Java From figure (a) and (b), in the case of while and do-while loops, continue statement causes the control of execution to go directly to the test condition and then to continue to the repetition process.

But in the case of for loop, the increment section of the loop executes first and then the test condition evaluates.


Let’s understand the use of continue statement in Java with the help of an example program.

Suppose we want to display numbers in descending order from 1 to 10. For this, let’s write a program using for loop. Look at the source code.

Program code 1:

package javaProgram;
public class ContinueUse {
public static void main(String[] args)
{
for(int i = 10;  i >= 1; i--) 
{
   System.out.println(i + " ");	
 }
 }
}
Output:
       10 9 8 7 6 5 4 3 2 1

In this program, the value of i begins with 10 and decrements by 1 until the value of i is greater or equal to 1. Now, we will add continue statement in this program like this:

package javaProgram;
public class ContinueUse {
public static void main(String[] args)
{
for(int i = 10;  i >= 1; i--)
{
   if(i > 5) 
    continue; // It will skip the rest statement and go back in the for loop.	 
    System.out.println(i + " ");
 }
 }}

In this coding, when i > 5, we are redirecting the flow of execution back to the next iteration of loop.

During the change of the value of i from 10 to 6, the continue statement will execute and subsequent statements will not execute.

Therefore, the value of i from 10 to 6 will not display and the output will be like this:

Output:
       5 4 3 2 1

Example Program based on Continue Statement


Let’s take some important example programs based on the continue statement in Java.

Program code 2: Use of continue statement within for loop

package javaProgram;
public class ContinueEx {
public static void main(String[] args)
{
for(int i = 10;  i >= 1; i--)
{
 if(i == 5) 
 continue; 
 System.out.println(i + " ");
 }
 }}
Output:
              10 9 8 7 6 4 3 2 1

As you can observe in the output, 5 is not displayed on the console because the loop is continued when it is equal to 5.


Program code 3: Use of continue statement within while loop

package javaProgram;
public class ContinueUse {
public static void main(String[] args)
{
// while loop.
 int x = 1; // Initialization.
 while(x <= 10) {
  if(x == 5) {
   x++;  
   continue;
  } 
  System.out.println(x);
  x++;
 }
 }}
Output:
             1 2 3 4 6 7 8 9 10

As you can see in the output, the number 5 is skipped.


Program code 4: Continue statement in do-while loop

package javaProgram;
public class ContinueUse {
public static void main(String[] args)
{
// do-while loop.
 int x = 1; // Declaration and Initialization of variable.
do{
  if(x == 5){
   x++;  
   continue;
  } 
 System.out.println(x);
  x++;
 } while(x <= 10);
 }
}
Output:
             1 2 3 4 6 7 8 9 10

Program code 5: Continue statement inside inner loop

package javaProgram;
public class ContinueUse {
public static void main(String[] args)
{
// Outer loop.	
for(int i = 1; i<= 3; i++)
{
// Inner loop. 
 for(int j = 1; j <= 3; j++)
 {
  if(i == 2 && j == 3)	
   continue; // continue statement inside inner loop.	
  System.out.println(i+ " " +j); 
  }
 }}}
Output:
             1 1
             1 2
             1 3
             2 1
             2 2
             3 1
             3 2
             3 3

More Examples on Continue Statement

Program code 6:

public class ContinueEx {
public static void main(String[] args) 
{
 int i;
 for(i = 0; i <= 6; i++)
 {
   if(i < 2) continue;
	 System.out.println(i+ " ");
   if(i < 4) continue;
	 System.out.println(10 * i+ " ");
  }
 }
}
Output:
      2 
      3 
      4 
      40 
      5 
      50 
      6 
      60

In this example program, we have created a loop that iterates the value of variable i from 0 to 6. If i is less than 2, we do nothing. Once i is 3 or higher, we print its value.

Once i is greater than 5, we print 10 * i. So, the above program will print the values as shown in the output.


Program code 7: Continue statement with label

public class ContinueEx {
public static void main(String[] args) 
{
 int i, j;
 outerloop:
 for(i = 0; i < 3; i++)
 {
   for(j = 0; j < 3; j++)
   {
     if((i == 2) && (j == 2))
     {
	continue outerloop; // skips when both expressions are true.
      } else {
	System.out.println(i+ ", " +j+ " ");
    }
 }
}}}
Output:
      0, 0 
      0, 1 
      0, 2 
      1, 0 
      1, 1 
      1, 2 
      2, 0 
      2, 1 

Hope that this tutorial has covered almost all the important points related to continue statement in Java and its uses with example programs. I hope that you will have understood this simple topic and have practiced programs.
Thanks for reading!!!
Next ⇒ Labelled loop in Java

⇐ Prev Next ⇒