Continue Statement in Java

 

Continue statement in Java is used to skip the portion of code that follows it, and continues with the next iteration. It is used to jump to the next iteration of the loop immediately. It can be used with all the loops; for loop, while loop, and do-while loop. In the case of a nested loop, a continue statement is applied to the loop in which it is written. It is applied along with a condition in the if statement. If the condition is fulfilled, then the continue statement is executed and the rest of the code for the current iteration is skipped or ignored. If not, the rest of the code is executed and execution then goes to the next iteration and so on.

 

 

Continue Statement in Java Example

//Java Program with continue statement
public class ContinueStatement {
    public static void main(String[] args) {
        //prints only those numbers from 1 to 10 that are divisible by 3
        for(int i=1;i<=10;i++){
            if(i%3!=0){
                System.out.println("This iteration is skipped.");
                continue;
            }
            System.out.println(i);
        }
    }
}

In the program provided above, a for loop is used to print numbers from 1 to 10 that are divisible by 3. If the numbers are not divisible by 3 which is checked using a condition in if statement, then the current iteration is skipped using a continue statement lest the number is printed to the terminal.

Output:

This iteration is skipped.
This iteration is skipped.
3
This iteration is skipped.
This iteration is skipped.
6
This iteration is skipped.
This iteration is skipped.
9
This iteration is skipped.

 

 

Continue with While Loop

//Java Program with continue statement using while loop
public class ContinueWhile {
    public static void main(String[] args){

        //loop variable
        int i=1;
        
        //prints only those numbers from 1 to 10 that are divisible by 3
        while(i<=10){
            if(i%3!=0){
                i++;
                System.out.println("This iteration is skipped.");
                continue;
            }
            System.out.println(i);
            i++;
        }
    }
}

In the program provided above, a while loop is used to print numbers from 1 to 10 that are divisible by 3. If the numbers are not divisible by 3 which is checked using a condition in if statement, then the current iteration is skipped using a continue statement lest the number is printed to the terminal.

Output:

This iteration is skipped.
This iteration is skipped.
3
This iteration is skipped.
This iteration is skipped.
6
This iteration is skipped.
This iteration is skipped.
9
This iteration is skipped.

 

 

Continue Statement with Nested Loop

//Java Program with continue statement using nested loop
public class ContinueNested {
    public static void main(String[] args) {

        //Prints @ in a pyramid pattern but not for third row
        for(int i=1;i<=5;i++){ //rows
            for(int j=1;j<=i;j++){ //columns
                if(i==3){
                    continue;
                }
                System.out.print("@");
            }
            System.out.println();
        }
    }
}

In the program provided above, a nested loop is used to create a pyramid pattern using “@” symbol. The inner loop has a condition which doesn’t allow the symbol to be printed for the third row. It skips the remaining code using continue statement for the third row and moves to next iteration of the outer loop.

Output:

@
@@

@@@@
@@@@@

 

 

Continue Statement with Do-While Loop

//Java Program with continue statement using do-while loop

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

        //loop variable
        int i=1;

        //prints only those numbers from 1 to 10 that are divisible by 3
        do{
            if(i%3!=0){
                i++;
                System.out.println("This iteration is skipped.");
                continue;
            }
            System.out.println(i);
            i++;
        }while(i<=10);
    }
}

In the program provided above, a do-while loop is used to print numbers from 1 to 10 that are divisible by 3. If the numbers are not divisible by 3 which is checked using a condition in if statement, then the current iteration is skipped using a continue statement lest the number is printed to the terminal.

Output:

This iteration is skipped.
This iteration is skipped.
3
This iteration is skipped.
This iteration is skipped.
6
This iteration is skipped.
This iteration is skipped.
9
This iteration is skipped.

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *