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

边栏推荐
- [image segmentation] image segmentation based on Markov random field with matlab code
- 7-3 combinatorial problems (*)
- E-commerce (njupt)
- ASEMI的MOS管24N50参数,24N50封装,24N50尺寸
- The 2023 MBA (Part-time) of Beijing University of Posts and telecommunications has been launched
- mysql 联合索引和BTree
- 《经济学人》:WTO MC12重启 数字经济成为全球经济复苏和增长的核心引擎
- The Economist: WTO MC12 restarts the digital economy and becomes the core engine of global economic recovery and growth
- I don't want to open an account online. Is it safe to open an account online?
- Gmail: how do I recall an outgoing message?
猜你喜欢

使用图像处理技术和卷积神经网络(CNN)的作物病害检测

Specific methods for JS to realize full screen display

Web3游戏:游戏体验的探寻与重塑

Visual slam lecture notes-10-1
制造出静态坦克

Practice of Flink CDC in Dajian cloud warehouse
Function development of user information management

cf:E. Price Maximization【排序 + 取mod + 双指针+ 配对】

疫情下远程办公沟通心得|社区征文
![[untitled]](/img/02/49d333ba80bc6a3e699047c0c07632.png)
[untitled]
随机推荐
Cf:d. black and white stripe
Record the phpstudy configuration php8.0 and php8.1 extension redis
On the translation of rich text storage database format
Web3游戏:游戏体验的探寻与重塑
懂机器学习如何入门量化交易?
The US inflation rate reached a 41 year high of 8.6%! High inflation fever? The stock and encryption markets fell first!
动态爆炸效果
Let our tanks move happily
cf:A. Print a Pedestal (Codeforces logo?)【简单遍历模拟】
【信号去噪】基于非线性滤波器实现语音自适应去噪附matlab代码
Leetcode: sword finger offer 59 - ii Maximum value of queue [deque + sortedlist]
Deploy a go MSSQL API endpoint on SAP kyma
KMP! You deserve it!!! Run directly!
添加自己喜欢的背景音乐
Cf:b. array determinations
First acquaintance with enterprise platform
Add your favorite background music
[signal denoising] signal denoising based on FFT and fir with matlab code
Pymysql uses cursor operation database method to encapsulate!!!
CMU 15-445 database course lesson 5 text version - buffer pool