$Id: 102
$SOId: 7956
Simple form using one variable:
for i := 0; i < 10; i++ {
fmt.Print(i, " ")
}
Using two variables (or more):
for i, j := 0, 0; i < 5 && j < 10; i, j = i+1, j+2 {
fmt.Println(i, j)
}
Without using initialization statement:
i := 0
for ; i < 10; i++ {
fmt.Print(i, " ")
}
Without a test expression:
for i := 1; ; i++ {
if i&1 == 1 {
continue
}
if i == 22 {
break
}
fmt.Print(i, " ")
}
Without increment expression:
for i := 0; i < 10; {
fmt.Print(i, " ")
i++
}
When all three initialization, test and increment expressions are removed, the loop becomes infinite:
i := 0
for {
fmt.Print(i, " ")
i++
if i == 10 {
break
}
}
This is an example of infinite loop with counter initialized with zero:
for i := 0; ; {
fmt.Print(i, " ")
if i == 9 {
break
}
i++
}
When just the test expression is used (acts like a typical while loop):
i := 0
for i < 10 {
fmt.Print(i, " ")
i++
}
Using just increment expression: