2.24. Go language for loop

发布时间 : 2025-10-25 13:32:56 UTC      

Page Views: 10 views

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 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。