在Java中通过引用其名称打破任何外部嵌套循环

在Java中通过引用其名称打破任何外部嵌套循环

Programming is all about coming up with the best and most efficient ways to solve the real−world problems. There are situations when you want to exit multiple loops simultaneously. This can be accomplished in Java by simply referencing the name of the loop you want to exit. In this tutorial, we'll look at how to break any outer nested loop in Java by referencing its name.

Referencing Loop Names in Java

You can break out of the Java nested loop by labelling the outer loop. This can be accomplished by using a label before the outer loop followed by a colon. The break statement and label combination can then be used inside the inner loop to exit the outer loop.

Syntax

Following is the syntax to reference the loop −

labelname :for(initialization;condition;incr/decr){ //code to be executed } 登录后复制