三、程序流程控制
1、分支流程
if(x>y) { // 满足条件(x>y)进入
System.out.println("x>y");
}else {
System.out.println("x<=y");
}
2、循环流程
2 - 1 for循环
for(int i = 0; i < 5 ; i++){
}
2 - 1 while循环-do while循环
while(){
}
do{
}while();
2 - 1 多重选择-switch语句
Scanner in = new Scanner(System.in);
int choice = in.nextInt();
System.out.println("请输入要进行的操作(1,2,3):");
switch(choice){
case 1:
break;
case 2:
break;
case 3:
break;
}
2 - 1 - break / continue 关键字
- break
- continue
- 结束本次循环(越过当前循环体的剩余部分,直接跳到循环首部)
public class Main {
public static void main(String[] args) {
int i = 1;
while (i < 10) {
if (i == 5){
System.out.println("因为 i=5 ::> 结束本次循环");
i++;
continue;
}else{
System.out.println(i);
i++;
}
if (i == 7){
System.out.println("因为 i=7 ::> 结束循环");
break;
}
}
}
}