「Java」break文で二重ループを抜けるサンプル
書式
ラベル名:{
for(ループ1){
for(ループ2) {
if(条件式) break ラベル名
}
}
}
ラベル名:{
for(ループ1){
for(ループ2) {
if(条件式) break ラベル名
}
}
}
ラベル名:{ for(ループ1){ for(ループ2) { if(条件式) break ラベル名 } } }
使用例
package com.arkgame.study;
public class LoopBreakTest {
private static int MM = 18;
private static int NN = 19;
public static void main(String[] args) {
TESTLABEL: {
for (int i = 14; i <= MM; i++) {
for (int j = 15; j <= NN; j++) {
System.out.println("計算結果: " + i + "+" + j + "=" + (i + j));
if (j > 16)
break TESTLABEL;
}
}
}
}
}
package com.arkgame.study;
public class LoopBreakTest {
private static int MM = 18;
private static int NN = 19;
public static void main(String[] args) {
TESTLABEL: {
for (int i = 14; i <= MM; i++) {
for (int j = 15; j <= NN; j++) {
System.out.println("計算結果: " + i + "+" + j + "=" + (i + j));
if (j > 16)
break TESTLABEL;
}
}
}
}
}
package com.arkgame.study; public class LoopBreakTest { private static int MM = 18; private static int NN = 19; public static void main(String[] args) { TESTLABEL: { for (int i = 14; i <= MM; i++) { for (int j = 15; j <= NN; j++) { System.out.println("計算結果: " + i + "+" + j + "=" + (i + j)); if (j > 16) break TESTLABEL; } } } } }
実行結果
計算結果: 14+15=29
計算結果: 14+16=30
計算結果: 14+17=31