「Java」break、 return とcontinueの使い方サンプルコード
Javaコード
package itstudy;
public class LoopTest {
public static void main(String[] args) {
funcBreak();
funcContinue();
funcReturn();
}
static void funcBreak() {
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
System.out.println(“i=" + i);
} else {
System.out.println(“break running、exit loop");
break;
}
}
}
static void funcContinue() {
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
System.out.println(“continue not run output i=" + i);
} else {
System.out.println(“continue run、exit loop!");
continue;
}
}
}
@SuppressWarnings(“unused")
static void funcReturn() {
for (int i = 0; i < 10; i++) {
System.out.println(“return run,exit funcReturn()");
return;
}
}
}
実行結果
i=0
break running、exit loop
continue not run output i=0
continue run、exit loop!
continue not run output i=2
continue run、exit loop!
continue not run output i=4
continue run、exit loop!
continue not run output i=6
continue run、exit loop!
continue not run output i=8
continue run、exit loop!
return run,exit funcReturn()