# 5, Process control

Posted by MesaFloyd on Thu, 03 Mar 2022 22:40:04 +0100

1. Sequential control

2. Branch control

3. Cycle control

4. ### Sequential control

Step by step from top to bottom

1. ### Branch control

Branch control is to give Cheng Xi a choice to execute

1. Single branch

2. Double branch

3. Multi branch

4. #### Single branch control

```//if conditional expression{
//}

func main() {
var age int
fmt.Scanln(&age)

if age>18{
}
}
```
1. #### 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 1. ### switch branch control

2. There is no bireak in go

3. There can be multiple case s in go, separated by commas

4. case\switch is followed by an expression (constant, variable, function with return value)

5. The value type of the expression after case must be consistent with that of the switch expression

6. default is not required

7. The switch can also be completed without an expression. The type is if else

8. 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)

9. 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.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.Scanln(&score)
switch int(score / 60) {
case 1:
fmt.Println("pass")
case 0:
fmt.Println("fail,")
default:
fmt.Println("Be honest")
}
```
1. ### 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++
}
```
1. #### 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)
```
1. #### Implementation of while and do while  1. #### Multiple cycles

2. After the memory cycle is completed, it will enter the outer cycle

3. 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()
}
```
1. #### 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)
```
1. break will jump out of the latest loop and enter the previous loop by default
2. 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)
}
}
}
```
1. #### continue

2. End this cycle and enter the next cycle

3. 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
```
1. #### Jump control statement goto

2. goto statement can unconditionally transfer to the specified line in the program

3. 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

4. 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)
```
1. #### Jump out of control statement return

In a method or function, it means to jump out of the method or function

Topics: Go