Loops in Go Language (original) (raw)

Last Updated : 19 Nov, 2019

Go language contains only a single loop that is for-loop. A for loop is a repetition control structure that allows us to write a loop that is executed a specific number of times. In Go language, this for loop can be used in the different forms and the forms are:1. As simple for loop It is similar that we use in other programming languages like C, C++, Java, C#, etc.Syntax:

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

Here,

Example:

C `

// Go program to illustrate the
// use of simple for loop package main

import "fmt"

// Main function func main() {

// for loop 
// This loop starts when i = 0 
// executes till i<4 condition is true
// post statement is i++
for i := 0; i < 4; i++{
  fmt.Printf("GeeksforGeeks\n")  
}

}

`

Output:

GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks

2. For loop as Infinite Loop: A for loop is also used as an infinite loop by removing all the three expressions from the for loop. When the user did not write condition statement in for loop it means the condition statement is true and the loop goes into an infinite loop.Syntax:

for{ // Statement... }

Example:

C `

// Go program to illustrate the
// use of an infinite loop package main

import "fmt"

// Main function func main() {

// Infinite loop
for {
  fmt.Printf("GeeksforGeeks\n")  
}

}

`

Output:

GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks GeeksforGeeks ...........

3. for loop as while Loop: A for loop can also work as a while loop. This loop is executed until the given condition is true. When the value of the given condition is false the loop ends.Syntax:

for condition{ // statement.. }

Example:

C `

// Go program to illustrate the
// the for loop as while Loop package main

import "fmt"

// Main function func main() {

// while loop
// for loop executes till 
// i < 3 condition is true
i:= 0
for i < 3 {
   i += 2
}

fmt.Println(i) }

`

Output:

4

4. Simple range in for loop: You can also use the range in the for loop.Syntax:

for i, j:= range rvariable{ // statement.. }

Here,

Example:

C `

// Go program to illustrate the
// use of simple range loop package main

import "fmt"

// Main function func main() {

// Here rvariable is a array
rvariable:= []string{"GFG", "Geeks", "GeeksforGeeks"} 

// i and j stores the value of rvariable
// i store index number of individual string and
// j store individual string of the given array
for i, j:= range rvariable {
   fmt.Println(i, j) 
}

}

`

Output:

0 GFG 1 Geeks 2 GeeksforGeeks

5. Using for loop for strings: A for loop can iterate over the Unicode code point for a string. Syntax:

for index, chr:= range str{ // Statement.. }

Here, The index is the variable which store the first byte of UTF-8 encoded code point and chr store the characters of the given string and str is a string.Example:

C `

// Go program to illustrate the
// use for loop using string package main

import "fmt"

// Main function func main() {

// String as a range in the for loop
for i, j:= range "XabCd" {
   fmt.Printf("The index number of %U is %d\n", j, i) 
}

}

`

Output:

The index number of U+0058 is 0 The index number of U+0061 is 1 The index number of U+0062 is 2 The index number of U+0043 is 3 The index number of U+0064 is 4

6. For Maps: A for loop can iterate over the key and value pairs of the map.Syntax:

for key, value := range map { // Statement.. }

Example:

C `

// Go program to illustrate the
// use for loop using maps package main

import "fmt"

// Main function func main() {

// using maps
mmap := map[int]string{
    22:"Geeks",
    33:"GFG",
    44:"GeeksforGeeks",
}
for key, value:= range mmap {
   fmt.Println(key, value) 
}

}

`

Output:

22 Geeks 33 GFG 44 GeeksforGeeks

7. For Channel: A for loop can iterate over the sequential values sent on the channel until it closed.Syntax:

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

Example:

C `

// Go program to illustrate the
// use for loop using channel package main

import "fmt"

// Main function func main() {

// using channel
chnl := make(chan int)
go func(){
    chnl <- 100
    chnl <- 1000
   chnl <- 10000
   chnl <- 100000
   close(chnl)
}()
for i:= range chnl {
   fmt.Println(i) 
}

}

`

Output:

100 1000 10000 100000

Important Points: