流程控制语句
2022-11-07 大约 4 分钟
# 判断语句
# if
public class Demo {
public static void main(String[] args) {
int a = 10;
int b = 20;
// 变量使用if判断
if (a == b) {
System.out.println("a等于b");
}
int c = 10;
if (a == c) {
System.out.println("a等于c");
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# if...else
public class Demo {
public static void main(String[] args) {
// 判断给定的数据是奇数还是偶数
// 定义变量
int a = 1;
if (a % 2 == 0) {
System.out.println("a是偶数");
} else {
System.out.println("a是奇数");
}
System.out.println("结束");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# if..else if...else
public class Demo {
public static void main(String[] args) {
int x = 5;
int y;
if (x >= 3) {
y = 2 * x + 1;
} else if (x >= 1 && x < 3) {
y = 2 * x;
} else {
y = 2 * x - 1;
}
System.out.println("y的值是:" + y);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 选择语句
# switch
public class Demo {
public static void main(String[] args) {
//定义变量,判断是星期几
int weekday = 6;
//switch语句实现选择
switch (weekday) {
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;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# case的穿透性
在switch语句中,如果case的后面不写break,将出现穿透现象,也就是不会在判断下一个case的值,直接向后运行,直到遇到break,或者整体switch结束。
public class Demo {
public static void main(String[] args) {
int i = 5;
switch (i) {
case 0:
System.out.println("执行case0");
break;
case 5:
System.out.println("执行case5");
case 10:
System.out.println("执行case10");
default:
System.out.println("执行default");
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
上述程序中,执行case5后,由于没有break语句,程序会一直向后走,不会在判断case,也不会理会break,直接运行完整体switch。由于case存在穿透性,因此初学者在编写switch语句时,必须要写上break。
相关信息
switch语句中,表达式的数据类型,可以是byte,short,int,char,enum(枚举),JDK7后可以接收字符串。
# 循环语句
# for
public class Demo {
// 使用循环,计算1-100之间的偶数和
public static void main(String[] args) {
// 1.定义一个初始化变量,记录累加求和,初始值为0
int sum = 0;
// 2.利用for循环获取1‐100之间的数字
for (int i = 1; i <= 100; i++) {
// 3.判断获取的数组是奇数还是偶数
if (i % 2 == 0) {
// 4.如果是偶数就累加求和
sum += i;
}
}
// 5.循环结束之后,打印累加结果
System.out.println("sum:" + sum);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# while
public class Demo {
public static void main(String[] args) {
// while循环实现打印10次HelloWorld
// 定义初始化变量
int i = 1;
// 循环条件<=10
while (i <= 10) {
System.out.println("HelloWorld");
// 步进
i++;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# do...while
// 输出10次HelloWorld
public class Demo {
public static void main(String[] args) {
int x = 1;
do {
System.out.println("HelloWorld");
x++;
} while (x <= 10);
}
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
do...while循环的特点:无条件执行一次循环体,即使我们将循环条件直接写成false,也依然会循环一次。这样的 循环具有一定的风险性,因此初学者不建议使用do...while循环。
public class demo01DataType {
public static void main(String[] args) {
do {
System.out.println("无条件执行一次");
} while (false);
}
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# 循环语句的区别
- for 和 while 的小区别:
- 控制条件语句所控制的那个变量,在for循环结束后,就不能再被访问到了,而while循环结束还可以继 续使用,如果你想继续使用,就用while,否则推荐使用for。原因是for循环结束,该变量就从内存中消 失,能够提高内存的使用效率。
- 在已知循环次数的时候使用推荐使用for,循环次数未知的时推荐使用while。
# 跳出语句
# break
- 使用场景:终止switch或者循环
- 在选择结构switch语句中。
- 在循环语句中。
- 离开使用场景的存在是没有意义的。
public class Demo {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
// 需求:打印完两次HelloWorld之后结束循环
if (i == 3) {
break;
}
System.out.println("HelloWorld" + i);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# continue
使用场景:结束本次循环,继续下一次的循环
public class demo01DataType {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
//需求:不打印第三次HelloWorld
if (i == 3) {
continue;
}
System.out.println("HelloWorld" + i);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 扩展知识点
# 死循环
循环中的条件永远为true,死循环的是永不结束的循环。例如:while(true){}。
# 嵌套循环
指一个循环的循环体是另一个循环。比如for循环里面还有一个for循环,就是嵌套循环。总共的循环次数=外循环次数*内循环次数。
// 使用嵌套循环,打印5*8的矩形
public class demo01DataType {
public static void main(String[] args) {
//5*8的矩形,打印5行*号,每行8个
//外循环5次,内循环8次
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 8; j++) {
//不换行打印星号
System.out.print("*");
}
//内循环打印8个星号后,需要一次换行
System.out.println();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15