[JavaSE column 16] Java loop exit statement break, continue

Author's homepage : Designer Xiaozheng
Author's brief introduction : A Java full-stack software engineer from Ningbo, Zhejiang. Certified lecturer of CSDN Academy and Blue Bridge Cloud Class, high-quality creator in the full-stack field. Love technology, focus on business, open cooperation, willing to share, look forward to growing up with you and me!
Main direction : Vue, SpringBoot, WeChat applet

The break and continue statements are one of the loop exit statements in Java . This article will explain the break and continue statements in Java.


1. The syntax of the break statement

In Java, the break statement is used to terminate the execution of a loop or switch statement and jump out of the current code block.

When a certain condition is met, you can use the break statement to end the loop early without waiting for the natural termination of the loop condition.

A code example of the break statement is as follows.

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

The above code uses a for loop to iterate, when the variable iii is equal to5 5At 5 o'clock, executethe breakstatement, terminate the loop and jump out. Therefore, the code will output something like this:

0
1
2
3
4

notice when iii is equal to5 55 , the loop is terminated early and subsequent iterations are not executed.

insert image description here


Second, the syntax of the continue statement

In Java, the continue statement is used to skip the remaining code of the current loop and start the next loop .

It will ignore all codes after the continue statement in the loop body, and directly enter the next loop , and its syntax is as follows:

continue;

Here is a concrete code example:

for (int i = 0; i < 5; i++) {
    
    
    if (i == 2) {
    
    
        continue; // 当i等于2时,跳过当前循环
    }
    System.out.println(i);
}

The above code uses a for loop to iterate, when the variable iii is equal to2 2At 2 o'clock, executethe continue statement, skip the remaining code of the current loop, and proceed directly to the next round of loop. Therefore, the code will output something like this:

0
1
3
4

notice when iii is equal to2 22 , the corresponding value is skipped and not printed. The cycle then continues untiliii is less than5 55 so far.

insert image description here


3. Differences and usage scenarios of break and continue

In Java, both break and continue are keywords used to control the flow of loop execution , but their functions and usage scenarios are different.

3.1 break statement

  • Function : Terminate the execution of the current loop or switch statement, and jump out of the current code block.
  • Usage scenario : When a certain condition is met, it is necessary to end the loop early or jump out of the switch statement. You can use break to achieve this goal.

Common usage scenarios include:

  1. In the loop, judge whether to terminate the loop according to certain conditions;
  2. After executing a specific branch in the switch statement, the execution of the switch statement ends prematurely.

3.2 continue statement

  • Function : Skip the remaining code of the current loop and start the next loop.
  • Usage scenario : When a certain condition is met, it is necessary to skip part of the code of the current loop and directly enter the next loop, which can be achieved by using continue.

Common usage scenarios include:

  1. In the loop, judge whether to skip the current iteration according to certain conditions;

  2. In some cases it is desirable to skip specific loop iterations instead of terminating the entire loop.

3.3 Summary of usage scenarios

The break statement is used to terminate the execution of a loop or switch statement, and is suitable for scenarios where the loop needs to be terminated early or the switch statement needs to be jumped out of.

The continue statement is used to skip the remaining code of the current loop and enter the next loop, which is suitable for scenarios where specific iterations or conditions need to be skipped.

insert image description here


Four. Summary

This article briefly introduces the loop exit statements break and continue in Java, explains the syntax, and demonstrates sample codes. In the next blog, I will explain the concepts and usage scenarios of stack and heap in Java.

insert image description here

Guess you like

Origin blog.csdn.net/qq_41464123/article/details/131483830