杰瑞科技汇

Java中if与continue如何配合使用?

Of course! Let's break down the if statement and the continue keyword in Java, and how they work together.

Java中if与continue如何配合使用?-图1
(图片来源网络,侵删)

The if Statement

The if statement is a fundamental control flow structure in Java. It allows your program to execute a block of code only if a certain condition is true.

Syntax:

if (condition) {
    // Code to execute if the condition is true
}

How it works:

  1. The condition inside the parentheses is evaluated.
  2. If the condition evaluates to true, the code inside the curly braces is executed.
  3. If the condition evaluates to false, the code inside the curly braces is skipped, and the program continues with the line of code after the if block.

Example:

Java中if与continue如何配合使用?-图2
(图片来源网络,侵删)
int age = 20;
if (age >= 18) {
    System.out.println("You are an adult.");
}
System.out.println("This line always runs.");

Output:

You are an adult.
This line always runs.

The continue Keyword

The continue keyword is used inside loops (like for, while, or do-while). It's a jump statement that tells the loop to immediately stop the current iteration and jump to the next iteration.

How it works: When continue is executed:

  1. The rest of the code inside the loop's body for the current iteration is skipped.
  2. The loop's update expression (in a for loop) or condition check (in a while loop) is performed.
  3. If the loop condition is still true, the next iteration begins.

Example without if:

Java中if与continue如何配合使用?-图3
(图片来源网络,侵删)
for (int i = 1; i <= 5; i++) {
    System.out.println("Iteration: " + i);
    if (i == 3) {
        continue; // Skip the rest of the code when i is 3
    }
    System.out.println("This line was skipped for i=3.");
}

Output:

Iteration: 1
This line was skipped for i=3.
Iteration: 2
This line was skipped for i=3.
Iteration: 3
Iteration: 4
This line was skipped for i=3.
Iteration: 5
This line was skipped for i=3.

Notice how "This line was skipped for i=3." was not printed during the 3rd iteration.


if and continue Together

Combining if and continue is a very common and powerful pattern. The logic is:

"If a certain condition is met, stop processing the rest of this loop iteration and move on to the next item."

This is useful for filtering out items you don't want to process.

Common Use Case: Filtering a Loop

Let's say you want to process a list of numbers, but only the even ones.

Example: Print only even numbers from 1 to 10

System.out.println("Printing only even numbers:");
for (int i = 1; i <= 10; i++) {
    // 1. Check the condition
    if (i % 2 != 0) {
        // 2. If the number is odd...
        continue; // ...skip the rest of this loop iteration.
    }
    // 3. This code only runs if the 'if' condition was false (i.e., the number is even)
    System.out.println("Found an even number: " + i);
}

How it works step-by-step:

  • i = 1: 1 % 2 != 0 is true. continue is executed. The System.out.println line is skipped.
  • i = 2: 2 % 2 != 0 is false. The if block is skipped. The System.out.println line runs.
  • i = 3: 3 % 2 != 0 is true. continue is executed. The System.out.println line is skipped.
  • ...and so on.

Output:

Printing only even numbers:
Found an even number: 2
Found an even number: 4
Found an even number: 6
Found an even number: 8
Found an even number: 10

Key Differences: continue vs. break

It's crucial not to confuse continue with break.

Keyword Action Effect on Loop
continue Jumps to the next iteration. Skips the rest of the current iteration and continues the loop.
break Exits the loop immediately. Stops the loop entirely. The code after the loop will execute next.

Example with break:

System.out.println("Loop with break:");
for (int i = 1; i <= 5; i++) {
    if (i == 3) {
        break; // Exit the loop completely when i is 3
    }
    System.out.println("Iteration: " + i);
}

Output:

Loop with break:
Iteration: 1
Iteration: 2

The loop stops completely at i = 3. It never reaches i = 4 or i = 5.


Summary

Concept Description Analogy
if A decision-maker. It runs a block of code only if a condition is true. A fork in the road: "If the road is open, go this way."
continue A loop controller. It tells a loop to stop the current cycle and start the next one. Skipping a question on a test and moving to the next one.
if + continue A filter. It checks a condition and, if true, skips the rest of the current loop cycle. "If this apple is rotten, throw it away (continue) and check the next one."
分享:
扫描分享到社交APP
上一篇
下一篇