Flow Control Statements in Go Language

Table of contents

flow control statement

if statement

if···else statement

switch statement

for loop

break statement

continue statement

goto statement


flow control statement

if statement

In the Go language, an execution statement can be added after the if, and the execution statement can also be used to judge the if condition. Its syntax format is:

if a := condition(); a == nil {
    //代码块
}

a is assigned the result of the condition() method, and then let a and nil be judged. If it is true, the code in the code block will be executed, otherwise it will not be executed, and the scope of a will only be within the scope of this statement.

if···else statement

If there is a second branch, you can add an else statement and another code block on top of the code above. The code in this code block will only be executed if the condition is not met. The two code blocks in if and else are independent branches, and only one of them can be executed. The syntax format is:

if condition {
    //代码块
} else {
    //代码块
}

switch statement

The switch statement is used for multi-branch selection, and the switch statement of Go language is more general than that of other languages. The value of the expression does not need to be a constant, or even an integer. Its syntax format is:

switch expression {
    case value1:
        //代码块
    case value2:
        //代码块
    default:
        //代码块
}

expression is an expression, and value1, value2, etc. are possible values. If expression is equal to one of the values, the corresponding code block will be executed. If none of them match, the default code block will be executed.

In the Go language, case and case are independent code blocks. By default, each case comes with a break, which does not need to be written by the user. If you do not need to automatically terminate each case after execution, you can use fallthrough to force the execution of case codes that have not been executed.

d := 2
switch d {
    case 1:
        fmt.Print("星期一")
    case 2:
        fmt.Print("星期二")
        fallthrough
    case 3:
        fmt.Print("星期三")
    default:
        //代码块
}

The output result is Tuesday and Wednesday

for loop

The for statement is a statement used for looping, and its grammatical format is:

for initialization; condition; post {
    //代码块
}

initialization is an initialization statement, condition is a loop condition, and post is a statement to be executed after each loop ends. When the condition is true, the code in the code block will be executed, then the post statement will be executed, and then it will be judged whether the condition is true again, and so on until the condition is false.

Infinite loop:

for {
    //代码块
}

If expression 2 is omitted, then this is an infinite loop, and the code in the code block will always be executed unless the break statement is used to break out of the loop.

break statement

The break statement in the Go language can jump out of the code block of the loop or switch, and can also add a label after the break to exit the code block specified by a certain label. Its syntax format is:

for condition {
    //代码块
    if condition2 {
        break
    }
}

When condition2 is met, it will jump out of the loop.

continue statement

The continue statement is used to skip the current loop and enter the next loop, and can be used in the for loop. Its syntax format is:

for condition {
    //代码块
    if condition2 {
        continue
    }
}

When condition2 is met, the current cycle will be skipped and the next cycle will be entered.

goto statement

In the Go language, goto can be used to jump labels and jump between codes. Its syntax format is:

label:
//代码块
goto label

Where label is an identifier, which can be any string. When the goto statement is executed, it will jump to the specified label to execute the code. Note that the use of goto statements can easily lead to poor readability and maintainability of the code, and should be avoided as much as possible.

Guess you like

Origin blog.csdn.net/weixin_71646897/article/details/130551146