当前位置:网站首页>【愚公系列】2022年7月 Go教学课程 018-分支结构之switch
【愚公系列】2022年7月 Go教学课程 018-分支结构之switch
2022-07-27 02:30:00 【愚公搬代码】
前言
Switch在一些计算机语言中是保留字,其作用大多情况下是进行判断选择。以Go语言来说,switch(开关语句)常和case default一起使用。
一、switch语句
1.switch基本使用
Go 语言中 switch 中的每一个 case 都是独立代码块,无需通过 break 语句跳出代码块,以避免执行到下一个 case 代码块。
相关案例:
package main
import "fmt"
func main() {
num := 1
switch num {
case 1:
fmt.Println("小愚公")
case 2:
fmt.Println("大愚公")
default:
fmt.Println("不是愚公")
}
}

上面代码中,case 对整型 num 进行判断,若条件符合,则打印相应的字符串。另外,还使用了 default 分支,default 分支表示若条件均不符合,则执行改代码块。
2.一分支多值
package main
import "fmt"
func main() {
num := 1
switch num {
case 1, 2:
fmt.Println("都是愚公")
}
}

3.分支表达式
package main
import "fmt"
func main() {
num := 1
switch {
case num > 0 && num < 2:
fmt.Println("猜对了是愚公")
}
}

4.fallthrough关键字
Go 语言中 swtich 语句中的 case 均为独立代码块,执行完成后,不会继续执行下一个 case,fallthrough就可以实现继续执行。
package main
import "fmt"
func main() {
num := 1
switch {
case num > 0:
fmt.Println("愚公一号")
fallthrough
case num < 2:
fmt.Println("愚公二号")
}
}

二、相关案例
1.成绩打分
package main
import "fmt"
func main() {
//考试成绩大于等于90输出A,大于等于80输出B,大于等于70输出C,大于等于60输出D,不及格输出E
var score float64
fmt.Println("请输入考试成绩:")
fmt.Scan(&score)
switch {
case score >= 90:
fmt.Println("A")
case score >= 80:
fmt.Println("B")
case score >= 70:
fmt.Println("C")
case score >= 60:
fmt.Println("D")
default:
fmt.Println("E")
}
}

总结
- 某个case后面跟着的代码执行完毕后,不会再执行后面的case,而是跳出整个switch结构,相当于每个case后面都跟着break(终止),但是如果我们想执行完成某个case后,强制执行后面的case,可以使用fallthrough。
- switch结构和if结构是可以结合来使用的,可以根据具体的问题具体分析,灵活来应用。
边栏推荐
- J-3-point practice in the second game of 2022 Niuke multi school
- Practical application of digital twins: smart city project construction solution
- If the detailed explanation is generated according to the frame code
- Debug mode in pycharm for detailed debugging
- GetObject call timing of factorybean
- 使用redis c库,异步内存泄露的问题
- Leetcode- > 2-point search and clock in (3)
- Introduction to redis
- Redis source code learning (33), command execution process
- connman介绍
猜你喜欢

Contour detection based on OpenCV (1)

About the solution of using hyperbeach to appear /bin/sh: 1: packr2: not found
![[tree chain dissection] template question](/img/6b/7ec6f36d5f2373aee163c2cb766b29.png)
[tree chain dissection] template question

Double disk: the main differences between DFS and BFS, the differences in ideology, and the differences in code implementation

Deployment of ruoyi's environment and operation of the system
![[Android synopsis] kotlin multithreaded programming (I)](/img/04/4349bacbd401868d73a3b05d018b66.png)
[Android synopsis] kotlin multithreaded programming (I)

222. Number of nodes of complete binary tree

Solution to Chinese garbled code in console header after idea connects to database to query data

Six determination methods of Worthington peroxidase activity

代码回滚,你真的理解吗?
随机推荐
unity之二维数组实现正六边形地图
C# 使用SqlSugar Updateable系统报错无效数字,如何解决?求指导!
分析一下CSDN大佬写的CAS,可重入锁, 非公平锁
基于OpenCV的轮廓检测(1)
Share the current life -- a six week internship experience of a high school graduate in CCTV
Function pointer and callback function
Vector to SVG method
Reading notes of Kazuo Inamori's advice to young people
回归测试:意义、挑战、最佳实践和工具
Redis source code learning (33), command execution process
Characteristics and experimental suggestions of abbkine abfluor 488 cell apoptosis detection kit
大家有遇到CDC读MySQL字段不全的情况吗?怎么处理的?
Csu18m91 is used as the master controller of the intelligent scale scheme
深圳家具展首日,金可儿展位三大看点全解锁!
OC message mechanism
一维数组的应用
If the detailed explanation is generated according to the frame code
开机启动流程及营救模式
Double disk: the main differences between DFS and BFS, the differences in ideology, and the differences in code implementation
477-82(236、61、47、74、240、93)