What is Java continue Statement? (Complete Guide With Examples)

 

What is Java continue Statement?

    Introduction to Java continue Statement

    The continue statement in Java is used to skip the current iteration of a loop and proceed with the next one. It provides a powerful way to control how loops behave, especially when certain conditions are met during iteration.

    Example:

    java

    Copy

    Edit

    for (int i = 1; i <= 5; i++) {

    if (i == 3) {

       continue;

      }

     System.out.println(i);

    }

    Output:

    Copy

    Edit

    1

    2

    4

    5

    Importance of Loop Control in Java

    Loop control mechanisms like break and continue allow developers to manage loop execution precisely. While break terminates a loop, continue helps skip specific iterations, making the code more efficient and readable.

    Syntax of continue Statement

    The basic syntax of the continue statement is simple:

    java

    Copy

    Edit

    continue;

    You can also use it with labels for nested loops:

    java

    Copy

    Edit

    continue labelName;

    Working of continue Statement

    When the continue statement is executed, the rest of the code inside the loop for the current iteration is skipped, and control jumps to the next loop iteration.

    Example:

    java

    Copy

    Edit

    int i = 0;

    while (i < 5) {

       i++;

       if (i == 2) continue;

       System.out.println(i);

    }

    continue in for Loop

    The most common use of continue is within a for loop.

    Example:

    java

    Copy

    Edit

    for (int i = 0; i < 10; i++) {

        if (i % 2 == 0) continue;

        System.out.println(i);

    }

    Output:

    Copy

    Edit

    1

    3

    5

    7

    9

    continue in while Loop

    When used in a while loop, the continue skips to the next iteration by jumping to the condition check.

    Example:

    java

    Copy

    Edit

    int i = 0;

    while (i < 5) {

       i++;

       if (i == 3) continue;

       System.out.println(i);

    }

    continue in do-while Loop

    The continue statement in a do-while loop works similarly but always executes the body at least once.

    Example:

    java

    Copy

    Edit

    int i = 0;

    do {

       i++;

       if (i == 2) continue;

       System.out.println(i);

    } while (i < 5);

    Using continue with if Statement

    The continue statement is often placed inside an if condition to skip certain values.

    Example:

    java

    Copy

    Edit

    for (int i = 1; i <= 5; i++) {

       if (i == 4) continue;

       System.out.println("i: " + i);

    }

    Difference between break and continue

    Feature break continue
    Function Exits the loop Skips current iteration
    Used in Loops and switch-case Loops only
    Control Flow Terminates loop completely Goes to next iteration

    Nested Loops with continue

    In nested loops, continue affects only the innermost loop.

    Example:

    java

    Copy

    Edit

    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);

       }

    }

    continue with Label

    Labels allow continue to apply to outer loops.

    Example:

    java

    Copy

    Edit

    outer:

    for (int i = 1; i <= 3; i++) {

       for (int j = 1; j <= 3; j++) {

          if (j == 2) continue outer;

          System.out.println("i=" + i + ", j=" + j);

       }

    }

    Practical Use Cases of continue

    •  Skipping specific elements in a list.
    •  Skipping invalid inputs.
    •  Filtering data dynamically.

    Skipping Odd or Even Numbers

    Example:

    java

    Copy

    Edit

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

       if (i % 2 != 0) continue;

       System.out.println(i); // Prints only even numbers

    }

    Filtering Specific Elements in Arrays

    Example:

    java

    Copy

    Edit

    int[] numbers = {10, 15, 20, 25};

    for (int num : numbers) {

       if (num == 20) continue;

       System.out.println(num);

    }

    continue in Infinite Loops

    Even infinite loops can benefit from continue when using a break condition.

    Example:

    java

    Copy

    Edit

    int i = 0;

    while (true) {

      i++;

      if (i % 2 == 0) continue;

      if (i > 10) break;

      System.out.println(i);

    }

    Performance Considerations

    Using continue can optimize performance by reducing unnecessary computation in some loops, especially in large datasets.

    Common Mistakes and How to Avoid Them

    •  Not updating loop control variable before continue.
    •  Using continue unnecessarily.
    •  Misunderstanding labeled continue.

    Best Practices for Using continue

    •  Use only when it enhances clarity.
    •  Avoid nested continue when possible.
    •  Add comments for complex continue logic.

    Alternatives to continue

    In some cases, using conditions instead of continue can make code clearer.

    Instead of:

    java

    Copy

    Edit

    if (x == 3) continue;

    Use:

    java

    Copy

    Edit

    if (x != 3) {

       // your code here

    }

    Debugging Code with continue

    Using continue may skip important debugging lines. Consider logging or breakpoints before it.

    Interview Questions on continue

    How does continue differ from break?

    Can continue be used outside of loops?

    What happens if you forget to update a variable before continue?

    Advanced Loop Control with continue

    Combine continue with return, break, and labels to build complex logic while avoiding deeply nested structures.

    Comparison with Other Languages

    Language continue Usage Notes
    Java Supported Label support
    Python Supported No labels
    C++ Supported Similar syntax

    Real-life Java Project Examples using continue

    Example: Input Validation Loop

    java

    Copy

    Edit

    Scanner sc = new Scanner(System.in);

    for (int i = 0; i < 5; i++) {

       System.out.print("Enter number: ");

       int num = sc.nextInt();

       if (num < 0) {

         System.out.println("Invalid! Skipping...");

         continue;

       }

       System.out.println("Accepted: " + num);

    }

    Summary and Final Thoughts

    The continue statement is a fundamental tool for controlling loop execution in Java. By mastering continue, you write more efficient and readable code. Always use it when it simplifies logic and improves performance.


    Also Read:

    Post a Comment

    0 Comments