2.24. Go language for loop

发布时间 :2023-10-12 23:00:10 UTC      

for loop is a loop control structure that can execute a specified number of loops.

2.24.1. Grammar #

There are three forms of For loops in the Go language, with only one using a semicolon.

Just like the for in C language:

for init; condition; post { }

Same as while in C:

for condition { }

Same as C’s for(;;) :

for { }
  • init is generally an assignment expression that assigns an initial value to a control variable;

  • condition relational or logical expressions, loop control conditions;

  • post is generally an assignment expression that increments or decrements the control variable.

for statement execution process is as follows:

  • 1.Match the expression first 1 assign initial value;

  • 2.Discriminant assignment expression init whether the given condition is satisfied, if its value is true and the loop condition is satisfied, the statement inside the loop is executed and then executed post enter the second cycle and then judge condition ; otherwise judge condition if the value is false, if the condition is not met, it will be terminated. for loop to execute the extra corporeal statement.

The format of the range loop for for can be applied to slice map , arrays, strings, and so on. The format is as follows:

for key, value := range oldMap {
    newMap[key] = value
}

for statement syntax flow is shown in the following figure:

Image0

2.24.2. Example #

Calculate the sum of numbers from 1 to 10 :

Example #

package main
import "fmt"
func main() {
        sum := 0
        for i := 0; i <= 10; i++ {
                sum += i
        }
        fmt.Println(sum)
}

The output is as follows:

55

init and post parameter is optional, and we can omit it directly, similar to While statement.

The following example is found in the sum less than 10 time calculation sum self-added value:

Example #

package main
import "fmt"
func main() {
        sum := 1
        for ; sum <= 10; {
                sum += sum
        }
        fmt.Println(sum)
        // It can also be written this way,
        more like a While statement form
        for sum <= 10{
                sum += sum
        }
        fmt.Println(sum)
}

The output is as follows:

16
16

Infinite loop:

Example #

package main
import "fmt"
func main() {
        sum := 0
        for {
            sum++ // Endless cycle
        }
        fmt.Println(sum) // Unable to output
}

To stop an infinite loop, press in the command window ctrl-c .

For-each range cycle

Loops in this format can iterate over strings, arrays, slices, and so on.

Example #

package main
import "fmt"
func main() {
        strings := []string{"google", "runoob"}
        for i, s := range strings {
                fmt.Println(i, s)
        }
        numbers := [6]int{1, 2, 3, 5}
        for i,x:= range numbers {
                fmt.Printf("Value of x in position% d=%d\\n", i,x)
        }
}

The output of the above instance is as follows:

0 Google
1 runoob
The value of position 0 x=1
The value of the first digit x=2
The value of the second digit x=3
The value of the third digit x=5
The value of the 4th digit x=0
Value of 5th digit x=0

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.