Loops in Go

Loops: Loops are used for iterating over a sequence such as a range, map, string, or channel. Go has only one looping construct — the for loop — but it can be used in many different ways.

Types of Loops in Go

  1. For loop as a three-component loop
  2. For loop as an infinite loop
  3. For loop as a while loop
  4. Simple range in for loop
  5. For loop for strings
  6. For loop for maps
  7. For loop for channels

1. For Loop as Three-Component Loop

This is the most common form of the for loop in Go. It includes three components — initialization, condition, and post (increment/decrement).

Syntax:

for initialization; condition; post {
    // executable statements
}

Example Program:

package main
import "fmt"

func main() {
    for i := 1; i <= 5; i++ {
        fmt.Println("Count:", i)
    }
}

Output:

Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

2. For Loop as Infinite Loop

When all three components of the for loop are omitted, it becomes an infinite loop. It will continue executing until a break or return statement is encountered.

Syntax:

for {
    // executable statements
}

Example Program:

package main
import "fmt"

func main() {
    i := 0
    for {
        fmt.Println("i =", i)
        i++
        if i == 3 {
            break
        }
    }
}

Output:

i = 0
i = 1
i = 2

3. For Loop as While Loop

Go does not have a separate while loop. A for loop with only a condition acts like a while loop.

Syntax:

for condition {
    // executable statements
}

Example Program:

package main
import "fmt"

func main() {
    i := 1
    for i <= 5 {
        fmt.Println("Value:", i)
        i++
    }
}

Output:

Value: 1
Value: 2
Value: 3
Value: 4
Value: 5

4. Simple Range in For Loop

The range keyword in Go is used to iterate over arrays, slices, or collections.

Syntax:

for i, j := range rangeVariable {
    // executable statements
}

Example Program:

package main
import "fmt"

func main() {
    numbers := []int{10, 20, 30, 40, 50}
    for index, value := range numbers {
        fmt.Printf("Index: %d, Value: %d\n", index, value)
    }
}

Output:

Index: 0, Value: 10
Index: 1, Value: 20
Index: 2, Value: 30
Index: 3, Value: 40
Index: 4, Value: 50

5. Using For Loop for Strings

When you use range with strings, it returns both the index and the Unicode character (rune).

Syntax:

for index, chr := range str {
    // executable statements
}

Example Program:

package main
import "fmt"

func main() {
    str := "GoLang"
    for index, chr := range str {
        fmt.Printf("Index: %d, Character: %c\n", index, chr)
    }
}

Output:

Index: 0, Character: G
Index: 1, Character: o
Index: 2, Character: L
Index: 3, Character: a
Index: 4, Character: n
Index: 5, Character: g

6. Using For Loop for Maps

When iterating over a map using range, it returns both key and value.

Syntax:

for key, value := range map {
    // executable statements
}

Example Program:

package main
import "fmt"

func main() {
    countryCapital := map[string]string{
        "India": "New Delhi",
        "Japan": "Tokyo",
        "France": "Paris",
    }

    for country, capital := range countryCapital {
        fmt.Printf("Country: %s, Capital: %s\n", country, capital)
    }
}

Output:

Country: France, Capital: Paris
Country: India, Capital: New Delhi
Country: Japan, Capital: Tokyo

(Note: Map order is not guaranteed.)

7. Using For Loop for Channels

When you use range on a channel, the loop receives values from the channel until it is closed.

Syntax:

for item := range Chnl {
    // executable statements
}

Example Program:

package main
import "fmt"

func main() {
    ch := make(chan int, 3)
    ch <- 1
    ch <- 2
    ch <- 3
    close(ch)

    for item := range ch {
        fmt.Println("Received:", item)
    }
}

Output:

Received: 1
Received: 2
Received: 3

Summary: Go uses only the for loop, but with flexibility to behave like traditional for, while, foreach, or even infinite loops. The range keyword makes iteration easy over collections such as arrays, slices, strings, maps, and channels.