switch分支语句-全
# 一、程序的流程结构
程序的流程控制结构一共有三种:顺序结构、选择结构、循环结构.
# 二、条件语句
# 2.1.switch语句:"开关"
- switch是一个条件语句,它计算表达式并将其与可能匹配的列表进行比较,并根据匹配执行代码块.它可以被认为是一种惯用的方式来写多个
if else
子句. switch
语句用于基于不同条件执行不同动作,每一个case
分支都是唯一的,从上直下逐一测试,直到匹配为止.switch
语句执行的过程从上至下,直到找到匹配项,匹配项后面也不需要再加break
.- 而如果switch没有表达式,它会匹配
true
Go
里面switch
默认相当于每个case
最后带有break
,匹配成功后不会自动向下执行其他case
,而是跳出整个switch
, 但是可以使用fallthrough
强制执行后面的case
代码.
变量var1
可以是任何类型,而val1
和val2
则可以是同类型的任意值.类型不被局限于常量或整数,但必须是相同的类型;或者最终结果为相同类型的表达式.
可以同时测试多个可能符合条件的值,使用逗号分割它们,例如:case val1, val2, val3.
switch var1 {
case val1:
...
case val2:
...
default:
...
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 示例代码1
package main
import "fmt"
func main() {
/* 定义局部变量 */
var grade string = "B"
var marks int = 90
switch marks {
case 90: grade = "A"
case 80: grade = "B"
case 50,60,70 : grade = "C" //case 后可以由多个数值
default: grade = "D"
}
switch {
case grade == "A" :
fmt.Printf("优秀!\n" )
case grade == "B", grade == "C" :
fmt.Printf("良好\n" )
case grade == "D" :
fmt.Printf("及格\n" )
case grade == "F":
fmt.Printf("不及格\n" )
default:
fmt.Printf("差\n" );
}
fmt.Printf("你的等级是 %s\n", grade );
}
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
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
# 代码实例2
package main
import "fmt"
func main() {
/*
switch语句:
语法结构:
switch 变量名 {
case 数值1: 分支1
case 数值2: 分支2
case 数值3: 分支3
......
default:
最后一个分支
}
注意事项:
1.switch可以作用在其他类型上,case后的数值必须和switch作用的变量类型一致.
2.case是无序的
3.case后的数值是唯一的
4.default语句是可选的操作
*/
num := 5
switch num {
case 1:
fmt.Println("第一季度")
case 2:
fmt.Println("第二季度")
case 3:
fmt.Println("第三季度")
case 4:
fmt.Println("第四季度")
default:
fmt.Println("数据有误")
}
//模拟计算器
num1 := 0
num2 := 0
oper := ""
fmt.Println("请输入一个整数: ")
fmt.Scanln(&num1)
fmt.Println("请再次输入一个整数: ")
fmt.Scanln(&num2)
fmt.Println("请输入一个操作: +,-,*,/")
fmt.Scanln(&oper)
switch oper {
case "+":
fmt.Printf("%d + %d = %d\n", num1, num2, num1+num2)
case "-":
fmt.Printf("%d - %d = %d\n", num1, num2, num1-num2)
case "*":
fmt.Printf("%d * %d = %d\n", num1, num2, num1*num2)
case "/":
fmt.Printf("%d / %d = %d\n", num1, num2, num1/num2)
}
fmt.Println("main...over...")
}
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 代码实例3
package main
import "fmt"
func main() {
/*
1.switch的标注写法:
switch 变量{
case 数值1: 分支1
case 数值2: 分支2
...
default:
最后一个分支
}
2.省略switch后的变量,相当于直接操作在true上
switch { //true
case true:
case false:
}
3.cae后可以同时跟随多个数值
switch 变量 {
case 数值1,数值2,数值3:
case 数值4,数值5:
}
4.switch后可以多一条初始化语句
switch 初始化语句;变量{
}
*/
switch {
case true:
fmt.Println("true")
case false:
fmt.Println("false")
}
/*
成绩:
[0-59],不及格
[60,69],及格
[70,79],中
[80,89],良好
[90,99],优秀
*/
score := 88
switch {
case score >= 0 && score < 60:
fmt.Println(score, "不及格")
case score >= 60 && score <= 69:
fmt.Println(score, "及格")
case score >= 70 && score <= 79:
fmt.Println(score, "中")
case score >= 80 && score <= 89:
fmt.Println(score, "良好")
case score >= 90 && score <= 99:
fmt.Println(score, "优秀")
}
fmt.Println("-----------------------")
latter := "A"
switch latter {
case "A", "E", "I", "O", "U":
fmt.Println(latter, "是元音")
case "M", "N":
fmt.Println("M或N...")
default:
fmt.Println("其他...")
}
/*
一个月的天数
1,3,5,7,8,10,12
31
4,6,9,11
30
2: 29/28
*/
month := 0
fmt.Println("请输入一个月份:")
fmt.Scanln(&month)
day := 0
year := 2022
switch month {
case 1, 3, 5, 7, 8, 10, 12:
day = 31
case 4, 6, 9, 11:
day = 30
case 2:
if year%400 == 0 || year%4 == 0 && year%100 != 0 {
day = 29
} else {
day = 28
}
default:
fmt.Println("月份有误")
}
fmt.Printf("%d 年 %d 月 的天数是: %d\n", year, month, day)
fmt.Println("-----------------------------")
switch language := "golang"; language {
case "golang":
fmt.Println("Go语言...")
case "java":
fmt.Println("Java语言...")
case "python":
fmt.Println("Python语言...")
}
}
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 2.2.break与fallthrough
break: 可以使用在
switch
中,也可以使用在fo
r循环中;强制结束case
语句,从而结束switch
分支fallthrough: 用于穿透
switch
(如需贯通后续的case
,就添加fallthrough
)
package main
import "fmt"
func main() {
/*
switch中的break和fallthroungh语句
break: 可以使用在switch中,也可以使用在for循环中、
强制结束case语句,从而结束switch分支
fallthrough: 用于穿透switch
当switch中某个case匹配成功之后,就执行该case语句
如果遇到fallthrough,那么后面禁令的case,无需匹配,执行穿透执行.
fallthrough应该运维某个case的最后一行
*/
n := 2
switch n {
case 1:
fmt.Println("我是熊大")
fmt.Println("我是熊大")
fmt.Println("我是熊大")
case 2:
fmt.Println("我是熊二")
fmt.Println("我是熊二")
break //用于强制结束case,意味着switch被强制结束
fmt.Println("我是熊二")
case 3:
fmt.Println("我是光头强")
fmt.Println("我是光头强")
fmt.Println("我是光头强")
}
fmt.Println("------------------")
m := 2
switch m {
case 1:
fmt.Println("第一季度")
case 2:
fmt.Println("第二季度")
fallthrough
case 3:
fmt.Println("第三季度")
fallthrough
case 4:
fmt.Println("第四季度")
}
fmt.Println("main...over...")
}
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
switch
的注意事项
case
后的常量值不能重复case
后可以有多个常量值fallthrough
应该是某个case
的最后一行.如果它出现在中间的某个地方,编译器就会抛出错误.
# 2.3.Type Switch
switch
语句还可以被用于type-switch
来判断某个interface
变量中实际存储的变量类型.
switch x.(type){
case type:
statement(s);
case type:
statement(s);
/* 你可以定义任意个数的case */
default: /* 可选 */
statement(s);
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
package main
import "fmt"
func main() {
var x interface{}
switch i := x.(type) {
case nil:
fmt.Printf(" x 的类型 :%T",i)
case int:
fmt.Printf("x 是 int 型")
case float64:
fmt.Printf("x 是 float64 型")
case func(int) float64:
fmt.Printf("x 是 func(int) 型")
case bool, string:
fmt.Printf("x 是 bool 或 string 型" )
default:
fmt.Printf("未知型")
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
运行结果:
x 的类型 :<nil>
1
上次更新: 2024/04/09, 16:48:42
- 01
- AWS NAT-NetWork-Firwalld配置(一)04-09
- 02
- AWS NAT-NetWork-Firwalld配置(二)04-09
- 03
- kubernetes部署minio对象存储01-18