条件和循环语句
1、顺序结构
按照顺序执行下来的
public class Test01 {
public static void main(String[] args) {
System.out.println("aaa");
System.out.println("bbb");
System.out.println("ccc");
}
}
输出结果为:
aaa
bbb
ccc
Process finished with exit code 0
2、分支结构
2.1 if 语句
if后面括号中的条件为真,就执行下面的代码
//if 语句
int num = 10;
//条件为真,输出hello
if(num < 20){
System.out.println("hello");
}
输出结果:
hello
Process finished with exit code 0
if后面括号中的条件为假,就不会执行下面的代码
//条件为假不执行后面的语句,什么都不输出
int num = 10;
if (num > 20) {
System.out.println("hello");
}
输出结果:
Process finished with exit code 0
if 搭配 else 使用
//搭配 else 使用
int num = 10;
if (num > 20) {
//表达式为真,执行该语句
System.out.println("hello");
}else{
//表达式为假,执行该语句
//显然条件不满足,输出结果为 world
System.out.println("world");
}
输出结果:
world
Process finished with exit code 0
多个分支的情况:
按顺序执行,看符合哪一个代码块的条件,就执行哪一个代码块的代码
int score = 75;
//输出为 中等
if (score >= 90) {
System.out.println("优秀");
} else if (score >= 80) {
System.out.println("良好");
} else if (score >= 70) {
System.out.println("中等");
} else if (score >= 60) {
System.out.println("及格");
}else {
System.out.println("不及格");
}
输出结果:
中等
Process finished with exit code 0
不加大括号,只能写一条语句,还是加上大括号比较好,防止代码出错,提高代码的可读性
else与最近的 if 配对
int a = 10;
int b = 20;
if(a ==10)
if(b == 30)
System.out.println("aaa");
else
System.out.println("bbb");
输出结果:
bbb
Process finished with exit code 0
2.2 switch语句
switch后面的括号中只能是:整数、枚举、字符、字符串
每个case中的break一般不能省略,否则代码就会顺序执行直到遇到下一个break
//switch语句
//打印今天是星期几
int day = 5;
switch(day){
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四");
break;
case 5:
System.out.println("星期五");
break;
case 6:
System.out.println("星期六");
break;
case 7:
System.out.println("星期日");
break;
default:
System.out.println("输入错误");
break;
}
输出结果:
星期五
Process finished with exit code 0
switch的局限性
1、switch的()中只能有限的几种类型
2、break语句一旦遗漏,程序的执行可能出错
3、switch很难表达一些复杂的逻辑
4、switch虽然支持嵌套,但是很丑
3、循环语句
3.1 while循环
while()括号中的循环条件为true就执行循环语句,否则就结束循环
//打印1-10
int num = 1;
while(num <= 10){
System.out.print(num + " ");
num += 1;
// num++;
// num = num + 1;
}
输出结果:
1 2 3 4 5 6 7 8 9 10
Process finished with exit code 0
3.2 for循环
for(表达式1;表达式2;表达式3;)
表达式1 :用于初始化循环变量
表达式2:循环条件
表达式3:更新循环变量
//for 循环
//打印 1-10
for(int num = 1; num <= 10; num++){
System.out.print(num + " ");
}
输出结果:
1 2 3 4 5 6 7 8 9 10
Process finished with exit code 0
break和continue的区别
break表示结束整个循环,continue表示结束本次循环
//吃包子
int n = 1;
while (n <= 5) {
if (n == 3) {
//有个虫
n++;
continue;
}
System.out.println("吃第" + n +"个包子");
n++;
}
输出结果:
吃第1个包子
吃第2个包子
吃第4个包子
吃第5个包子
Process finished with exit code 0
可以看到continue是结束了本次循环进入到下一个循环中
int m = 1;
while(m <= 5){
if (m == 3) {
//有半个虫子
break;
}
System.out.println("吃第" + m +"个包子");
m++;
}
输出结果:
吃第1个包子
吃第2个包子
Process finished with exit code 0
break是结束整个循环代码不会再进行下一个循环了
3.3 do while 循环
//使用do while 打印1-10
int num = 1;
do{
System.out.print(num + " ");
num++;
}while(num <= 10);
输出结果:
1 2 3 4 5 6 7 8 9 10
Process finished with exit code 0
先执行一次do中的代码,再判断while中的条件是否符合
4、利用断点调试代码
- 打断点
- 点击Debug
- 分析执行过程