Java Jump statements - continue statement in Java Programming with examples

 In the previous tutorial we learnt about the most widely used Jump statement in Java - break statement. Following the same set, In this tutorial, we'll learn about the java continue statement

In some scenarios, we expect our program to stop or forcefully prevent the execution of code for a certain condition in a block of code. In such cases, java continue statement becomes very much helpful. Since the break statement breaks the execution, whereas continue skips the current iteration and continues the remaining.

Java continue statement 


Syntax: continue;

The continue statement can be used only in loops, like for loop, while and do-while. It is useful to force an early iteration of loop or continuing a loop. Whenever we use a continue in a looping structure, it skips all the statements after the continue and then moves the control to the next iteration. Most of the times, continue statement is used inside an if statement, so that it may only get executed when the specific condition approaches so as to prevent de-structuring of the code. Let's make it more clear through an example:

In this example, we have applied a condition that when i becomes equal to 5, then execute continue statement in the Java for loop

public class continueStatementExample1
 {

    public static void main(String[] args) {
         for(int i=1;i<=10;i++) {
             if(i==5) {
                 continue;
             }
             System.out.println("printing in "+i+"th iteration");
         }
    }

}

OUTPUT

printing in 1th iteration
printing in 2th iteration
printing in 3th iteration
printing in 4th iteration
printing in 6th iteration
printing in 7th iteration
printing in 8th iteration
printing in 9th iteration
printing in 10th iteration

* When i becomes equal to 5, continue statement executes and it skips all the other statements below it for the 5th iteration. Thus, shifting the control to the next iteration, by redirecting the control to the increment part of the for loop. Thats why, after showing the message of 4th iteration, it didn't printed 'printing in 5th iteration'.

continue with Java while loop

The following code uses continue within Java while loop. So as to continue the whole loop structure when the condition gets specified, i.e, i becomes equal to 5. 

public class continueStatementExample2
 {

    public static void main(String[] args) {
        int i=1;
         while(i<=10) {
             if(i==5) {
                 continue;
             }
             System.out.println("printing in "+i+"th iteration");
         }
    }

}

OUTPUT

printing in 1th iteration
printing in 2th iteration
printing in 3th iteration
printing in 4th iteration
printing in 6th iteration
printing in 7th iteration
printing in 8th iteration
printing in 9th iteration
printing in 10th iteration

As soon as the boolean condition in the if statement gets true, continue statement executes and continues the loop for the 5th iteration, meaning skips the execution of the statements during the 5th iteration, and runs for 6th iteration.

continue with Java do-while loop

public class continueStatementExample2
 {

    public static void main(String[] args) {
        int i=1;
         do {
             if(i==5) {
                 continue;
             }
             System.out.println("printing in "+i+"th iteration");
         }while(i<=10);
    }

}

OUTPUT

printing in 1th iteration
printing in 2th iteration
printing in 3th iteration
printing in 4th iteration
printing in 6th iteration
printing in 7th iteration
printing in 8th iteration
printing in 9th iteration
printing in 10th iteration

continue with Java nested loops

Like the break statement, when continue is used inside nested loop, only the inner loop is continued and no effect is seen on the outer loop. But it mostly depends upon where we use continue statement.

public class continueWithNestedLoops
 {

     public static void main(String[] args) {
            for(int i=1;i<=2;i++) {
                for(int j=1;j<=6;j++) {
                    if(j==3continue;
                    System.out.println("Pass "+i+" : "+"["+j+"]"+"th iteration");
                }
                System.out.println();
            }
        }

}

OUTPUT

Pass 1 : [1]th iteration
Pass 1 : [2]th iteration
Pass 1 : [4]th iteration
Pass 1 : [5]th iteration
Pass 1 : [6]th iteration

Pass 2 : [1]th iteration
Pass 2 : [2]th iteration
Pass 2 : [4]th iteration
Pass 2 : [5]th iteration
Pass 2 : [6]th iteration

Here, it continues the loop for the 3rd iteration of j in each ith iteration and printing all statements in other iterations.

continue with labelled loops 

As with the break, continue can also specify a label to describe which enclosing loop to continue. 

public class continueWithLabelledLoop
 {

    public static void main(String[] args) {
         outer: for (int i=1; i<=4; i++) {  
             inner : for(int j=1; j<=5; j++) {  
               if(j == i) {  
                 continue outer;  
               }  
               System.out.println(i+" : "+ j);  
             }  
           }  
           System.out.println();   
        }

}

OUTPUT

2 : 1
3 : 1
3 : 2
4 : 1
4 : 2
4 : 3

Here 2 labels are declared for each of the loops, the outer loop has label outer and inner loop has label inner. As soon as the values of i and j, becomes equal. The outer loop continues. thus preventing the printing of same i : j pairs like 1 : 1,  2 : 2 etc in the above example.

Ritish

Just a novice blogger

Post a Comment (0)
Previous Post Next Post