当前位置:网站首页>Go语言入门(五)——分支语句
Go语言入门(五)——分支语句
2022-06-11 19:06:00 【艾醒】
分支语句
if-else语句
代码形式
if 布尔表达式 {
//代码块
}else{
//代码块
}
go中的分支语句与C语言中很像,只不过去掉了括号,特别要注意的是if后面只能加布尔表达式而不能像C一样用0和非0来表示真值
基本示例
这里我们借用了fmt中的Scan来获取键盘的输入来进行分支判断输入的数字与0的关系,代码如下
package main
import "fmt"
func main(){
var a int
fmt.Scan(&a);
if a < 0{
fmt.Printf("%v is smaller than 0\n", a)
}else if a > 0{
fmt.Printf("%v is bigger than 0\n", a)
}else{
fmt.Printf("%v is equal with 0\n", a)
}
}
可以看出运行三次分别输入9,-8,0得到的结果是不一样的
先赋值后判断示例
if-else分支语句也可以先赋值再判断
package main
import "fmt"
func main(){
if a:=-1;a < 0{
fmt.Printf("%v is smaller than 0\n", a)
}else if a > 0{
fmt.Printf("%v is bigger than 0\n", a)
}else{
fmt.Printf("%v is equal with 0\n", a)
}
}

switch语句
go中的switch与C中的很像,但是要注意他的几点特性
1.默认执行到一个case就结束,不需要加break,如果要顺序执行下面的case需要在末尾加上fallthrough
2.case中的判断可以是一个值,多个值或者布尔表达式
单值switch示例
除了不用加break之外和C基本一致
package main
import "fmt"
func main(){
var m int
fmt.Scan(&m)
switch m{
case 1:
fmt.Printf("1\n")
case 2:
fmt.Printf("2\n")
case 3:
fmt.Printf("3\n")
default:
fmt.Printf("Error\n")
}
}

多值switch示例
输入月份后通过switch打印月份对应的天数
package main
import "fmt"
func main(){
var m int
fmt.Scan(&m)
switch m{
case 1, 3, 5, 7, 8, 10, 12:
fmt.Printf("31\n")
case 2:
fmt.Printf("28\n")
case 4, 6, 9, 11:
fmt.Printf("30\n")
default:
fmt.Printf("Error\n")
}
}

布尔表达式switch示例
我们以学生成绩评级为例使用布尔表达式来判断
package main
import "fmt"
func main(){
var score int
fmt.Scan(&score)
switch {
case score >= 90:
fmt.Printf("A\n")
case score >= 80 && score < 90:
fmt.Printf("B\n")
case score >= 70 && score < 80:
fmt.Printf("C\n")
default:
fmt.Printf("D\n")
}
}

边栏推荐
- 对‘g2o::VertexSE3::VertexSE3()’未定义的引用
- 视觉SLAM十四讲笔记-10-1
- 电子商务(njupt)
- KMP!你值得拥有!!! 直接运行直接跑!
- 制造出静态坦克
- Uploading and downloading of necessary files in development
- 《经济学人》:WTO MC12重启 数字经济成为全球经济复苏和增长的核心引擎
- [solution] codeforces round 798 (Div. 2)
- The US inflation rate reached a 41 year high of 8.6%! High inflation fever? The stock and encryption markets fell first!
- leetcode:66. add one-tenth
猜你喜欢
![Leetcode: sword finger offer 59 - ii Maximum value of queue [deque + sortedlist]](/img/6b/f2e04cd1f3aaa9fe057c292301894a.png)
Leetcode: sword finger offer 59 - ii Maximum value of queue [deque + sortedlist]

记录一下phpstudy配置php8.0和php8.1扩展redis

一款自适应的聊天网站-匿名在线聊天室PHP源码

手把手教你学会FIRST集和FOLLOW集!!!!吐血收藏!!保姆级讲解!!!

【信号去噪】基于FFT和FIR实现信号去噪附matlab代码
![[video denoising] video denoising based on salt with matlab code](/img/79/e1f8255061a342e02232ac1e393217.png)
[video denoising] video denoising based on salt with matlab code

Uni app Muke hot search project (I) production of tabbar
![[image denoising] image denoising based on Markov random field with matlab code](/img/ef/d28b89a47723b43705fca07261c958.png)
[image denoising] image denoising based on Markov random field with matlab code
![Cf:g. count the trains [sortedset + bisect + simulation maintaining strict decreasing sequence]](/img/0b/1d3cd06e3d593a997a993a4d96e441.png)
Cf:g. count the trains [sortedset + bisect + simulation maintaining strict decreasing sequence]

Key contents that wwdc22 developers need to pay attention to
随机推荐
Gmail: how do I recall an outgoing message?
【图像去噪】基于马尔可夫随机场实现图像去噪附matlab代码
Bottomsheetdialog usage details, setting fillet, fixed height, default full screen, etc
Teach you how to learn the first set and follow set!!!! Hematemesis collection!! Nanny level explanation!!!
【视频去噪】基于SALT实现视频去噪附Matlab代码
让我们的坦克欢快的动起来吧
公共字段自动填充,你了解吗
全国院校MBA、EMBA、MPA、MEM、提前面试(预面试)时间批次已出(持续更新中)-文都管联院
在 SAP Kyma 上部署一个 Go MSSQL API Endpoint
Do you know that public fields are automatically filled in
更换目标检测的backbone(以Faster RCNN为例)
The US inflation rate reached a 41 year high of 8.6%! High inflation fever? The stock and encryption markets fell first!
疫情下远程办公沟通心得|社区征文
今天睡眠质量记录60分
电子商务(njupt)
[Multisim Simulation] using operational amplifier to generate sawtooth wave
開發中必備的文件的上傳與下載
给你一个项目,你将如何开展性能测试工作?
Cool visualization tool: first introduction to processing
一款自适应的聊天网站-匿名在线聊天室PHP源码