By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Some languages give you while, do while and for. Go gives you one keyword: for. That is not a limitation. Every loop you need is a spelling of for, so there is one form to learn and one form to read in everyone else's code.
The most common form has three parts separated by semicolons: start, condition and step. The start runs once before the loop. The condition is checked before every pass, and the body runs only while it is true. The step runs after each pass, before the condition is checked again.
package main
import "fmt"
func main() {
for i := 1; i <= 3; i++ {
fmt.Println("expense", i)
}
fmt.Println("done")
}Read the header as: start i at 1; while i is at most 3, run the body and then add 1 to i. There are no parentheses around the condition. Go does want the braces, and the opening brace stays on the same line as for.
The three parts are not fixed to counting up. Start high, test with >=, and step down with i--.
package main
import "fmt"
func main() {
for i := 3; i >= 1; i-- {
fmt.Println(i)
}
fmt.Println("liftoff")
}i-- is shorthand for i = i - 1, the same way i++ means i = i + 1. Both work on integer variables and both are statements, so they belong in the step position or on a line of their own.
If the start happens before the loop and the step happens inside the body, leave those two parts out and keep only the condition. Then change something inside the body, or the condition never becomes false and the loop never ends.
package main
import "fmt"
func main() {
cents := 100
for cents > 0 {
fmt.Println("spend", cents)
cents = cents - 25
}
fmt.Println("empty")
}This is the shape of a while loop in other languages, and it fits best when the end depends on data instead of a counter. Notice the order: print, then reduce, then test. Reverse the order and the last line would be spend 0.
Writing for { with no condition at all repeats forever. That is sometimes exactly right, for a program that waits for input, but you need a way to leave. break leaves the loop immediately. Step 14 studies break and continue in detail; for now read it as stop looping.
package main
import "fmt"
func main() {
count := 0
for {
count++
fmt.Println("pass", count)
if count == 2 {
break
}
}
fmt.Println("stopped")
}Go checks the condition before the first pass, so a loop whose condition is false from the start runs zero times. Most loop bugs are subtler than that. They are off by one: the loop runs one time too many or one time too few because the condition uses < where it needed <=, or because the start value is 0 when 1 was meant.
| Header | Values of i | Passes |
|---|---|---|
for i := 0; i < 3; i++ | 0, 1, 2 | 3 |
for i := 0; i <= 3; i++ | 0, 1, 2, 3 | 4 |
for i := 1; i <= 3; i++ | 1, 2, 3 | 3 |
for i := 3; i >= 1; i-- | 3, 2, 1 | 3 |
When a loop prints a count that looks wrong, write down the first value, the last value and the condition on paper. That small table finds the bug almost every time.
Print the numbers 1 to 10 on one line, separated by spaces, then print 10 down to 1, using two separate loops. After that, change the first loop so it ends at 9 without editing the <= condition: adjust the start or the step instead. Run it and check the last value with your own eyes. When you trust the output, add a comment above each loop that states the first and last value it prints.
Key takeaways
Go has one loop keyword: for.
The three-part form is start; condition; step, and the opening brace stays on the for line.
Condition-only loops are Go's while loops; change something in the body so they end.
for { repeats until a break, and the condition is checked before the first pass.
Most loop bugs are off by one: check the first value, the last value and the comparison.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.