For in Go
Golang Concepts
Go: For
In Go we have only construct for looping which is
For
. Here we will discuss some basic uses offor
-
For with single condition:
i := 1
for i <= 5 {
fmt.Println(i)
i = i + 1
}
##3 A classic for
loop:
for i := 1; i <= 9; i++ {
fmt.Println(i)
}
For
loop without condition:
for without a condition will loop repeatedly until you break out of the loop or return from the enclosing function.
// break
for {
fmt.Println("loop")
break
}
//continue
for n := 0; n <= 5; n++ {
if n%2 == 0 {
continue
}
fmt.Println(n)
}