当前位置:网站首页>[Yugong series] July 2022 go teaching course 018 switch of branch structure
[Yugong series] July 2022 go teaching course 018 switch of branch structure
2022-07-27 04:00:00 【Yugong move code】
List of articles
Preface
Switch In some computer languages, it is a reserved word , Its function is mostly to judge and choose . With Go language ,switch( Switch statement ) Constant harmony case default Use it together .
One 、switch sentence
1.switch Basic use
Go In language switch Each of them case Are independent code blocks , There is no need to pass break Statement jumps out of the code block , To avoid execution to the next case Code block .
Related cases :
package main
import "fmt"
func main() {
num := 1
switch num {
case 1:
fmt.Println(" Little fool ")
case 2:
fmt.Println(" Big fool ")
default:
fmt.Println(" Not Yugong ")
}
}

In the above code ,case On integers num Judge , If the conditions meet , Then print the corresponding string . in addition , Also used. default Branch ,default Branch means if the conditions are not met , Then change the code block .
2. One branch, many values
package main
import "fmt"
func main() {
num := 1
switch num {
case 1, 2:
fmt.Println(" All fools ")
}
}

3. Branch expressions
package main
import "fmt"
func main() {
num := 1
switch {
case num > 0 && num < 2:
fmt.Println(" Guess right, it's Yugong ")
}
}

4.fallthrough keyword
Go In language swtich Statement case Are independent code blocks , After execution , Will not proceed to the next case,fallthrough You can continue to execute .
package main
import "fmt"
func main() {
num := 1
switch {
case num > 0:
fmt.Println(" Yugong No.1 ")
fallthrough
case num < 2:
fmt.Println(" Yugong No. 2 ")
}
}

Two 、 Related cases
1. Score your grades
package main
import "fmt"
func main() {
// The examination result is greater than or equal to 90 Output A, Greater than or equal to 80 Output B, Greater than or equal to 70 Output C, Greater than or equal to 60 Output D, Fail output E
var score float64
fmt.Println(" Please enter the test scores :")
fmt.Scan(&score)
switch {
case score >= 90:
fmt.Println("A")
case score >= 80:
fmt.Println("B")
case score >= 70:
fmt.Println("C")
case score >= 60:
fmt.Println("D")
default:
fmt.Println("E")
}
}

summary
- Some case After the following code is executed , Will not perform the following case, But out of the whole switch structure , It's equivalent to each case Followed by break( End ), But if we want to do something case after , Enforce the following case, have access to fallthrough.
- switch The structure and if Structure can be used in combination , It can be analyzed according to specific problems , Flexible to apply .
边栏推荐
- Csu18m91 is used as the master controller of the intelligent scale scheme
- Is it safe for tongdaxin to open an account
- Machine learning [Matplotlib]
- Record the problem of PHP program accessing system files incorrectly
- Prime factorization -- C (GCC) -- PTA
- The job created by flinksqlclient will disappear after the restart of Flink. Is there any way?
- Redis spike case, learn from Shang Silicon Valley teacher in station B
- 477-82(236、61、47、74、240、93)
- [tree chain dissection] 2022 Hangzhou Electric Multi school 21001 static query on tree
- Debug mode in pycharm for detailed debugging
猜你喜欢

Record the problem of PHP program accessing system files incorrectly

Function pointer and callback function

flask_ Reqparse parser inheritance in restful

Chapter 5 决策树和随机森林实践

复盘:图像有哪些基本属性?关于图像的知识你知道哪些?图像的参数有哪些

C. Cypher

Meta Quest内容生态总监谈App Lab设计初衷

代码回滚,你真的理解吗?

Contour detection based on OpenCV (2)

Plato Farm有望通过Elephant Swap,进一步向外拓展生态
随机推荐
Design method and test method of APP interface use case
Day 28 of leetcode
【无标题】JDBC连接数据库读超时
The fifth strong network cup national network security challenge Title reappearance (with title attachment, detailed explanation)
注释有点好玩哦
Contour detection based on OpenCV (1)
Basic concept and essence of Architecture
Review in the sixth week
Kettle reads file split by line
Member array and pointer in banyan loan C language structure
Function pointer and callback function
LeetCode 第二十八天
【无标题】
DNS record type and explanation of related terms
Interview question: the difference between three instantiated objects in string class
【安卓小叙】Kotlin多线程编程(一)
Two help points distribution brings to merchants
Programming implementation of eight queens
Learning and understanding of four special data types of redis
Characteristics and determination scheme of Worthington pectinase