当前位置:网站首页>【愚公系列】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结构是可以结合来使用的,可以根据具体的问题具体分析,灵活来应用。
边栏推荐
- 基于OpenCV的轮廓检测(1)
- LeetCode 第二十八天
- Six determination methods of Worthington peroxidase activity
- Plato Farm有望通过Elephant Swap,进一步向外拓展生态
- [tree chain dissection] template question
- C语言力扣第43题之字符串相乘。优化竖式
- flask_ Reqparse parser inheritance in restful
- 分享当下人生——一个高中毕业生在中央电视台的六星期实习经历
- 安装umi4阻碍一天的问题解决了
- Function pointer and callback function
猜你喜欢

222. 完全二叉树的节点个数

MySQL Chinese failure

Digital analog 1232

Worthington papain dissociation system solution

Program to change the priority of the process in LabVIEW

About the solution of using hyperbeach to appear /bin/sh: 1: packr2: not found

The fifth strong network cup national network security challenge Title reappearance (with title attachment, detailed explanation)

Connman introduction

一维数组的应用

数字孪生应用及意义对电力的主要作用,概念价值。
随机推荐
明汯投资裘慧明:长期优异超额的背后考验的是团队的投研能力和策略的完整性
C language introduction practice (12): find the value of natural constant e
FastBoot brush machine
typescript ts 基础知识之接口、泛型
Implementation of API short message gateway based on golang
使用redis c库,异步内存泄露的问题
小于等于K的最大子数组累加和
Insert pictures and videos in typera
面试题:String类中三种实例化对象的区别
Cocos game practice-04-collision detection and NPC rendering
【安卓小叙】Kotlin多线程编程(一)
Briefly sort out the dualpivotquicksort
基于OpenCV的轮廓检测(2)
Redis source code learning (33), command execution process
Introduction to redis
DNS record type and explanation of related terms
Permutation and binary (Ji, DA) (day 84)
How to interact with the server when the client sends an SQL message
Mysql database related operations
Share the current life -- a six week internship experience of a high school graduate in CCTV