What is the difference between the loop exit statement break and continue?

1. The break statement

In Java, the break statement is used to terminate the execution of the current loop or switch statement and jump out of the structure. When the break statement is executed, the program will jump out of the innermost loop or switch statement that contains the break statement, and then continue to execute the code after the structure.

The break statement can be used in different scenarios, as follows:

Using the break statement in a loop: When a certain condition is met, you can use the break statement to terminate the execution of the loop early.

for (int i = 0; i < 10; i++) {
    
    
    if (i == 5) {
    
    
        break; // 当 i 等于 5 时跳出循环
    }
    System.out.println(i);
}

Use a break statement in a switch statement: Each case branch usually ends with a break statement, which avoids executing other case branches. If no break statement is used, execution will continue to the next case branch until a break statement is encountered or the switch statement ends.

int day = 5;
switch (day) {
    
    
    case 1:
        System.out.println("Monday");
        break;
    case 2:
        System.out.println("Tuesday");
        break;
    case 3:
        System.out.println("Wednesday");
        break;
    default:
        System.out.println("Other day");
}

In short, the break statement is used to terminate the execution of the loop or switch statement early, which can help control the flow of the program.

insert image description here

Two, continue statement

In Java, the continue statement is used to end the current loop iteration and start the next iteration. When the program reaches the continue statement, it skips the remaining code of the current iteration and starts the next iteration directly.

The continue statement is usually used in conjunction with a conditional statement (such as an if statement) to skip loop iterations under certain conditions.

The following is an example of the use of the continue statement:

for (int i = 0; i < 10; i++) {
    
    
    if (i == 3 || i == 6) {
    
    
        continue; // 当i等于3或6时跳过当前迭代,开始下一次迭代
    }
    System.out.println(i);
}

In the above code, when i is equal to 3 or 6, the continue statement will skip the current iteration and start the next iteration directly. Therefore, the numbers 3 and 6 are skipped in the output.

The continue statement can also be used in nested loops to skip the current iteration of the loop and start the next iteration.

In short, the continue statement is used to skip the current iteration and start the next iteration, which can help control the execution flow of the loop.

insert image description here

3. What is the difference between break and continue statements?

In Java, both the break statement and the continue statement are keywords used to control the execution flow of a loop, but their functions and usage are slightly different:

break statement: The break statement is used to terminate the execution of the current loop or switch statement and jump out of the structure. When the break statement is executed, the program will jump out of the innermost loop or switch statement that contains the break statement, and then continue to execute the code after the structure. The break statement can be used in loops and switch statements.

continue statement: The continue statement is used to end the current iteration of the loop and start the next iteration. When the program executes to the continue statement, it will skip the remaining code of the current iteration and start the next iteration directly. The continue statement is usually used in conjunction with a conditional statement (such as an if statement) to skip loop iterations under certain conditions. The continue statement can only be used in loops.

Summary of differences:

The break statement is used to terminate the execution of the entire loop or switch statement, and jump out of the loop or switch statement.
The continue statement is used to end the current iteration, skip the rest of the code, and start the next iteration.
The break statement can be used in loops and switch statements, while the continue statement can only be used in loops.
The break statement will jump out of the innermost loop or switch statement that contains it, and the continue statement will only end the current iteration and continue to the next iteration.

Guess you like

Origin blog.csdn.net/2301_77899321/article/details/131774786