break和continue跳出多重循环

11 篇文章 1 订阅

跳出单层循环：

public static void main (String args[]) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (j == 2) break;
System.out.println("i = " + i + "; j = " + j);
}
}
System.out.println("==============");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (j == 2) continue;
System.out.println("i = " + i + "; j = " + j);
}
}
}


i = 0; j = 0
i = 0; j = 1
i = 1; j = 0
i = 1; j = 1
i = 2; j = 0
i = 2; j = 1
i = 3; j = 0
i = 3; j = 1
==============
i = 0; j = 0
i = 0; j = 1
i = 0; j = 3
i = 1; j = 0
i = 1; j = 1
i = 1; j = 3
i = 2; j = 0
i = 2; j = 1
i = 2; j = 3
i = 3; j = 0
i = 3; j = 1
i = 3; j = 3


如何直接跳出多层循环：

public static void main (String args[]) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (j == 2) break grade1;
System.out.println("i = " + i + "; j = " + j);
}
}
System.out.println("==============");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (j == 2) continue grade2;
System.out.println("i = " + i + "; j = " + j);
}
}
}


i = 0; j = 0
i = 0; j = 1
==============
i = 0; j = 0
i = 0; j = 1
i = 1; j = 0
i = 1; j = 1
i = 2; j = 0
i = 2; j = 1
i = 3; j = 0
i = 3; j = 1

• 8
点赞
• 22
收藏
• 打赏
• 0
评论
03-16 1万+
10-28 456
11-20 2711
05-08 6190
02-11 9190
11-21 8884
01-05 1108
07-14 5531
06-14 863
11-12 420
05-05 360
02-08 546
11-22 394

“相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

©️2022 CSDN 皮肤主题：深蓝海洋 设计师：CSDN官方博客

leo_messi94

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。