「Java」break文で二重ループを抜けるサンプル

2021年1月15日

書式

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
ラベル名:{
for(ループ1){
for(ループ2) {
if(条件式) break ラベル名
}
}
}
ラベル名:{ for(ループ1){ for(ループ2) { if(条件式) break ラベル名 } } }
ラベル名:{
 for(ループ1){
  for(ループ2) {
    if(条件式) break ラベル名
  }
 } 
}

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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

Java

Posted by arkgame