「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 = 8;
private static int NN = 9;
public static void main(String[] args) {
for (int i = 5; i <= MM; i++) {
for (int j = 6; j <= NN; j++) {
System.out.println("計算結果: " + i + "+" + j + "=" + (i + j));
if (j > 7)
break;
}
}
}
}
package com.arkgame.study; public class LoopBreakTest { private static int MM = 8; private static int NN = 9; public static void main(String[] args) { for (int i = 5; i <= MM; i++) { for (int j = 6; j <= NN; j++) { System.out.println("計算結果: " + i + "+" + j + "=" + (i + j)); if (j > 7) break; } } } }
package com.arkgame.study;

public class LoopBreakTest {

      private static int MM = 8;
      private static int NN = 9;

      public static void main(String[] args) {

            for (int i = 5; i <= MM; i++) {
                  for (int j = 6; j <= NN; j++) {
                        System.out.println("計算結果: " + i + "+" + j + "=" + (i + j));
                        if (j > 7)
                              break;

                  }

            }

      }

}

実行結果
計算結果: 5+6=11
計算結果: 5+7=12
計算結果: 5+8=13
計算結果: 6+6=12
計算結果: 6+7=13
計算結果: 6+8=14
計算結果: 7+6=13
計算結果: 7+7=14
計算結果: 7+8=15
計算結果: 8+6=14
計算結果: 8+7=15
計算結果: 8+8=16

 

Java

Posted by arkgame