The for
loop loops through a block of code a specified number of times.
The for
loop is the only loop available in Go.
Loops are handy if you want to run the same code over and over again, each time with a different value.
Each execution of a loop is called an iteration.
The for
loop can take up to three statements:
for
statement1; statement2; statement3 {
// code to be executed for each iteration
}
statement1 Initializes the loop counter value.
statement2 Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
statement3 Increases the loop counter value.
Note: These statements don't need to be present as loops arguments. However, they need to be present in the code in some form.
This example will print the numbers from 0 to 4:
package main
import ("fmt")
func main() {
for i:=0; i < 5; i++ {
fmt.Println(i)
}
}
Result:
0
1
2
3
4
This example counts to 100 by tens:
package main
import ("fmt")
func main() {
for i:=0; i <= 100; i+=10 {
fmt.Println(i)
}
}
Result:
0
10
20
30
40
50
60
70
80
90
100
The continue
statement is used to skip one or more iterations in the loop. It then continues with the next iteration in the loop.
This example skips the value of 3:
package main
import ("fmt")
func main() {
for i:=0; i < 5; i++ {
if i == 3 {
continue
}
fmt.Println(i)
}
}
Result:
0
1
2
4
The break
statement is used to break/terminate the loop execution.
This example breaks out of the loop when i is equal to 3:
package main
import ("fmt")
func main() {
for i:=0; i < 5; i++ {
if i == 3 {
break
}
fmt.Println(i)
}
}
Result:
0
1
2
Note: continue
and break
are usually used with conditions.
It is possible to place a loop inside another loop.
Here, the "inner loop" will be executed one time for each iteration of the "outer loop":
package main
import ("fmt")
func main() {
adj := [2]string{"big", "tasty"}
fruits := [3]string{"apple", "orange", "banana"}
for i:=0; i < len(adj); i++ {
for j:=0; j < len(fruits); j++ {
fmt.Println(adj[i],fruits[j])
}
}
}
Result:
big apple
big orange
big banana
tasty apple
tasty orange
tasty banana
The range
keyword is used to more easily iterate over an array, slice or map. It returns both the index and the value.
The range
keyword is used like this:
for
index, value := array|
slice|
map {
// code to be executed for each iteration
}
This example uses range
to iterate over an array and print both the indexes and the values at each (idx
stores the index, val
stores the value):
package main
import ("fmt")
func main() {
fruits := [3]string{"apple", "orange", "banana"}
for idx, val := range fruits {
fmt.Printf("%v\t%v\n", idx, val)
}
}
Result:
0 apple
1 orange
2 banana
Tip: To only show the value or the index, you can omit the other output using an underscore (_
).
Here, we want to omit the indexes (idx
stores the index, val
stores the value):
package main
import ("fmt")
func main() {
fruits := [3]string{"apple", "orange", "banana"}
for _, val := range fruits {
fmt.Printf("%v\n", val)
}
}
Result:
apple
orange
banana
Here, we want to omit the values (idx
stores the index, val
stores the value):
package main
import ("fmt")
func main() {
fruits := [3]string{"apple", "orange", "banana"}
for idx, _ := range fruits {
fmt.Printf("%v\n", idx)
}
}
Result:
0
1
2