1.3-逻辑控制
Create by fall on 20 Aug 2023 Recently revised in 20 Aug 2023
流程控制
if
包括以下关键字 if
、else-if
、else
public class StudyFall {
public static void main(String[] args){
int a = 10;
if(a>10){
System.out.println('你比较大');
}else if(0<a && a<10){
System.out.println('你不是那么大');
};
}
}
switch
包括以下关键字:case
、switch
、default
、break
public class StudyFall {
public static void main(String[] args){
int a = 4;
switch(a){
case 5: System.out.println("奇迹");break;
case 4: System.out.println("没有这回事");
case 3: System.out.println("谁来解释一下");break;
default: System.out.println("自在无言中")
}
}
}
for
public class StudyFall {
public static void main(String[] args){
for(int a = 0;a<5;a++){
System.out.println("这是一个梦境");
}
}
}
while
包括以下关键字:while
、do
public class StudyFall {
public static void main(String[] args){
int num = 0;
// 先判断,再执行
while(num<10){
System.out.println("它现在多大了?"+num);
}
// 先执行,再判断
do{
System.out.println("它现在多大了?"+num);
}while(num>5);
}
}
break 语句用于终止某个语句块的执行,一般使用在 switch 或者循环(
for
,while
,do-while
)中,用于终止之后的语句和循环continue 语句用于跳过这次循环之后的代码,执行下一次循环
参考文章
作者 | 文章名称 |
---|---|
韩顺平 | Java零基础学Java【完整笔记】 |