YouTip LogoYouTip

Go For Loop

Go Language Loop StatementsGo Language Loop Statements

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

Syntax

Go's for loop has three forms, and only one of them uses semicolons.

Same as C's for:

for init; condition; post { }

Same as C's while:

for condition { }

Same as C's for(;;):

for { }
  • init: Usually an assignment expression to initialize the control variable;
  • condition: A relational or logical expression, the loop control condition;
  • post: Usually an assignment expression to increment or decrement the control variable.

The execution process of the for statement is as follows:

  1. First, assign an initial value to expression 1;
  2. Determine if the assignment expression init satisfies the given condition. If its value is true and the loop condition is met, execute the statements inside the loop body, then execute post, enter the second loop, and check the condition again. Otherwise, if the condition's value is false and the condition is not met, terminate the for loop and execute the statements outside the loop body.

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

for key, value := range oldMap { newMap = 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 diagram:

Image 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 result is:

55

The init and post parameters are optional. We can omit them directly, similar to the While statement.

The following example calculates the value of sum added to 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 way of writing is also possible, more like the While statement form

    for sum <= 10 {

        sum += sum

    }

    fmt.Println(sum)

}

The output result is:

1616

Infinite loop:

Example

package main

import "fmt"

func main() {

    sum := 0

    for {

        sum++ // Infinite loop

    }

    fmt.Println(sum) // Cannot output

}

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

For-each range loop

This format of loop can iterate over strings, arrays, slices, etc., to output elements.

Example

package main

import "fmt"

func main() {

    strings := []string{"google", ""}

    for i, s := range strings {

        fmt.Println(i, s)

    }

    numbers := int{1, 2, 3, 5}

    for i, x := range numbers {

        fmt.Printf("The %d-th x value = %dn", i, x)

    }

}

The output of the above example is:

0 google 1  The 0-th x value = 1
The 1-th x value = 2
The 2-th x value = 3
The 3-th x value = 5
The 4-th x value = 0
The 5-th x value = 0

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

Example

package main

import "fmt"

func main() {

    map1 := make(mapfloat32)

    map1 = 1.0

    map1 = 2.0

    map1 = 3.0

    map1 = 4.0

    // Read key and value

    for key, value := range map1 {

        fmt.Printf("key is: %d - value is: %fn", key, value)

    }

    // Read key

    for key := range map1 {

        fmt.Printf("key is: %dn", key)

    }

    // Read value

    for _, value := range map1 {

        fmt.Printf("value is: %fn", value)

    }

}

The output of 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

← Go FunctionsAndroid Intents Filters β†’