You are currently viewing Continue Statement in Java | Java Tutorial

Continue Statement in Java | Java Tutorial

In the previous tutorial, we have learned about the Break statement. In this, we will learn about continue statement with an example.

When you need to jump to the next iteration of the loop right away, you utilize the continue statement in the loop control structure. It can be used in conjunction with a for loop or a while loop.

The loop is continued using the Java continue command. It continues the program’s current flow while skipping the remaining code at the specified circumstance. It only continues the inner loop in the case of an inner loop.

The continue statement in Java can be used in all forms of loops, including for loops, while loops, and do-while loops.  

Syntax:

continue;

When working with loops, you may want to skip some statements or end the loop. Break and continue statements are used in such cases.

Example: Java continue statement

class Main {
  public static void main(String[] args) {

    // for loop
    for (int i = 1; i <= 10; ++i) {

    // if I is between 4 and 9, continue is  execution
      if (i > 5 && i < 8) {
        continue;
      }
      System.out.println(i);
    }
  }
}

Output:

1
2
3
4
5
8
9
10

  • The for loop is used in the preceding program to print the value of I after each iteration. Visit Java for loop to learn more about how for loop works.
  • if( I > 5 && I < 8) then continue.
  • When the value of I becomes greater than 5 but less than 8, the continue statement is executed.
  • The print statement is then skipped for those values. As a result, the output skips values 6 and 7.

Example: Continue statement.

public class Test {

   public static void main(String args[]) {
      int [] numbers = {11, 22, 33, 44, 55};

      for(int value : numbers ) {
         if( x == 22 ) {
            continue;
         }
         System.out.print(value);
         System.out.print("\n");
      }
   }
}

Output:

11
33
44
55

Example: continue statement in While loop

ublic class Main {

   public static void main(String args[]){
	int number=10;
	while (number >=0)
	{
           if (number==7)
           {
	       number--;
	       continue;
           }
           System.out.print(number+" ");
           number--;
	}
   }
}

Output:

10 9 8 6 5 4 3 2 1 0

Example: Continue Statement in do-while Loop

//Java Program to show the use of continue statement  
public class Sample {  
public static void main(String[] args) {  
    int i=1;  
    do{  
        if(i==5){  
                 i++;  
                //using continue statement  
            continue;
        }  
        System.out.println(i);  
        i++;  
    }while(i<=10);  
}  
}  

Output:

1
2
3
4
6
7
8
9
10

Example: Continue Statementswith Labelled For Loop

//Java Program to show the use of continue statement  
public class Sample {  
public static void main(String[] args) {  
            x:  
            for(int i=1;i<=3;i++){    
                    y:  
                    for(int j=1;j<=3;j++){    
                        if(i==2&&j==2){    
                            //using continue statement 
                            continue x;    
                        }    
                        System.out.println(i+" "+j);    
                    }    
            }    
}  
}  

Output:

1 1
1 2
1 3
2 1
3 1
3 2
3 3

Summary:

Within loops, the continue statements are commonly used. Control jumps to the beginning of the loop for the next iteration when it is encountered within a loop, skipping the execution of statements within the loop’s body for the current iteration. This is particularly useful when you want to continue the loop but don’t want the remaining statements in the loop body ( after the continue statements) to run for that iteration.

We hope that this article will assist you in understanding all about continue statement. We have concentrated on making a basic, meaningful, and easy-to-learn guide to the concepts. Still, if you have any problems regarding this, please post them in the comment section, we will be glad to assist you.

Leave a Reply