0

0

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

WBOY

WBOY

发布时间:2023-08-26 11:49:06

|

830人浏览过

|

来源于tutorialspoint

转载

在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.

Magician
Magician

Figma插件,AI生成图标、图片和UX文案

下载

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速学教程(入门到精通)
java速学教程(入门到精通)

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

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
Java 桌面应用开发(JavaFX 实战)
Java 桌面应用开发(JavaFX 实战)

本专题系统讲解 Java 在桌面应用开发领域的实战应用,重点围绕 JavaFX 框架,涵盖界面布局、控件使用、事件处理、FXML、样式美化(CSS)、多线程与UI响应优化,以及桌面应用的打包与发布。通过完整示例项目,帮助学习者掌握 使用 Java 构建现代化、跨平台桌面应用程序的核心能力。

37

2026.01.14

php与html混编教程大全
php与html混编教程大全

本专题整合了php和html混编相关教程,阅读专题下面的文章了解更多详细内容。

19

2026.01.13

PHP 高性能
PHP 高性能

本专题整合了PHP高性能相关教程大全,阅读专题下面的文章了解更多详细内容。

37

2026.01.13

MySQL数据库报错常见问题及解决方法大全
MySQL数据库报错常见问题及解决方法大全

本专题整合了MySQL数据库报错常见问题及解决方法,阅读专题下面的文章了解更多详细内容。

19

2026.01.13

PHP 文件上传
PHP 文件上传

本专题整合了PHP实现文件上传相关教程,阅读专题下面的文章了解更多详细内容。

16

2026.01.13

PHP缓存策略教程大全
PHP缓存策略教程大全

本专题整合了PHP缓存相关教程,阅读专题下面的文章了解更多详细内容。

6

2026.01.13

jQuery 正则表达式相关教程
jQuery 正则表达式相关教程

本专题整合了jQuery正则表达式相关教程大全,阅读专题下面的文章了解更多详细内容。

3

2026.01.13

交互式图表和动态图表教程汇总
交互式图表和动态图表教程汇总

本专题整合了交互式图表和动态图表的相关内容,阅读专题下面的文章了解更多详细内容。

45

2026.01.13

nginx配置文件详细教程
nginx配置文件详细教程

本专题整合了nginx配置文件相关教程详细汇总,阅读专题下面的文章了解更多详细内容。

9

2026.01.13

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Kotlin 教程
Kotlin 教程

共23课时 | 2.5万人学习

SQL 教程
SQL 教程

共61课时 | 3.4万人学习

C++教程
C++教程

共115课时 | 12.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

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