capypad
0 day streak
go / beginner
Snippet

The For Loop

Go only has one looping construct: 'for'. It can be used as a standard C-style loop or as a 'while' loop by omitting the initialization and post statements.

snippet.go
go
1
2
3
4
5
6
7
8
9
for i := 0; i < 5; i++ {
fmt.Println("Iteration:", i)
}
 
// While-like loop
sum := 1
for sum < 10 {
sum += sum
}
Breakdown
1
for i := 0; i < 5; i++
A standard loop with initialization, condition, and increment.
2
for sum < 10
Acts as a 'while' loop, running as long as the condition is true.