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.

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

In simple words, it is used to continue the loop. The general syntax of continue statement in Java is as follows:

Syntax:
   continue;

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.

This statement can be used 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 is 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 is executed first and then the test condition is evaluated.


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 source code :

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 it is decremented 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 be executed and subsequent statements will not be executed.

Therefore, the value of i from 10 to 6 will not be displayed 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 source code 1: Use of continue statement inside 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 source code 2: Use of continue statement inside 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

Program source code 3: Use of continue 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 source code 4: Use of continue statement in 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

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 ⇒