Sequential control
Branch control
Cycle control
Sequential control
Step by step from top to bottom
Branch control
Branch control is to give Cheng Xi a choice to execute
Single branch
Double branch
Multi branch
Single branch control
//if conditional expression{ //} func main() { var age int fmt.Println("Please enter age") fmt.Scanln(&age) if age>18{ fmt.Println("adult") } }
Dual branch control
if Conditional expression { Execute code block 1 }else{ Execute code block 2 }
Case:
seek
The solution of the equation ax^2+bx+c=0, a, B and C are the parameters of the function respectively
If: B ^ 2-4ac > 0, there are two solutions; b^2-4ac=0, then there is a solution; B ^ 2-4ac < 0, there is no solution;
Tip 1:
x_1 = (-b+sqrt(b^2-4ac))/2a
x_2 = (-b-sqrt(b^2-4ac))/2a
Tip 2:
math.Sqrt(num); You can find the square root. You need to introduce math package
Test data: 3100,6
switch branch control
There is no bireak in go
There can be multiple case s in go, separated by commas
case\switch is followed by an expression (constant, variable, function with return value)
The value type of the expression after case must be consistent with that of the switch expression
default is not required
The switch can also be completed without an expression. The type is if else
Switch penetration fallthrough, also known as switch penetration. Adding fallthrough after case will continue to execute the next case (only one layer can be penetrated by default)
switch determines the type of variable
var key byte fmt.Scanf("%c",&key) switch key { case 'a':fmt.Println("1") case 'b':fmt.Println("2") case 'c':fmt.Println("3") default: fmt.Println("0") } switch test(key)+1 case 1,2,3,4
Without expression
//Without expression var age int = 10 switch { case age==90: fmt.Println(1) case age==80: fmt.Println(2) default: fmt.Println(0) } //case judgment on range mirroring Case age>10 && age<20: fmt.Println(0) //The switch is followed by a declared variable and ends with a semicolon switch age := 10;{ ... }
switch penetration
var num int =10 switch num { case 10: fmt.Println(1) fallthrough case 20: fmt.Println(2) case 30: fmt.Println(3) } //10 //20
Judge variable type
var x interface{} var y = 10.0 x = y switch i := x.(type) { case nil: fmt.Printf("%T", i) case int: fmt.Printf("int") case float64: fmt.Printf("float64") case func(int) float64: fmt.Printf("func(int)") case bool, string: fmt.Printf("bool perhaps string") } //float64
Lowercase to uppercase
var s byte fmt.Println("Please enter") fmt.Scanf("%c", &s) switch s { case 'a': fmt.Println("A") case 'b': fmt.Println("B") case 'c': fmt.Println("C") case 'd': fmt.Println("D") default: fmt.Println("other") }
example
//Judge whether you pass or not var score float64 fmt.Println("Please enter") fmt.Scanln(&score) switch int(score / 60) { case 1: fmt.Println("pass") case 0: fmt.Println("fail,") default: fmt.Println("Be honest") }
For
Syntax format
//for-demo1 for i := 0; i < 10; i++ { fmt.Println("test") } Loop variable initialization, loop condition, loop variable iteration
The second way to write
k := 1 for k <= 10 { fmt.Println("test") k++ }
The third way to write
k := 1 for { if k <= 10 { fmt.Println("test") } else { break } k++ }
String traversal range
//Traditional way var str string = "hello golang" for i := 0; i < len(str); i++ { fmt.Printf("%c\n", str[i]) } //range var str string = "hello golang" for k, v := range str { fmt.Printf("k=%d,v=%c\n", k, v) }
However, there will be errors in Chinese character traversal. Traversal is based on bytes. A Chinese character has three bytes in utf8
Solution: str is converted to [] run slice, and range can be traversed directly
var str string = "i am in Shenzhen" str1 := []rune(str) for i := 0; i < len(str1); i++ { fmt.Printf("%c\n", str1[i]) } for k, v := range str { fmt.Printf("k=%d,v=%c\n", k, v) }
example
var max uint64 = 100 var count uint64 = 0 var sum uint64 = 0 var i uint64 = 1 for ; i <= max; i++ { if i/9 == 0 { count++ sum += i } } fmt.Printf("count=%v sum=%v\n", count, sum)
Implementation of while and do while
Multiple cycles
After the memory cycle is completed, it will enter the outer cycle
Cycle times: inner cycle + outer cycle
Example: print multiplication table
var num int = 9 for i := 1; i <= num; i++ { for j := 1; j <= i; j++ { fmt.Printf("%v * %v = %v\t", j, i, j*i) } fmt.Println() }
Break
Abort the current for loop or switch statement
var count int = 0 for { rand.Seed(time.Now().UnixNano()) n := rand.Intn(100) + 1 fmt.Println(n) count++ if n == 99 { break } } fmt.Println(count)
- break will jump out of the latest loop and enter the previous loop by default
- In a multi-level nested loop, a label indicates which level of statement to terminate
label1: for i := 0; i < 3; i++ { for j := 0; j < 12; j++ { if j == 2 { //break break label1 } fmt.Println(j) } } }
continue
End this cycle and enter the next cycle
The label can indicate which layer of loop to skip
label1: for i := 0; i < 3; i++ { for j := 0; j < 5; j++ { if j == 2 { continue label1 } fmt.Println(j) } } // 01 01 01 for i := 0; i < 3; i++ { for j := 0; j < 5; j++ { if j == 2 { continue } fmt.Println(j) } } // 0,1,3,4, 0,1,3,4, 0,1,3,4
Jump control statement goto
goto statement can unconditionally transfer to the specified line in the program
goto statements are usually used together with conditional statements, which can be used to realize conditional transfer, jump out of the loop body and other functions
goto is not recommended to avoid confusion of program flow
var n int = 20 fmt.Println(1) fmt.Println(2) if n > 11 { goto lable1 } fmt.Println(3) lable1: fmt.Println(4) fmt.Println(5)
Jump out of control statement return
In a method or function, it means to jump out of the method or function