当前位置:网站首页>Go branch and loop
Go branch and loop
2022-07-02 03:54:00 【UPythonFish】
List of articles
go Branches and loops
if-else sentence
go Of if-else Judgment statement , and js It's like , The grammar is as follows
if condition {
} else if condition {
} else {
}
When condition When the conditions are met, execute { } The code in , Not satisfied with the implementation of else Back
func main() {
number := 55
if number >=90 && number <= 100 {
// The beginning of braces should be on the same line as the keyword
// go By default, no line ends with ; An error will be reported if the line changes
fmt.Println(" good ")
} else if number >= 60 && number <= 90 {
// else if There must be conditions behind
fmt.Println(" good ")
} else {
// else There can be no conditions behind
fmt.Println(" fail, ")
}
}
for Loop statement
for yes Go The only loop statement in language .Go There are no other languages in the language, such as python Medium while loop .
complete for loop grammar
for initialisation; condition; post {
}
The initialization statement is executed only once . After loop initialization , Cycle conditions will be checked . If the result of the condition is true , be {} The inner loop body will execute , Then perform post sentence .post Statement will execute after each successful loop iteration . In execution post After the statement , The conditions will be checked again . If true, Then the loop will continue to execute , otherwise for The cycle will end .( Translation notes : This is typical for Loop three expressions , The first is the initialization expression or assignment statement ; The second is the expression of cyclic condition determination ; The third is the loop variable correction expression , That is to say post )
for Cyclic 5 Method of use
1. Index based loop
func main() {
// Loop printing 0-9
for i := 0; i < 10; i++ {
fmt.Println(i)
}
}
2. External initialization
func main(){
i := 0 // i The scope of is expanded
for ; i<10; i++{
// ; Don't omit
fmt.Println(i)
}
}
3. The loop variable correction expression is written internally
func main(){
i := 0 // i The scope of is expanded
for ; i<10; {
// Write only conditions It can be omitted ;
fmt.Println(i)
i++
}
}
func main(){
i := 0 // i The scope of is expanded
for i<10 {
// Write only conditions It can be omitted ;
fmt.Println(i)
i++
}
}
4. Omit the cycle condition
func main(){
for ; ; {
// here ; You can omit it
fmt.Println(" Dead cycle ")
}
}
func main(){
for {
// here ; You can omit it
fmt.Println(" Dead cycle ")
break // You can add break Keyword to terminate , There are also continue keyword , Skip the following code directly , Execute next cycle
}
}
5. Iteration based loop
func main(){
var a = [3]int[1, 25, 34] // Array
for i,value := range a {
fmt.Println(i) // Indexes
fmt.Println(value) // value
}
}
go Kind of for Same cycle Like other languages , It can also be executed A nested loop , The following list is printed 9 x 9 Multiplication table
func main() {
for i := 1; i < 10; i++ {
for j := 1; j <= i; j++ {
fmt.Printf("%d x %d = %d ", j, i, i*j)
}
fmt.Println()
}
}
switch sentence
switch It's a conditional statement , Used to compare the value of an expression with a list of possible matches , And execute the corresponding code block according to the matching situation . It can be thought of as an alternative to multiple if else The common way of clause .
// 1 Usage 1
name:="tom"
switch name {
case "lxx":
fmt.Println("lxx")
case "tom":
fmt.Println("tom")
}
// Usage mode 2 Without conditions
name:="tom"
switch {
case name=="lxx":
fmt.Println("lxx")
case name=="tom":
fmt.Println("tom")
}
// Usage mode 3 Multiple conditions
name:="tom"
switch name {
case "lxx","jason": // perhaps
fmt.Println("lxx")
case "tom","marry":
fmt.Println("tom")
}
name := "tom"
switch {
case name == "lxx" || name == "jason": // perhaps
fmt.Println("lxx")
case name == "tom", name == "marry":
fmt.Println("tom")
}
// 4 The way 4 ,default Use , Does not meet all case after , perform
name:="tom"
switch name {
case "lxx":
fmt.Println("lxx")
case "tom":
fmt.Println("tom")
default:
fmt.Println(" I don't know who it is ")
}
// 5 fallthrough Use Just see this keyword , Unconditionally execute the next case
name:="lxx"
switch name {
case "lxx":
fmt.Println("lxx")
fallthrough
case "tom":
fmt.Println("tome")
fallthrough
default:
fmt.Println(" I don't know who it is ")
}
matters needing attention :
- case Duplicate options are not allowed after , If repeated, an error will be reported
边栏推荐
- Influence of air resistance on the trajectory of table tennis
- 《西线无战事》我们才刚开始热爱生活,却不得不对一切开炮
- The 10th Blue Bridge Cup single chip microcomputer provincial competition
- JVM knowledge points
- Flutter中深入了解MaterialApp,常用属性解析
- Demonstration description of integrated base scheme
- Oracle viewing locked tables and unlocking
- leetcode-1380. Lucky number in matrix
- Object oriented thinking
- Imageai installation
猜你喜欢

go 包的使用

Didi open source Delta: AI developers can easily train natural language models

潘多拉 IOT 开发板学习(RT-Thread)—— 实验1 LED 闪烁实验(学习笔记)

蓝桥杯单片机省赛第十一届第一场

Interface debugging tool simulates post upload file - apipost

What do you know about stock selling skills and principles

Learn more about materialapp and common attribute parsing in fluent

The 10th Blue Bridge Cup single chip microcomputer provincial competition

蓝桥杯单片机省赛第十二届第一场

The 11th Blue Bridge Cup single chip microcomputer provincial competition
随机推荐
【直播回顾】战码先锋首期8节直播完美落幕,下期敬请期待!
Basic operations of MySQL database (based on tables)
蓝桥杯单片机省赛第六届
潘多拉 IOT 开发板学习(RT-Thread)—— 实验1 LED 闪烁实验(学习笔记)
蓝桥杯单片机省赛第十一届第二场
Qt插件之Qt Designer插件实现
The first practical project of software tester: web side (video tutorial + document + use case library)
【leetcode】74. Search 2D matrix
Basic syntax of unity script (6) - specific folder
Jetpack之LiveData扩展MediatorLiveData
JVM知识点
WPViewPDF Delphi 和 .NET 的 PDF 查看组件
leetcode-1380. Lucky number in matrix
The 7th Blue Bridge Cup single chip microcomputer provincial competition
【DesignMode】建造者模式(Builder model)
微信小程序中 在xwml 中使用外部引入的 js进行判断计算
近段时间天气暴热,所以采集北上广深去年天气数据,制作可视化图看下
Is the product of cancer prevention medical insurance safe?
5g era is coming in an all-round way, talking about the past and present life of mobile communication
JVM knowledge points