当前位置:网站首页>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")
}
}

边栏推荐
- First acquaintance with enterprise platform
- 软件开发的整体流程
- 电子商务(njupt)
- SAP BTP 上 workflow 和 Business Service 的 project 管理
- The US inflation rate reached a 41 year high of 8.6%! High inflation fever? The stock and encryption markets fell first!
- Gmail: how do I recall an outgoing message?
- Complete in-depth learning of MySQL from 0 to 1 -- phase 2 -- basics
- Key contents that wwdc22 developers need to pay attention to
- Flask CKEditor 富文本编译器实现文章的图片上传以及回显,解决路径出错的问题
- 全国院校MBA、EMBA、MPA、MEM、提前面试(预面试)时间批次已出(持续更新中)-文都管联院
猜你喜欢

动态爆炸效果
![[image denoising] impulse noise image denoising based on absolute difference median filter, weighted median filter and improved weighted median filter with matlab code attached](/img/dc/6348cb17ca91afe39381a9b3eb54e6.png)
[image denoising] impulse noise image denoising based on absolute difference median filter, weighted median filter and improved weighted median filter with matlab code attached
![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]
制造出静态坦克

视觉SLAM十四讲笔记-10-2

Uni app Muke hot search project (I) production of tabbar

Flink CDC 在大健云仓的实践

Today's sleep quality record is 60 points

An adaptive chat site - anonymous online chat room PHP source code

全国院校MBA、EMBA、MPA、MEM、提前面试(预面试)时间批次已出(持续更新中)-文都管联院
随机推荐
01. Telecommunications_ Field business experience
【信号去噪】基于非线性滤波器实现语音自适应去噪附matlab代码
7-3 combinatorial problems (*)
美国通胀率8.6%创41年历史新高!通胀高烧不退?股票、加密市场先跌为敬!
cf:A. Print a Pedestal (Codeforces logo?)【简单遍历模拟】
2022-2023年西安交通大学管理学院MEM提前批面试网报通知
MySQL in-depth and complete learning - stage 1 - overview of learning
Given a project, how will you conduct performance testing?
Gmail:如何撤回发出的邮件?
Gmail: how do I recall an outgoing message?
Today's sleep quality record is 60 points
Téléchargement et téléchargement des fichiers nécessaires au développement
MBA, EMBA, MPa, MEM, pre interview (pre interview) time batch of national colleges and universities has been released (continuously updated) - Wendu Management Institute
Tips for using apipost
Flink CDC 在大健云仓的实践
Leetcode: sword finger offer 59 - ii Maximum value of queue [deque + sortedlist]
开发中必备的文件的上传与下载
Cf:f. shifting string [string rearrangement in specified order + grouping into rings (cutting connected components) + minimum same moving cycle of each group + minimum common multiple]
On the translation of rich text storage database format
cf:F. Shifting String【字符串按指定顺序重排 + 分组成环(切割联通分量) + 各组最小相同移动周期 + 最小公倍数】