Go for loop

Summary: in this tutorial, you will learn how to use a Go for loop to repeatedly run a code block.

The for loop statement allows you to run a block of code repeatedly. There are some variants of the for loop statement.

Basic Go for loop statement

Here’s the basic syntax of the for loop statement:

for initialization; condition; post {
    // loop body
}Code language: JavaScript (javascript)

In this syntax, the for loop consists of three components:

  • initialization statement: Go executes this statement once at the beginning of the loop. Typically, you use this part to declare and initialize a loop control variable.
  • condition: Go evaluates the condition expression before each iteration of the loop. If the condition is true, Go executes the loop body. If it is false, Go terminates the loop. The condition determines whether the loop continues or not.
  • post statement: Go executes the post statement at the end of each iteration, after executing the loop body. Typically, you use the post statement to update the loop control variable declared in the initialization statement.

The following flowchart illustrates how the Go for loop works:

Go for loop

For example, the following shows how to use the basic for loop statement to display 4 numbers from 1 to 4 on the screen:

package main

import "fmt"

func main() {
    for i := 1; i < 5; i++ {
        fmt.Println(i)
    }
}Code language: Go (go)

Output:

1
2
3
4

How it works.

  • Step 1. Initialization: Declare the loop control variable i and initialize its value to 1: i := 1
  • Step 2. Condition: Evaluate the condition i < 5. If it is true, execute the loop body, otherwise, terminate it.
  • Step 3. Execute loop body: Print the current value of the loop control variable (i)
  • Step 4. Post statement: Increment i by one after each iteration.
  • Step 5. Repeat the steps 2 to 4 until the condition i < 5 evaluates to false.

Go does not support while and do while loop in other programming languages such as Java and C#. Instead, you can use the for loop as the while and do while loop.

Using for loop as a while loop

The following syntax allows you to execute a block of code as long as a condition is true:

for condition {
}Code language: Go (go)

It’s like a while loop in other programming languages such as Java or C#.

The following flowchart illustrates how the for condition works:

Go do while loop

For example, the following uses a for loop to display five numbers from 1 to 4 on the screen:

package main

import "fmt"

func main() {
    index := 1
    for index < 5 {
        fmt.Println(index)
        index++
    }
}Code language: Go (go)

How it works.

First, declare a variable index and initialize its value to 1.

index := 1Code language: Go (go)

Second, execute a block of code within the curly braces ({}) as long as the expression index < 5 is true:

for index < 5 {
   // ...
}Code language: Go (go)

Third, display the value of the index and increase its value by one in each iteration:

fmt.Println(index)
index++Code language: Go (go)

When the index is 5, the condition index < 5 becomes false, the loop is terminated and the control is passed to the statement after the for loop.

Infinite loops

If you omit the condition, you’ll have an infinite loop:

for {
}Code language: Go (go)

To exit a loop based on a condition, you can use the break statement:

for {
   if condition {
       break
   }
}Code language: JavaScript (javascript)

The following example shows how to use a for loop without a condition to display five numbers from 1 to 5 on the screen:

package main

import "fmt"

func main() {
    index := 1
    for {
        fmt.Println(index)
        index++
        if index > 5 {
            break
        }
    }
}Code language: Go (go)

How it works.

First, declare a variable index and initialize its value to 1:

index := 1Code language: Go (go)

Second, display the index and increase the index‘s value one in each iteration:

fmt.Println(index)
index++Code language: Go (go)

Third, exit the loop if the index is greater than 5.

if index > 5 {
    break
}Code language: Go (go)

After five iterations, the index is 6, which makes the expression index > 5 true, and the break statement is reached which terminates the loop.

Summary

  • Use the for loop to execute a block of code a specified number of times.
  • Use the for condition {} to execute a code block repeatedly as long as a condition is true.
  • Use the for {} to create an infinite loop.
  • Use the break statement to terminate a loop immediately.
Was this tutorial helpful?