Go Language Chapter 2 Loop Structure (for Loop)

Go language chapter 2 (for loop)

Summary : There is no while loop construct in Go language. However, similar functionality can be achieved with for loops and break statements.
First of all, if you don't have a compiler, you can type code through this URL: Lightly

for

In the Go language, a for loop is a construct used to repeatedly execute a block of code. Its basic syntax is as follows:

for 初始化语句; 条件表达式; 后置语句 {
    
    
    // 循环体
}

Among them, the initialization statement is executed before the first loop, usually used to initialize variables; the conditional expression is evaluated before each loop, if it is true, then continue to execute the loop body, otherwise jump out of the loop; the post statement is executed in each loop After execution, it is usually used to update variables.

For example, the following code uses a for loop to calculate the sum from 1 to 10:

sum := 0
for i := 1; i <= 10; i++ {
    
    
    sum += i
}
fmt.Println(sum) // 输出 55

In this example, the initialization statement is i := 1to initialize the variable i to 1; the conditional expression is i <= 10to continue executing the loop body as long as i is not greater than 10; the postfix statement is i++to add 1 to i after each loop.

The for loop in Go language can have many variants, for example:

  • The initialization statement and the post statement can be omitted, and only the conditional expression is reserved to realize the function of the while loop.
i := 0
for i < 10 {
    
    
    fmt.Println(i)
    i++
}
  • All statements can be omitted to realize the function of infinite loop.
for {
    
    
    fmt.Println("无限循环")
}
  • You can use the range keyword to traverse collection types such as arrays, slices, and maps.
arr := []int{
    
    1, 2, 3}
for index, value := range arr {
    
    
    fmt.Printf("索引:%d,值:%d\n", index, value)
}

In addition to the above commonly used forms, the for loop of Go language also supports label syntax and continue/break/return statements, which can realize more complex control flow structures. It should be noted that when writing a loop, pay attention to the correctness of the loop condition expression and avoid the occurrence of an infinite loop.

example

1. Print the numbers from 1 to 10

package main

import "fmt"

func main() {
    
    
    for i := 1; i <= 10; i++ {
    
    
        fmt.Println(i)
    }
}

2. Calculate the sum from 1 to 100

package main

import "fmt"

func main() {
    
    
    sum := 0
    for i := 1; i <= 100; i++ {
    
    
        sum += i
    }
    fmt.Println("1到100的和为:", sum)
}

3. Print the ninety-nine multiplication table

package main

import "fmt"

func main() {
    
    
    for i := 1; i <= 9; i++ {
    
    
        for j := 1; j <= i; j++ {
    
    
            fmt.Printf("%d*%d=%d ", j, i, i*j)
        }
        fmt.Println()
    }
}

4. Using a for loop to iterate through an array

package main

import "fmt"

func main() {
    
    
    arr := []int{
    
    1, 2, 3}
    for i := 0; i < len(arr); i++ {
    
    
        fmt.Printf("索引:%d,值:%d\n", i, arr[i])
    }
}

Guess you like

Origin blog.csdn.net/qq_51447496/article/details/130107587