c语言case后无条件,9. Go 语言流程控制:switch-case

Hi,大家好,我是明哥。

在自己学习 Golang 的这段时间里,我写了详细的学习笔记放在我的个人微信公众号 《Go编程时光》,对于 Go 语言,我也算是个初学者,因此写的东西应该会比较适合刚接触的同学,如果你也是刚学习 Go 语言,不防关注一下,一起学习,一起成长。

我的在线博客:http://golang.iswbm.com

我的 Github:github.com/iswbm/GolangCodingTime

Go里的流程控制方法还是挺丰富,整理了下有如下这么多种:

if - else 条件语句

switch - case 选择语句

for - range 循环语句

goto 无条件跳转语句

defer 延迟执行

上一篇讲了 if -else 条件语句,今天先来讲讲 switch - case 选择语句。

0. 语句模型

Go 里的选择语句模型是这样的

switch 表达式 {

case 表达式1:

代码块

case 表达式2:

代码块

case 表达式3:

代码块

case 表达式4:

代码块

case 表达式5:

代码块

default:

代码块

}

拿 switch 后的表达式分别和 case 后的表达式进行对比,只要有一个 case 满足条件,就会执行对应的代码块,然后直接退出 switch - case ,如果 一个都没有满足,才会执行 default 的代码块。

1. 最简单的示例

switch 后接一个你要判断变量 education (学历),然后 case 会拿这个 变量去和它后面的表达式(可能是常量、变量、表达式等)进行判等。

如果相等,就执行相应的代码块。如果不相等,就接着下一个 case。

import "fmt"

func main() {

education := "本科"

switch education {

case "博士":

fmt.Println("我是博士")

case "研究生":

fmt.Println("我是研究生")

case "本科":

fmt.Println("我是本科生")

case "大专":

fmt.Println("我是大专生")

case "高中":

fmt.Println("我是高中生")

default:

fmt.Println("学历未达标..")

}

}

输出如下

我是本科生

2. 一个 case 多个条件

case 后可以接多个多个条件,多个条件之间是 或 的关系,用逗号相隔。

import "fmt"

func main() {

month := 2

switch month {

case 3, 4, 5:

fmt.Println("春天")

case 6, 7, 8:

fmt.Println("夏天")

case 9, 10, 11:

fmt.Println("秋天")

case 12, 1, 2:

fmt.Println("冬天")

default:

fmt.Println("输入有误...")

}

}

输出如下

冬天

3. case 条件常量不能重复

当 case 后接的是常量时,该常量只能出现一次。

以下两种情况,在编译时,都会报错: duplicate case "male" in switch

错误案例一

gender := "male"

switch gender {

case "male":

fmt.Println("男性")

// 与上面重复

case "male":

fmt.Println("男性")

case "female":

fmt.Println("女性")

}

错误案例二

gender := "male"

switch gender {

case "male", "male":

fmt.Println("男性")

case "female":

fmt.Println("女性")

}

4. switch 后可接函数

switch 后面可以接一个函数,只要保证 case 后的值类型与函数的返回值 一致即可。

import "fmt"

// 判断一个同学是否有挂科记录的函数

// 返回值是布尔类型

func getResult(args ...int) bool {

for _, i := range args {

if i < 60 {

return false

}

}

return true

}

func main() {

chinese := 80

english := 50

math := 100

switch getResult(chinese, english, math) {

// case 后也必须 是布尔类型

case true:

fmt.Println("该同学所有成绩都合格")

case false:

fmt.Println("该同学有挂科记录")

}

}

5. switch 可不接表达式

switch 后可以不接任何变量、表达式、函数。

当不接任何东西时,switch - case 就相当于 if - elseif - else

score := 30

switch {

case score >= 95 && score <= 100:

fmt.Println("优秀")

case score >= 80:

fmt.Println("良好")

case score >= 60:

fmt.Println("合格")

case score >= 0:

fmt.Println("不合格")

default:

fmt.Println("输入有误...")

}

6. switch 的穿透能力

正常情况下 switch - case 的执行顺序是:只要有一个 case 满足条件,就会直接退出 switch - case ,如果 一个都没有满足,才会执行 default 的代码块。

但是有一种情况是例外。

那就是当 case 使用关键字 fallthrough 开启穿透能力的时候。

s := "hello"

switch {

case s == "hello":

fmt.Println("hello")

fallthrough

case s != "world":

fmt.Println("world")

}

代码输出如下:

hello

world

需要注意的是,fallthrough 只能穿透一层,意思是它只给你一次再判断case的机会,不管你有没有匹配上,都要退出了。

s := "hello"

switch {

case s == "hello":

fmt.Println("hello")

fallthrough

case s == "xxxx":

fmt.Println("xxxx")

case s != "world":

fmt.Println("world")

}

输出如下,并不会输出 world(即使它符合条件)

hello

xxxx

系列导读

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值