杰瑞科技汇

continue if java

Of course! The continue keyword in Java is a control flow statement used inside loops (for, while, do-while). Its purpose is to skip the rest of the current iteration and immediately jump to the next iteration of the loop.

continue if java-图1
(图片来源网络,侵删)

Think of it as saying, "I'm done with this specific round of the loop, let's move on to the next one."


How continue Works

When the continue statement is executed:

  1. The code inside the loop, after the continue statement, is skipped.
  2. The program immediately jumps to the loop's condition check (for while and do-while) or the update/iteration statement (for for).
  3. The loop then continues with the next iteration.

Key Differences: continue vs. break

This is a very common point of confusion. Here’s a simple way to remember:

Keyword Action Effect on the Loop
break Exits the loop entirely. The loop stops completely. The next statement after the loop is executed.
continue Skips the current iteration. The loop continues running, but it starts over from the beginning of the next iteration.

Examples in Different Loop Types

Let's see continue in action with for, while, and do-while loops.

continue if java-图2
(图片来源网络,侵删)

for Loop

This is the most common use case. The continue statement jumps to the i++ part of the loop.

public class ContinueExampleFor {
    public static void main(String[] args) {
        System.out.println("Printing numbers from 1 to 10, but skipping 4 and 7:");
        for (int i = 1; i <= 10; i++) {
            // Check if the current number is 4 or 7
            if (i == 4 || i == 7) {
                System.out.println("Skipping " + i);
                continue; // Skip the rest of this iteration and go to the next (i++)
            }
            // This line will be skipped for i = 4 and i = 7
            System.out.println("Current number: " + i);
        }
    }
}

Output:

Printing numbers from 1 to 10, but skipping 4 and 7:
Current number: 1
Current number: 2
Current number: 3
Skipping 4
Current number: 5
Current number: 6
Skipping 7
Current number: 8
Current number: 9
Current number: 10

while Loop

In a while loop, continue jumps back to the condition check.

public class ContinueExampleWhile {
    public static void main(String[] args) {
        int count = 0;
        System.out.println("Counting to 5, but skipping 3:");
        while (count < 5) {
            count++; // Increment the counter first
            if (count == 3) {
                System.out.println("Skipping " + count);
                continue; // Jump back to the 'while (count < 5)' condition
            }
            System.out.println("Current count: " + count);
        }
    }
}

Output:

continue if java-图3
(图片来源网络,侵删)
Counting to 5, but skipping 3:
Current count: 1
Current count: 2
Skipping 3
Current count: 4
Current count: 5

do-while Loop

Similar to the while loop, continue jumps back to the condition check.

public class ContinueExampleDoWhile {
    public static void main(String[] args) {
        int number = 0;
        System.out.println("Looping until number is 5, but skipping 2:");
        do {
            number++;
            if (number == 2) {
                System.out.println("Skipping " + number);
                continue; // Jump back to the 'while (number < 5)' condition
            }
            System.out.println("Current number: " + number);
        } while (number < 5);
    }
}

Output:

Looping until number is 5, but skipping 2:
Current number: 1
Skipping 2
Current number: 3
Current number: 4
Current number: 5

Labeled continue (Advanced)

Java allows you to use continue with a label. This is useful when you have nested loops and you want to skip an iteration in an outer loop.

  • A label is simply an identifier followed by a colon () placed before a loop.
  • continue labelName; will jump to the next iteration of the loop marked with labelName.

Example:

public class LabeledContinueExample {
    public static void main(String[] args) {
        outerLoop: // This is the label for the outer for loop
        for (int i = 1; i <= 3; i++) {
            System.out.println("\nOuter loop iteration: " + i);
            innerLoop:
            for (int j = 1; j <= 3; j++) {
                if (i == 2 && j == 2) {
                    System.out.println("  -> Skipping inner loop for i=2, j=2.");
                    continue outerLoop; // Jumps to the next iteration of the outerLoop
                }
                System.out.println("  -> Inner loop value: i=" + i + ", j=" + j);
            }
        }
    }
}

Output:

Outer loop iteration: 1
  -> Inner loop value: i=1, j=1
  -> Inner loop value: i=1, j=2
  -> Inner loop value: i=1, j=3
Outer loop iteration: 2
  -> Skipping inner loop for i=2, j=2.
Outer loop iteration: 3
  -> Inner loop value: i=3, j=1
  -> Inner loop value: i=3, j=2
  -> Inner loop value: i=3, j=3

Notice how when i=2 and j=2, the continue outerLoop; statement caused the entire inner loop to stop and the outer loop to go to its next iteration (i=3).


When to Use continue

continue is a useful tool for making code cleaner and more readable, especially when you have complex logic or "guard clauses" at the beginning of a loop body.

Good Use Case:

// Instead of nesting the main logic in an 'if' block
for (User user : users) {
    if (!user.isActive()) {
        continue; // Skip inactive users
    }
    // Main logic for active users (no indentation needed)
    sendEmail(user);
    logActivity(user);
}

Caution: Overusing continue can sometimes make code harder to follow, similar to overusing goto in other languages. Use it judiciously to improve clarity, not to create confusing control flow.

分享:
扫描分享到社交APP
上一篇
下一篇