首页 > Java > java教程 > 正文

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

WBOY
发布: 2023-08-26 11:49:06
转载
823人浏览过

在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  
}  
登录后复制

Example

以下是如何在Java中使用带标签的break语句退出嵌套循环的示例:

立即学习Java免费学习笔记(深入)”;

public class Test{
   public static void main(String args[]){   
      outerloop:
      for (int i = 0; i < 5; i++) {
         for (int j = 0; j < 5; j++) {
            if (i == 2 && j == 2) {
               break outerloop;
            }
         System.out.println("i = " + i + ", j = " + j);
         }
      }
   } 
}
登录后复制

Output

i = 0, j = 0
i = 0, j = 1
i = 0, j = 2
i = 0, j = 3
i = 0, j = 4
i = 1, j = 0
i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 1, j = 4
i = 2, j = 0
i = 2, j = 1
登录后复制

In this example, the outer loop is labeled as “outerloop” using the label syntax. Within the inner loop, the if statement checks if the current values of i and j are equal to 2. If they are equal, you can use the break statement with the label “outerloop” to come out of the outer loop, otherwise, the loop continues to execute and prints the current values of i and j.

NameGPT名称生成器
NameGPT名称生成器

免费AI公司名称生成器,AI在线生成企业名称,注册公司名称起名大全。

NameGPT名称生成器 0
查看详情 NameGPT名称生成器

When this program is run, it will print the values of i and j for each iteration of the loop until i and j are both equal to 2 and at that point, the control will break out of the outer loop and terminate the program.

Practical Applications of Breaking Multiple Loops

各种情况都需要能够打破多个循环的能力。例如,一个程序在多维数组中搜索特定元素。如果找到了该元素,我们可以退出两个循环,从而减少处理时间。另一个例子是检查游戏中的碰撞。如果发现碰撞,我们可以退出两个循环,以防止无意义的计算。

Common Mistakes to Avoid

There are a few frequent errors to avoid when breaking multiple loops in Java. Forgetting to name the outer loop is one of the most frequent mistakes. We won't be able to use the outer loop's label in the break statement if we don't label it. Using the incorrect loop name in the break statement is another frequent error. When exiting a loop, be sure to use the right name.

Conclusion

在本文中,您学习了如何在Java中通过引用其名称来中断任何外部嵌套循环。您还了解了如何创建嵌套循环,如何跳出单个循环以及如何使用标记循环跳出多个循环,以及中断多个循环的实际应用和要避免的常见错误。

以上就是在Java中通过引用其名称打破任何外部嵌套循环的详细内容,更多请关注php中文网其它相关文章!

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号