The program control of go is roughly divided into three if for case statements
1. if loop
There can be any number of else if between if else statements.
The order of condition judgment is from top to bottom. If the if or else if condition determines that the result is true, the corresponding code block is executed. If no condition is true, the else code block is executed.
Grammatical structure
if condition{
}else if condition{
}else{
}
Definition list
package main import "fmt" func iftest() { score :=61 if score > 60{ fmt.Println("Pass the grade") } else if score >90{ fmt.Println("Excellent performance") }else if score<0 { fmt.Println("Score cannot be negative") }else if score < 60 { fmt.Println("Unqualified results") } else{ fmt.Println("Illegal achievements") } } func main() { iftest() // output //Pass the grade }
2. for loop
There is only one go loop called for
Grammatical structure
for initialisation; condition; post {
}
The initialization statement is executed only once. After the loop is initialized, the loop conditions are checked. If the condition evaluates to true, the body of the loop within {} executes,
The post statement is then executed. The post statement will execute after each successful loop iteration. After the post statement is executed, the condition is checked again.
If true, the loop will continue, otherwise the for loop will terminate.
List
package main import "fmt" func fortest1() { for i :=1;i <=10;i++{ fmt.Println(i) } } // output 1 2 3 4 5 6 7 8 9 10 //In go, the break statement abruptly terminates the for loop before the execution is completed, and then the program will start the next line under the for loop //Code execution begins for i:=2; i<=20 ;i+=2{ if i >8{ break } fmt.Printf("%d",i) } fmt.Printf("\nline after for loop") // Output when the value of i is greater than 8, fmt.Printf("%d",i), execution is terminated, and the last fmt.Print is executed 2468 line after for loop // continue continue Statement to jump out for The current loop in the loop, //All for loop statements after the continue statement will not be executed in this loop. The loop will continue to execute in the next loop. for i :=1;i<=10;i++{ if i%2 ==0{ continue } fmt.Printf("%d",i) } } // Output, when not output for 0 13579 func main() { fortest1() } // Infinite loop statement of go for { fmt.Println("Hello World") }
3. switch statement
The switch statement is used to perform different actions based on different conditions. Each case is unique. It is tested from top to bottom until it matches
The switch statement runs from top to bottom until a match is found, and there is no need to add break after the match
By default, the case has a break statement at the end of the switch. After the match is successful, no other case will be executed. You can use fallthrough
The syntax structure is as follows
switch var1 {
case val1:
...
case val2:
...
default:
...
}
package main import "fmt" func switchtest1() { var scores int = 90 switch scores{ case 90: fmt.Println("very good score") fallthrough case 80: fmt.Println("good score") case 60: fmt.Println("just ok") } } //Output. When adding fallthrough under the condition of case 90, the condition of case 80 will also be output good score very good score func main() { switchtest1() }