Continue In Java: Syntax, Flowchart And Examples With Loops

The continue statement is used to skip the current iteration of a loop in Java. It is similar to the break statement, but unlike break, it does not terminate the entire loop. Instead, it only skips the current iteration and proceeds with the next one.

When continue is encountered, it ends the current iteration and moves to the next iteration of the loop.

The continue statement can be used in all looping statements (while, do-while, for, and for-each) but cannot be used in switch statements.

While continue works with all loops, its behavior varies slightly between while/do-while loops and the for loop. We will explain this with the help of the flowchart below.

Syntax:

continue;

The continue keyword is used in loops to skip the current iteration.

Continue In While Loop Flowchart

When the continue keyword is encountered in a while loop, the current iteration is skipped, and the control moves to the loop’s test condition. The while loop continues until the condition becomes false.

while loop with keyword continue in java flowchart

Explanation:

Inside the while loop, if condition is checked.

  • If the condition is true, the continue keyword is encountered, and the control moves back to the while loop’s test condition, skipping the rest of the loop body.
  • If the condition is false, the remaining part of the loop is executed.

The while loop continues until the test condition becomes false.

Program 1: How to use continue in a while loop

public class ContinueInWhile {
    public static void main(String[] args) {
        int num = 1;
        while (num <= 5) {
            if (num == 3) {
                System.out.println("Continue encountered on value " + num);
                num++; // Increment first to avoid infinite loop
                continue; // Skip the current iteration when num equals 3
            }
            System.out.println(num);
            num++;
        }
    }
}

Explanation:

This program uses a while loop to print the numbers from 1 to 5, but it skips printing the number 3.

  • Variable num is initialized with value 1
  • The loop runs as long as num is less than or equal to 5
  • When num equals 3, it prints a message that says a continue has been encountered. It then increments num to avoid an infinite loop.
  • The continue statement is reached, causing the loop to skip the print statement for num when it is 3.
  • The program prints the values of num for 1, 2, 4, and 5, skipping 3.
1
2
Continue encountered on value 3
4
5

Program 2: How to use continue in a do-while loop

public class ContinueInDoWhile{
    public static void main(String[] args) {
        int val = 1;
        do {
            if (val == 4) {
                System.out.println("Continue encountered on value " + val);
                System.out.println("Skip the value " + val);
                val++; // Increment first to avoid infinite loop
                continue; // Skip the current iteration when val equals 4
            }
            System.out.println(val);
            val++;
        } while (val <= 5);
    }
}

Explanation:

This program uses a do-while loop to print the numbers from 1 to 5, but it skips printing the number 4.

  • Initialize the variable val with value 1
  • In a do-while loop, the condition is checked at the end of the loop body, meaning the loop body will execute at least once, whether the condition is true or false.
  • For the first time, the loop body executes even if the condition is false.
  • In the do-while loop body, we check the condition (val == 4), and if this condition is true, a message is printed on the console, and the value of the variable val is incremented.
  • When the continue keyword is encountered, the current iteration is skipped, and the control moves to the next iteration “without executing the remaining statements in the loop body”.
  • The loop will run until the variable val becomes greater than 5.
Note : we increment the the value of val variable before condition triggered otherwise do-while loop becomes infinite.
1
2
3
Continue encountered on value 4
Skip the value 4
5

Continue In For Loop Flowchart

In a for loop, the continue keyword works differently compared to while and do-while loops. When the continue keyword is encountered, the current iteration is skipped, and control moves to the loop’s update part (increment/decrement). The for loop continues until the condition becomes false.

Continue In For Loop Flowchart in java

Explanation:

Inside the for loop, if condition is checked.

  • If the condition is true, the continue keyword is encountered, and the control moves back to the for loop’s Increment/Decrement Expression, skipping the rest of the loop body.
  • If the condition is false, the remaining part of the loop is executed.

The for loop continues until the test condition becomes false.

Program 3: How to use continue in a for loop

public class ContinueInForLoop{
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            if (i == 3) {
                System.out.println("Continue encountered at value " + i);
                continue;  // Skips the current iteration when i is 3
            }
            System.out.println("Value of i: " + i);
        }
    }
}

Explanation:

  • The for loop iterates from 1 to 5.
  • In the loop, we use a condition to demonstrate how the continue statement works.
  • When i equals 3, the continue statement is encountered, which skips the rest of the loop body for that iteration. The control then moves to the update part (incrementing i) and then after checking the condition.
  • As a result, the value 3 is not printed, and the loop continues with the next value.
Value of i: 1
Value of i: 2
Continue encountered at value 3
Value of i: 4
Value of i: 5

Program 4: How to use continue in a forEach loop

public class ContinueInForEach{
    public static void main(String[] args) {
        String[] fruits = {"Apple", "Banana", "Orange", "Mango", "Avocado"};

        for (String fruit : fruits) {
            if (fruit.equals("Orange")) {
                System.out.println("Skipping " + fruit);
                continue;  // Skip the rest of the loop for "Orange"
            }
            System.out.println(fruit);
        }
    }
}

Explanation:

  • The foreach loop iterates over an array of fruits.
  • When the loop encounters "Orange", the continue statement is triggered, which skips the rest of the loop body for that iteration.
  • As a result, "Orange" is not printed, and the loop continues with the next fruit.
Apple
Banana
Skipping Orange
Mango
Avocado

Program 5: How to use continue in a Nested for loop

public class ContinueInNestedLoop {
    public static void main(String[] args) {
        for (int i = 1; i <= 3; i++) {
            System.out.println("Outer loop iteration: " + i);
            
            for (int j = 1; j <= 3; j++) {
                if (j == 2) {
                    System.out.println("Skipping inner loop when j = " + j);
                    System.out.println(); // New line after skip
                    continue;  // Skip the rest of the inner loop when j equals 2
                }
                System.out.println("  Inner loop iteration: i = " + i + ", j = " + j);
            }
        }
    }
}

Explanation:

This program skips the inner loop iteration based on a condition while continuing with the outer loop.

  • The outer loop (for i) runs from 1 to 3, representing the outer iterations.
  • The inner loop (for j) also runs from 1 to 3, representing the inner iterations for each value of i.
  • When j == 2, the continue statement is encountered, which skips the rest of the code in that inner loop iteration. Afterward, a blank line is printed.
  • Control moves to the update part of the inner loop (j++), and then the condition (j <= 3) is tested again.
  • The outer loop continues normally, and the inner loop resumes with the next value of j after the skip
Outer loop iteration: 1
  Inner loop iteration: i = 1, j = 1
Skipping inner loop when j = 2

  Inner loop iteration: i = 1, j = 3
Outer loop iteration: 2
  Inner loop iteration: i = 2, j = 1
Skipping inner loop when j = 2

  Inner loop iteration: i = 2, j = 3
Outer loop iteration: 3
  Inner loop iteration: i = 3, j = 1
Skipping inner loop when j = 2

  Inner loop iteration: i = 3, j = 3

Summary:

  • The outer loop runs three times (for i = 1, 2, 3), and the inner loop runs three times for each outer loop iteration (for j = 1, 2, 3).
  • When the inner loop reaches j = 2, the continue statement is executed, skipping the rest of that loop iteration and printing a blank line.
  • The rest of the iterations run normally, continuing with the next value of j after the skip.
Note : When the continue statement is encountered in an inner loop, only the current iteration of that inner loop(not outer loop) is skipped. However, the inner loop will continue to run its future iterations until its condition becomes false.

Labeled Continue

Labeled continue statements are used in nested loops. When we use a labeled continue inside an inner loop, it skips the current iteration of the labeled loop (usually the outer loop) and interrupts the execution of the inner loop. Control then moves to the next iteration of the labeled loop.

Labeled Continue Syntax

label_name:
for (...) {
    for (...) {
        if (condition) {
            continue label_name;
        }
    }
}
Labeled Continue in Java

Program 6: How to use labeled continue in a Nested for loop

public class LabeledContinue {
    public static void main(String[] args) {
        outerLoop: // Label for the outer loop
        for (int i = 1; i <= 3; i++) {
            System.out.println("Outer loop iteration: " + i);
            
            for (int j = 1; j <= 3; j++) {
                if (j == 2) {
                    System.out.println("Skipping inner loop when j = " + j);
                    System.out.println(); // New line after skip
                    continue outerLoop;  // Skip to the next iteration of the outer loop when j equals 2
                }
                System.out.println("  Inner loop iteration: i = " + i + ", j = " + j);
            }
        }
    }
}

Explanation:

  • The outer loop iterates from 1 to 3.
  • The inner loop also iterates from 1 to 3.
  • If the value of j is 2, the continue outerLoop statement is executed, which skips the rest of the inner loop and goes to the next iteration of the outer loop.
  • If the value of j is not 2, the inner loop iteration is printed.
Outer loop iteration: 1
  Inner loop iteration: i = 1, j = 1
Skipping inner loop when j = 2

Outer loop iteration: 2
  Inner loop iteration: i = 2, j = 1
Skipping inner loop when j = 2

Outer loop iteration: 3
  Inner loop iteration: i = 3, j = 1
Skipping inner loop when j = 2

Conclusion

The continue statement in Java allows developers to skip the current iteration of a loop and proceed to the next one. It enhances code readability and control flow in while, do-while, for, and nested loops. By mastering continue, you can write cleaner and more efficient Java programs.

We’d Love to Hear From You!

Do you have questions or feedback about the continue statement in Java? We’re here to help! Whether you need further clarification, have suggestions, or just want to share your thoughts, please leave a comment below. Your feedback is invaluable in helping us improve our content and support others. Don’t hesitate to reach out—we’re excited to hear from you!

Related Articles:

While Loop In Java: Syntax, Flowchart, and Practical Examples

do while loop in Java: Syntax, Flowchart, and Practical Examples

Leave a Comment

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

Scroll to Top
Aitechray