当前位置:网站首页>Introduction to go language (V) -- branch statement
Introduction to go language (V) -- branch statement
2022-06-11 19:10:00 【AI Xing】
Branch statement
if-else sentence
Code form
if Boolean expression {
// Code block
}else{
// Code block
}
go The branch statements in are the same as C Very similar in language , Just remove the brackets , Special attention is paid to if It can only be followed by Boolean expressions, not like C Same use 0 He Fei 0 To represent the truth value
Basic example
Here we borrow fmt Medium Scan To get the input of the keyboard to branch the input numbers and 0 The relationship between , The code is as follows
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)
}
}
It can be seen that three times of operation are input respectively 9,-8,0 The results are different 
First assign values and then judge examples
if-else Branch statements can also be assigned before judgment
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 sentence
go Medium switch And C It's very similar to , But we should pay attention to some of his characteristics
1. The default execution is to a case ends , No need to add break, If you want to execute the following... In sequence case You need to add... At the end fallthrough
2.case The judgment in can be a value , Multiple values or Boolean expressions
Single value switch Example
Except you don't have to add break Extraneous and C Almost the same
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")
}
}

Multivalued switch Example
Enter the month and pass switch Print the number of days corresponding to the month
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")
}
}

Boolean expression switch Example
We use Boolean expression to judge the grade of students
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")
}
}

边栏推荐
- Understand how to get started with machine learning to quantify transactions?
- Visual slam lecture notes-10-2
- 【Multisim仿真】利用运算放大器产生方波、三角波发生器
- Web3游戏:游戏体验的探寻与重塑
- KMP! You deserve it!!! Run directly!
- 疫情下远程办公沟通心得|社区征文
- User group actions
- collect. stream(). Use of the collect() method
- Record the phpstudy configuration php8.0 and php8.1 extension redis
- cf:A. Print a Pedestal (Codeforces logo?)【简单遍历模拟】
猜你喜欢

cf:A. Print a Pedestal (Codeforces logo?)【简单遍历模拟】

Record the phpstudy configuration php8.0 and php8.1 extension redis

The US inflation rate reached a 41 year high of 8.6%! High inflation fever? The stock and encryption markets fell first!

2022各大厂最新总结的软件测试宝典,看完不怕拿不到offer

Teach you how to learn the first set and follow set!!!! Hematemesis collection!! Nanny level explanation!!!

On the translation of rich text storage database format

cf:F. Shifting String【字符串按指定顺序重排 + 分组成环(切割联通分量) + 各组最小相同移动周期 + 最小公倍数】

程序员10年巨变,一切都变了又好像没变...

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

ASEMI的MOS管25N120在不同应用场景的表现
随机推荐
On high availability architecture
一款自适应的聊天网站-匿名在线聊天室PHP源码
Web3 Games: exploring and reshaping the game experience
Performance of MOS transistor 25n120 of asemi in different application scenarios
给你一个项目,你将如何开展性能测试工作?
《经济学人》:WTO MC12重启 数字经济成为全球经济复苏和增长的核心引擎
软件开发的整体流程
Analysis of runtime instantiation of XML view root node in SAP ui5
cf:A. Print a Pedestal (Codeforces logo?) [simple traversal simulation]
The US inflation rate reached a 41 year high of 8.6%! High inflation fever? The stock and encryption markets fell first!
In 2023, the MPAcc of School of management of Xi'an Jiaotong University approved the interview online in advance
视觉SLAM十四讲笔记-10-2
ASEMI的MOS管24N50参数,24N50封装,24N50尺寸
【信号去噪】基于非线性滤波器实现语音自适应去噪附matlab代码
求数据库设计毕业信息管理
On the translation of rich text storage database format
leetcode:剑指 Offer 59 - II. 队列的最大值[deque + sortedlist]
I don't want to open an account online. Is it safe to open an account online?
kubernetes 二进制安装(v1.20.15)(九)收尾:部署几个仪表盘
Gmail: how do I recall an outgoing message?