[Go] Go Language Tutorial--GO Conditions and Loop Statements (8)

Past tutorials:

GO language conditional statement

Conditional statements require the developer to specify one or more conditions, and determine whether to execute the specified statement by testing whether the condition is true, and execute another statement if the condition is false.

The following diagram shows the structure of a conditional statement in a programming language:
insert image description here

Go language conditional statement

Go language provides the following conditional judgment statements:

statement describe
if statement An if statement consists of a Boolean expression followed by one or more statements.
if...else statement An optional else statement can be used after the if statement, and the expression in the else statement is executed if the Boolean expression is false.
if nested statement You can embed one or more if or else if statements within an if or else if statement.
switch statement The switch statement is used to perform different actions based on different conditions.
select statement The select statement is similar to the switch statement, but select randomly executes a runnable case. If there are no cases to run, it will block until there are cases to run.

Note: Go does not have a ternary operator, so ?:conditional judgment in the form of is not supported.

Go language loop statement

In many practical problems, there are many regular repeated operations, so some statements need to be executed repeatedly in the program.

The following is a flowchart of a loop program in most programming languages:

insert image description here

Go language provides the following types of loop processing statements:

cycle type describe
for loop Repeat block of statements
loop nesting Nest one or more for loops within a for loop

loop control statement

The loop control statement can control the execution process of the statement in the loop body.

The GO language supports the following loop control statements:

control statement describe
break statement Often used to interrupt the current for loop or jump out of a switch statement
continue statement Skip the remaining statements of the current loop and continue to the next loop.
goto statement Transfer control to the marked statement.

Infinite loop

If the conditional statement in the loop is never false, an infinite loop will be performed. We can execute an infinite loop by setting only one conditional expression in the for loop statement:

example

package main

import "fmt"

func main() {
    
    
    for true  {
    
    
        fmt.Printf("这是无限循环。\n");
    }
}

Guess you like

Origin blog.csdn.net/u011397981/article/details/131590680