[Other] goto, an almost forgotten loop label

continue and break can change the execution flow of the loop, but in multiple loops, these two statements cannot directly jump from the inner loop to the outer loop. In some languages, such as C, it can be realized through the goto statement Multiple loop jumps, but in the non-loop statement structure, the use of goto statement will make the program structure disorder, and the readability will be poor.

In order to prevent the abuse of goto, Java retains the goto keyword, but this keyword has no effect. Then Java invented a labeled continue and break statement to break out of multiple loops.

 

In fact, it is a limited goto statement specially used for loops. Usually, the continue and break statements we use do not have labels. At this time, it jumps out of the current loop by default, and the labeled loop actually gives the loop a name.

 

When using continue or break to add a label, it is to execute continue or break in the loop body where the label is located. For example, if I use break in the inner loop, then the inner loop will stop executing at this time, and then execute the next layer Outer loop, when using break+outer label, then the outer loop will be terminated directly.

Guess you like

Origin blog.csdn.net/weixin_43918614/article/details/124062122