Java continue Statement

Introduction

The continue statement in Java is a control flow statement that is used to skip the current iteration of a loop and proceed to the next iteration. It is commonly used within for, while, and do-while loops to control the flow of the loop based on certain conditions.

Table of Contents

  1. What is a continue Statement?
  2. Syntax of continue Statement
  3. How continue Statement Works
  4. Simple continue Statement Example
  5. continue Statement in a for Loop
  6. continue Statement in a while Loop
  7. continue Statement in a do-while Loop
  8. continue Statement in Nested Loops
  9. Using continue with Labels
  10. Conclusion

What is a continue Statement?

The continue statement is used to skip the remainder of the code in the current iteration of a loop and immediately proceed to the next iteration. This is particularly useful when you want to skip certain iterations based on specific conditions.

Syntax of continue Statement

Syntax:

continue;

How continue Statement Works

  1. When the continue statement is encountered, the remaining code in the loop's current iteration is skipped.
  2. The loop proceeds to the next iteration.
  3. In a for loop, the update expression is executed before the next iteration.
  4. In while and do-while loops, the condition is re-evaluated before the next iteration.

Simple continue Statement Example

Example:

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

Explanation: This loop prints the numbers 1, 2, 4, and 5. When i equals 3, the continue statement skips the remainder of the loop body, so 3 is not printed.

continue Statement in a for Loop

The continue statement can be used within a for loop to skip specific iterations based on a condition.

Example:

public class ContinueInForLoop {
    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            if (i % 2 == 0) {
                continue;
            }
            System.out.println("Odd number: " + i);
        }
    }
}

Explanation: This loop prints only the odd numbers between 1 and 10 by skipping the even numbers.

continue Statement in a while Loop

The continue statement can also be used within a while loop to skip specific iterations.

Example:

public class ContinueInWhileLoop {
    public static void main(String[] args) {
        int i = 1;
        while (i <= 10) {
            if (i % 2 == 0) {
                i++;
                continue;
            }
            System.out.println("Odd number: " + i);
            i++;
        }
    }
}

Explanation: This loop prints only the odd numbers between 1 and 10 by skipping the even numbers.

continue Statement in a do-while Loop

The continue statement can be used within a do-while loop to skip specific iterations.

Example:

public class ContinueInDoWhileLoop {
    public static void main(String[] args) {
        int i = 1;
        do {
            if (i % 2 == 0) {
                i++;
                continue;
            }
            System.out.println("Odd number: " + i);
            i++;
        } while (i <= 10);
    }
}

Explanation: This loop prints only the odd numbers between 1 and 10 by skipping the even numbers.

continue Statement in Nested Loops

The continue statement can be used within nested loops to skip iterations of the inner loop.

Example:

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

Explanation: This loop skips the inner loop's iteration when j equals 2, so it does not print pairs where j is 2.

Using continue with Labels

Labels can be used with the continue statement to specify which loop to continue.

Example:

public class ContinueWithLabel {
    public static void main(String[] args) {
        outerLoop: for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= 3; j++) {
                if (j == 2) {
                    continue outerLoop;
                }
                System.out.println("i: " + i + ", j: " + j);
            }
        }
    }
}

Explanation: This loop uses a label outerLoop and continues the outer loop when j equals 2, skipping to the next iteration of the outer loop.

Conclusion

The continue statement in Java is a useful control flow tool that allows you to skip the current iteration of a loop and proceed to the next iteration. Understanding how to use continue effectively, including within nested loops and with labels, can help you write more efficient and readable code.

Comments