1、if…else…
2、多重if
3、switch
4、while
5、do…while
6、while和do…while
7、Break
结束整个循环过程
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请输入你的选项:1,2");
int in = sc.nextInt();
if (in == 2) {
break;//结束整个循环过程
}
}
System.out.println("game over");
}
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请输入你的选项:1,2");
int in = sc.nextInt();
switch (in) {
case 1:
break;//结束当前switch语句,但不结束当前循环
case 2:
break;
default:
break;
}
if (in == 2){
break;//结束当前循环
}
}
如果我们在循环中使用switch语句,那么在switch语句中break就不能结束当前循环了,而只能结束switch语句。
8、Continue
Continue:跳过本次循环体中余下尚未执行的语句,立即进行下一次的循环条件判定,可以理解为仅结束本次循环。
9、break,continue,return的区别
public class Test {
public static void main(String[] args) {
for(int i=0; i<10; i++){
if(i==5){
break (continue){return};
}
System.out.print(i+" ");
}
}
}
执行结果:
break:
Continue:
return:
注意:
break是退出该循环,本循环结束但是不影响上一级循环。
return是明确地从一个方法返回。也就是,return 语句使程序控制返回到调用它方法。因此,将它分类为跳转语句。
continue是直接跳过当次循环。