Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Go for Loops

Go for Loops

Go Language Loop StatementsGo Language Loop Statements

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

Syntax

Go's for loop has 3 forms, only one of which uses semicolons.

Like the for loop in C:

for init; condition; post { }

Like while in C:

for condition { }

Like for(;;) in C:

for { }

The execution process of a for statement is as follows:

The range form of a for loop can iterate over slices, maps, arrays, strings, etc. The format is as follows:

for key, value := range oldMap {

    newMap[key] = value

}

The key and value in the above code can be omitted.

If you only want to read the key, the format is as follows:

for key := range oldMap

Or like this:

for key, _ := range oldMap

If you only want to read the value, the format is as follows:

for _, value := range oldMap

The syntax flow of the for statement is shown in the following figure:

Example

Calculate the sum of the 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:

55

The init and post parameters are optional; we can simply omit them, similar to a while statement.

The following example computes the value of sum after adding itself while sum is less than 10:

Example

package

main

import

"fmt"

func

main

()

{

   sum

:=

1

   

for

;

sum <

=

10

;

{

      sum

+=

sum

   

}

   fmt

.

Println

(

sum

)

   

// This also works and is more like the form of a while statement

   

for

sum <

=

10

{

      sum

+=

sum

   

}

   fmt

.

Println

(

sum

)

}

The output is:



16

16

Infinite loop:

Example

package

main

import

"fmt"

func

main

()

{

   sum

:=

0

   

for

{

      sum

++

// Loop infinitely

   

}

   fmt

.

Println

(

sum

)

// Cannot output

}

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

For-each range loop

This form of loop can iterate over strings, arrays, slices, etc., outputting their elements.

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

(

"The value of x at position %d = %d\n"

,

i

,

x

)

   

}

 

}

The output of running the above example is:

0 google

1 runoob

第 0 位 x 的值 = 1

第 1 位 x 的值 = 2

第 2 位 x 的值 = 3

第 3 位 x 的值 = 5

第 4 位 x 的值 = 0

第 5 位 x 的值 = 0

The range form of a for loop can omit key and value, as in the following example:

Example

package

main

import

"fmt"

func

main

()

{

    map1

:=

make

(

map

[

int

]

float32

)

    map1

[

1

]

=

1

.

0

    map1

[

2

]

=

2

.

0

    map1

[

3

]

=

3

.

0

    map1

[

4

]

=

4

.

0

   

   

// Read key and value

   

for

key

,

value

:=

range

map1

{

      fmt

.

Printf

(

"key is: %d - value is: %f\n"

,

key

,

value

)

   

}

   

// Read key

   

for

key

:=

range

map1

{

      fmt

.

Printf

(

"key is: %d\n"

,

key

)

   

}

   

// Read value

   

for

_

,

value

:=

range

map1

{

      fmt

.

Printf

(

"value is: %f\n"

,

value

)

   

}

}

The output of running the above example is:

key is: 4 - value is: 4.000000

key is: 1 - value is: 1.000000

key is: 2 - value is: 2.000000

key is: 3 - value is: 3.000000

key is: 1

key is: 2

key is: 3

key is: 4

value is: 1.000000

value is: 2.000000

value is: 3.000000

value is: 4.000000

Go Language Loop StatementsGo Language Loop Statements