banner
音小董

哩哔哩哔

这世上的热闹出自孤单

Chapter 5: Flow Control

Introduction#

Control statement categories:

  1. Sequential structure
  2. Branching structure
  3. Loop structure
    Control Flow.png

Branching Structure#

if branch#

Single branch#

Basic syntax
if condition {
logic code
}
The logic code in {} is only executed when the condition expression is true

  1. () can be added on both sides of the condition expression, but it is recommended not to add them.
  2. There must be a space between If and the condition expression.
  3. {} must be present.
package main  
  
import "fmt"  
  
func main() {  
var a int = 25  
var b int = 15  
if a > b {  
fmt.Println("a is greater than b")  
}
//semicolon can be used after if to include variable definition
if a2 := 20; a2 < 10 {  
fmt.Println("a is less than b")  
} 
}

Result:

a is greater than b

Double branch#

Basic syntax
if condition {
logic code 1
} else {
logic code 2
}
When the condition expression is true, execute logic code 1; otherwise, execute logic code 2

func main() {  
if a := 10; a > 5 {  
fmt.Println("a is greater than 5")  
} else {  
fmt.Println("a is not greater than 5")  
}  
  
if b := 10; b > 15 {  
fmt.Println("b is greater than 15")  
} else {  
fmt.Println("b is not greater than 15")  
}  
}

Result:

a is greater than 5
b is not greater than 5

Multiple branches#

Basic syntax
if condition {
logic code 1
} else {
logic code 2
}
......
else {
logic code n
}
Execute in order. When one condition expression is true, execute the corresponding logic code and end the execution without continuing to execute

package main  
  
import "fmt"  
  
func main() {  
var a int = 50  
if a > 10 {  
fmt.Println("a is greater than 10")  
} else if a > 20 {  
fmt.Println("a is greater than 20")  
} else if a > 30 {  
fmt.Println("a is greater than 30")  
} else if a > 40 {  
fmt.Println("a is greater than 40")  
} else {  
fmt.Println("None of the above")  
}   
}

Result:

a is greater than 10

switch branch#

Basic syntax
switch expression {
case value 1, value 2, ......:
statement block 1
Case value 3, value 4, ......:
statement block 2
......
default:
statement block
}
Execute in order. When one case satisfies the expression, execute the corresponding statement block and end the execution without continuing to execute
Student Scores.png

package main  
  
import (  
"fmt"  
)  
  
func main() {  
var score int = 50  
switch score / 10 {  
case 10:  
fmt.Println("Grade is A")  
case 9:  
fmt.Println("Grade is A")  
case 8:  
fmt.Println("Grade is B")  
case 7:  
fmt.Println("Grade is C")  
case 6:  
fmt.Println("Grade is D")  
case 5:  
fmt.Println("Grade is E")  
case 4:  
fmt.Println("Grade is E")  
case 3:  
fmt.Println("Grade is E")  
case 2:  
fmt.Println("Grade is E")  
case 1:  
fmt.Println("Grade is E")  
case 0:  
fmt.Println("Grade is E")  
default:  
fmt.Println("Invalid score")  
}  
}

Result:

Grade is E

Loop Structure#

for loop#

Basic syntax
for (initial expression; boolean expression; iteration factor){
loop body;
}

package main  
  
import "fmt"  
  
func main() {  
//Sum of 1+2+3+4+5  
//Method 1  
var n1 int = 1  
var n2 int = 2  
var n3 int = 3  
var n4 int = 4  
var n5 int = 5  
var sum1 int  
sum1 += n1  
sum1 += n2  
sum1 += n3  
sum1 += n4  
sum1 += n5  
fmt.Println(sum1)  
//Method 2  
var sum2 int  
for i := 1; i <= 5; i++ {  
sum2 += i  
}  
fmt.Println(sum2)  
}

for range#

Can traverse arrays, slices, strings, maps, and channels

Basic syntax
for key, val := range coll {

}

import "fmt"  
  
func main() {  
var str string = "mojito"  
//for i := 0; i < len(str); i++ {  
// fmt.Printf("%c \n",str[i])  
//}  
for i, value := range str {  
fmt.Printf("Index: %d, Value: %c \n", i, value)  
}  
}

Result:

Index: 0, Value: m
Index: 1, Value: o 
Index: 2, Value: j 
Index: 3, Value: i 
Index: 4, Value: t 
Index: 5, Value: o 

Keywords#

break#

Execute the current loop and then exit the entire loop

package main  package main  
  
import "fmt"  
  
func main() {  
var sum1 int  
for i := 1; i <= 100; i++ {  
sum1 += i  
}  
fmt.Println(sum1)  
  
var sum2 int  
for j := 1; j <= 100; j++ {  
sum2 += j  
if sum2 > 300 {  
break  
}  
}  
fmt.Println(sum2)  
}

Result:

5050
325

continue#

Exit the current loop and continue with the next loop

import "fmt"  
  
func main() {  
//Get numbers divisible by 6 within 1-100  
for i := 1; i <= 100; i++ {  
if i%6 != 0 {  
continue  
}  
fmt.Println(i)  
}  
}

Result:

6
12
18
24
30
36
42
48
54
60
66
72
78
84
90
96

goto#

Jump to the specified line and execute

package main  
  
import "fmt"  
  
func main() {  
if 1 == 1 {  
goto a  
}  
fmt.Println("hello1")  
fmt.Println("hello2")  
fmt.Println("hello3")  
fmt.Println("hello4")  
fmt.Println("hello5")  
fmt.Println("hello6")  
fmt.Println("hello7")  
a:  
fmt.Println("hello8")  
}

Result:

hello8

return#

End the current function without continuing to execute

package main  
  
import "fmt"  
  
func main() {  
for j := 1; j < 10; j++ {  
if j == 7 {  
return  
}  
fmt.Println(j)  
}  
fmt.Println("hello golang")  
}

Result:

1
2
3
4
5
6
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.