当前位置:网站首页>[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 .
边栏推荐
- LeetCode 第二十八天
- Leetcode- > 2-point search and clock in (3)
- 明汯投资裘慧明:长期优异超额的背后考验的是团队的投研能力和策略的完整性
- 222. Number of nodes of complete binary tree
- Review in the sixth week
- Cocos game practice-04-collision detection and NPC rendering
- 代码回滚,你真的理解吗?
- Six determination methods of Worthington peroxidase activity
- 一文读懂 | 数据中台如何支撑企业数字化经营
- 【无标题】JDBC连接数据库读超时
猜你喜欢

深圳家具展首日,金可儿展位三大看点全解锁!

Function pointer and callback function

Contour detection based on OpenCV (1)

Plato Farm有望通过Elephant Swap,进一步向外拓展生态

Duplicate disc: what are the basic attributes of an image? What do you know about images? What are the parameters of the image

分享当下人生——一个高中毕业生在中央电视台的六星期实习经历

C. Cypher

The fifth strong network cup national network security challenge Title reappearance (with title attachment, detailed explanation)

Message queue learning -- Concepts

Specific use of multithreading
随机推荐
There is no problem reading from flick CDC to mysql8 and mysql5. What should I do?
SkyWalking分布式系统应用程序性能监控工具-中
Installation and use of anti-virus software ClamAV
注释有点好玩哦
一文读懂 | 数据中台如何支撑企业数字化经营
The application and significance of digital twins are the main role and conceptual value of electric power.
Duplicate disc: what are the basic attributes of an image? What do you know about images? What are the parameters of the image
MySQL has a nonexistent error
Binary tree (Beijing University of Posts and Telecommunications machine test questions) (day85)
Detailed tutorial of typera
Maximum continuous subsequence (day 77)
Regression testing: meaning, challenges, best practices and tools
大家有遇到CDC读MySQL字段不全的情况吗?怎么处理的?
Practical application of digital twins: smart city project construction solution
代码回滚,你真的理解吗?
Deployment of ruoyi's environment and operation of the system
深圳家具展首日,金可儿展位三大看点全解锁!
Record the problem of PHP program accessing system files incorrectly
Principle understanding and application of hash table and consistent hash
Ming min investment Qiu Huiming: behind the long-term excellence and excess, the test is the team's investment and research ability and the integrity of strategy