Flow control of Go language

Keywords: Go

The process control of the program determines how the program is executed. There are three main process controls:

  • Sequential control
  • Branch control
  • Cycle control

1, Sequential control

Sequence control is the simplest process control, which emphasizes that the code is executed from top to bottom without any sequence and jump. Note that variables in the Go language use forward references. For example:

func main() {
    var a int = 1 // Declare variable a
    var b int = a  //Use variables a
    fmt.Println(b)

}        

  It is correct to define variables before using them. On the contrary, it is wrong to use a before defining a.

2, Branch control

  Branch control is to allow programs to execute selectively, which is mainly divided into three categories:

  • Single branch
  • Double branch
  • Multi branch

(1) Single branch

1. Basic grammar

if <condition> {
    // Execute code block
  
}

2. Instance

package main

import "fmt"

func main() {
    a := 10

    if a > 5 {
        fmt.Println("Greater than 5")
    }

}

(2) Dual branch control

1. Basic grammar

if <condition> {
    // Execute code block
  
} else {
   // Execute code block
}

2. Instance

package main

import "fmt"

func main() {
    a := 10

    if a > 5 {
        fmt.Println("Greater than 5")
    } else {
        fmt.Println("Up to 5")
    }

}

(3) Multi branch control

1. Basic grammar

if <condition1> {
    // Execute code block 1
  
} else if <condition2> {
   // Execute code block 2

} else {
   // Execute code block 3

}

First, judge whether condition 1 is true. If it is true, execute code block 1; If condition 1 is false, judge whether condition 2 is true. If condition 2 is true, execute code block 2; If all the conditions are not true, execute the code block in else.

Multiple branches can only have one execution entry.

2. Instance

package main

import "fmt"

func main() {
    a := 10

    if a < 5 {
        fmt.Println("Less than 5")
    } else if a >= 5 && a < 10{
        fmt.Println("Greater than or equal to 5 and less than 10")
    } else {
        fmt.Println("Greater than 10")

    }

}

(4) Nested branch

In one branch structure, another complete branch structure is completely nested. The inner branch structure is called inner branch, and the outer branch structure is called outer branch.

  • Basic grammar

if <condition1> {
     
    if <condition1.1> {
  
    } else {

       }
    
}  
...

3, switch branch control

switch statements are used to perform different actions based on different conditions. Each case branch is unique and tested one by one from top to bottom until it matches. There is no need to add break after the match

1. Basic grammar

package main

import "fmt"

func main() {
    
    switch <condition> {
        case <condition1>,<condition2>...:
            // Code block 1

        case <condition3>,<condition4>...:
            // Code block 2
...
default: // Code block 3 } }
  • The execution process of switch is to restrict the condition, obtain the value, compare it with the case expression, match it if it is equal, then execute the corresponding case code block, and then exit the switch control
  • If the value of the switch expression does not match any case expression successfully, execute the default code block and exit the switch process control after execution
  • In Go language, there can be multiple expressions after case, separated by commas
  • break is not required in the case code block in Go language, because it exists by default. When the case code block is executed, it will exit the switch control

2. Instance

 

package main

import "fmt"

func main() {
    
    score := 90

    switch {
        case score >= 90:
            fmt.Println("excellent...")

        case score >= 80 && score < 90:
            fmt.Println("good...")
        default:
            fmt.Println("commonly...")
    }


}

  In addition, you can directly define variables after switch:

package main

import "fmt"

func main() {
    
    switch score := 90; {
        case score >= 90:
            fmt.Println("excellent...")

        case score >= 80 && score < 90:
            fmt.Println("good...")
        default:
            fmt.Println("commonly...")
    }


}

If you add fallthrough to the case code block, it will penetrate, and the next case will be executed. By default, it will penetrate one layer.

package main

import "fmt"

func main() {
    
    switch score := 90; {
        case score >= 90:
            fmt.Println("excellent...")
            fallthrough // excellent...good...
case score >= 80 && score < 90:
            fmt.Println("good...")
            
        default:
            fmt.Println("commonly...")
    }


}

3. Comparison between switch and if

  • If there are few specific values to judge, it is recommended to use the switch statement
  • If the interval is judged as bool type, it is recommended to use if statement

4, Cycle control

Go language cycle control - iveBoy - blog Park (cnblogs.com)

 

Posted by RedMaster on Mon, 08 Nov 2021 14:59:56 -0800