Three of the countless uses of break in JAVA

In Java, there are three uses of the break statement. The first is to terminate the sequence of statements in a switch statement, the second is to exit the loop, and the third is the "civilized" form of a goto statement!

We know that the goto statement will destroy the structure of the program, so we generally do not use this feature. But in some cases, the goto statement is valuable for process control and the structure is legal.

For example, when exiting a series of deeply nested loops, the goto statement is very useful. Although the goto keyword is reserved in Java, there is no goto statement, and there is no place to use this keyword. So in order to be able to use functions similar to the goto statement, Java defines an extended form of the break statement.

That is, through this form of break statement, one or more code blocks can be interrupted. And these code blocks do not have to be part of a loop or switch statement, they can be any code block.

In addition, this form of break statement can specify exactly where to continue execution, because this form of break statement uses labels to work.

The general grammatical form of the break statement using labels is as follows:
break labe

label is the name of the label of a code block. It can be an independent code block or the target code block of another statement.

When this form of break statement is executed, the execution control of the program will jump out of the code block named by the label. The code block with the label must contain the break statement, but it does not have to contain the break statement immediately.

In other words, you can use the labeled break statement to jump out of a series of nested code. But you cannot use the break statement to transfer control out of a code block that does not contain the break statement.

In order to name the code block, you can put a label before the code block. The label can be any valid java identifier followed by a colon.

As long as the code block is named, you can use the named label as the target of the break statement, so that you can jump out of the code block and resume execution at the end of the identified code block.

The following program code:

class Dome {
    
    
 public static void main(String[] args) {
    
    
 a:{
    
    
  System.out.println("0");
  b:{
    
    
  System.out.println("1");
  c:{
    
    
  System.out.println("2");
  if(1==1)
  break a;
  }
  System.out.println("3");
  }a
  System.out.println("4");
  }
  System.out.println("5");
 }
}

So this extended form of the break statement provides the advantages of the goto statement without the problem of the goto statement.

Supplementary knowledge: Java multiple loops and how to use break and continue

Multiple loops

The structure containing loop statements in the loop body is called multiple loops. The three loop statements can be nested by themselves or each other. The most common is the double loop. In the double loop, each time the outer loop is executed, the inner loop is executed once.

As follows:
Insert picture description here

For example: use * to print a rectangle:

The implementation code is:
Insert picture description here

Execution process: When i = 1, the outer loop condition is established, enter the inner loop, and start printing the first line of content. At this time, j starts from 1, loops 8 times, and wraps after the inner loop ends to achieve the output of 8 * in the first line.
Next, return to the outer loop i to change to 2, ready to print the next line, and so on, until the rectangle is printed.

for(int i=0;i<5;i++){
    
    
for(int j=1;j<=5;j++){
    
    //i每增加1,也就是for循环一次,这个循环for要循环完,要循环5次。也就是一圈。
 }
}

Simply put, when the outer layer is executed once, the inner (the second for) has already been executed

break, continue statement

Both break and continue statements can control the execution flow of the loop. The break statement can directly jump out of the entire loop. By default, it jumps directly to the next statement for execution. For example, we want to output a triangle asterisk like the following.

Break can be used in if-else to jump out of the current loop directly.

In a multi-level loop, a break statement only jumps one level outward.

The function of the continue statement is to skip the remaining statements in the loop body and to the end of the loop to force the execution of the next loop.

The continue statement is only used in loop bodies such as for, while, do-while, and is often used together with if conditional statements to speed up the loop.

for (int i = 0; i < 3; i++) {
    
    
 System.out.println("开始第" + i + "次for循环");
 if (i == 1) {
    
    
   continue;//程序运行后面的代码就不会执行,但循环不会退出。
 }
 System.out.println("看看continue后这里执行了吗?");
}

If the above statement uses break, the loop after 1 will not be executed

The third usage of break in the above Java is all the content shared by the editor. I hope to give you a reference, and I hope you can support the editor.

Guess you like

Origin blog.csdn.net/dcj19980805/article/details/115206571