break
强制停止整个循环,不再执行
import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 10; i++) {
if (i == 5){
break; //当i == 5时,停止整个for循环
}
System.out.println(i);
}
scanner.close();
}
}
continue
终止这一次循环,不执行剩余的语句,接着进行下一次循环的判定
import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 10; i++) {
if (i == 5){
continue; //当i == 5时,停止这次for循环,不再输出,接着执行下一次循环
}
System.out.println(i);
}
scanner.close();
}
}