for: the computer never gets tired of repeating
The for loop: a counter, start · question · step, the sum of 1 to 5, an infinite loop and Ctrl+C, break: for people who have never written code.
🌐 Oʻzbekcha · Русский · English
Lesson GO-0.05 · Go: 0 → Guru · ▶️ video coming soon · 💻 GitHub
This is the full written version of the video lesson: you can learn from it without watching (or hearing) the video. The video itself is in Uzbek.
What you will learn
Imagine a punishment: write one sentence on the board a hundred times. Your hand gets tired fast. A computer does not get tired, even if it writes the sentence a million times. How do you tell it to do that? This lesson shows you.
The one idea: a loop does one job again and again. The counter is set once at the start, a question is asked
before every lap, and after every lap a step increases the counter. If you forget the step, the loop never ends and
you stop the program with Ctrl+C. To leave a loop from the inside, you use break.
By the end of the lesson you will be able to:
- read the line
for i := 1; i <= 3; i++in plain words: start, question, step; - say how many times a loop runs and what appears on the screen before you run the program;
- add up numbers with a loop (1 + 2 + 3 + 4 + 5);
- recognise an infinite loop, stop it with Ctrl+C, fix it, and leave a loop with
break.
There are three new words today:
| Word | Plain meaning | Term |
|---|---|---|
loop (for) | doing one job again and again | loop |
counter (i) | a variable that counts the laps | counter |
break | leaving the loop from the inside, at once | break |
You will also meet two helper words: ++ (“plus plus”, add one) and infinite loop (a loop that never ends).
Before you start
- GO-0.01 · What is a program?:
go run,fmt.Println, the program template, curly braces (the start and end of a group), and reading an error message asfile:line:column. - GO-0.02 · Variables: a variable is a label put on a value;
:=creates a new label, andochko = ochko + 1means “first the right side, then the result goes under the label”;Printlntakes several values separated by commas. - GO-0.03 · Numbers and text: the difference between a number and a text (in quotes).
- GO-0.04 · if: a question (
<=“less than or equal”,>=“greater than or equal”), its answertrue/false, theifgroup. - You don’t need Go installed: copy any program from this page into go.dev/play and press Run.
- If you downloaded the course code, open a terminal in the lesson folder (
lessons/000-05-for): the commands below run from there.
First, let’s recall the previous lesson. Which word does this code print? A) issiq · B) salqin ·
C) both
harorat := 30
if harorat > 25 {
fmt.Println("issiq")
} else {
fmt.Println("salqin")
}Answer
A: issiq (“hot”; salqin means “cool”). Under the label harorat (“temperature”) there is 30. The question:
is 30 greater than 25? The answer is true, so the if group ran. Only one of the two groups ever runs, so “both”
cannot happen.
| Line | What happened | On screen |
|---|---|---|
1 harorat := 30 | 30 under harorat | |
2 if harorat > 25 { | 30 > 25 → true | |
3 fmt.Println("issiq") | ran | issiq |
Notice: if asks its question once. Today we learn a command that asks the question again and again.
One job again and again: the loop
Say we need to print the numbers from 1 to 5. With what we know so far, we write a separate Println for each
number:
fmt.Println(1)
fmt.Println(2)
fmt.Println(3)
fmt.Println(4)
fmt.Println(5)Five numbers, five lines. What if we need a million numbers? Nobody writes a million lines by hand.
An example from life. At the stadium, the coach says: “Run three laps.” Before each lap you ask yourself: “is the lap number less than or equal to 3?” If yes, you run one more lap and add one to your count. If no, you stop.
we start with lap 1 (start)
│
▼
┌─► lap number ≤ 3? ── no ──► we stop
│ │ yes (question)
│ ▼
│ 🏃 run one lap (the repeated job)
│ │
│ ▼
└── count +1 (step)Doing one job again and again like this is called a loop. The picture has three parts: the start (where we count from), the question (do we run another lap?) and the step (add one to the count). Go code has the same three parts.
The counter: a label that counts the laps
When you run, you count the laps on your fingers. In a program, a separate variable does this. It is called a
counter, and its name is usually i.
A counter is just an ordinary label, like in GO-0.02. On every lap, the value under it goes up by one:
i := 1
i = i + 1
i = i + 1| After line | Under i |
|---|---|
1 i := 1 | 1 |
2 i = i + 1 | 2 (1 is gone) |
3 i = i + 1 | 3 (2 is gone) |
There is no loop here yet: we increased the value by hand. Now the loop will do it for us.
The for line: start, question, step
In Go, a loop starts with the word for. After it come three parts separated by semicolons (;). First we
look at two of them (the third is hidden behind … for now):
for i := 1; i <= 3; … {
fmt.Println(i)
}
fmt.Println("tamom")- First part, the start:
i := 1. A new labeli, value 1. - Second part, the question:
i <= 3, that is, “isiless than or equal to 3?”. Programmers call this part the condition. If the answer istrue, the loop goes on.
Third part, the step: i++. Read it as “i plus plus”. It increases i by one: i++ is the same as
i = i + 1. It is like ochko = ochko + 1 from GO-0.02, only shorter.
for i := 1; i <= 3; i++ {
fmt.Println(i)
}
fmt.Println("tamom")The lines between the { at the end of the for line and the next } are the loop group. They run on every
lap. The fmt.Println("tamom") after the } runs once, when the loop has finished.
The full program is sanash/main.go in the course folder (sanash means “counting”):
package main
import "fmt"
func main() {
for i := 1; i <= 3; i++ {
fmt.Println(i)
}
fmt.Println("tamom")
}| Line | Code | What it does |
|---|---|---|
| 1–5 | package main … func main() { | the familiar template from the first lesson |
| 6 | for i := 1; i <= 3; i++ { | the loop: start i := 1, question i <= 3, step i++; { is the start of the loop group |
| 7 | fmt.Println(i) | on every lap, prints the value under i |
| 8 | } | the end of the loop group: the program goes back to the step and the question on line 6 |
| 9 | fmt.Println("tamom") | after the loop, prints tamom (“done”) once |
| 10 | } | the end of main and of the program |
Before you run it, guess: what appears on the screen? A) 1 2 3 tamom · B) 1 2 tamom ·
C) 0 1 2 3 tamom
Answer
A: 1, 2, 3 and tamom. When i is 1, 2 and 3, the answer to the question is true, so three numbers are
printed. When i is 4, the answer is false, the loop ends and tamom is printed. If you chose “1 2 tamom”, you
read <= as “less than”: 3 <= 3 is also true, because the numbers are equal. If you chose “0 1 2 3”: the
counter starts from the value written in the start, that is 1.
We run the program in the sanash folder with go run:
$ go run sanash/main.go
1
2
3
tamomEach number came out on its own line: Println ends the line every time.
The loop in memory
Now let’s follow, step by step, what happened in memory:
| What ran | Under i | Question | On screen |
|---|---|---|---|
start i := 1 | 1 | 1 <= 3 → true | 1 |
step i++ | 2 | 2 <= 3 → true | 2 |
step i++ | 3 | 3 <= 3 → true | 3 |
step i++ | 4 | 4 <= 3 → false | the loop ended |
| line 9 | 4 | — | tamom |
Look at the last question: 4 <= 3 → false. Exactly this answer ended the loop.
In short:
| Part | In code | When it runs |
|---|---|---|
| start | i := 1 | once, at the very beginning |
| question | i <= 3 | before every lap; if true, the group runs, if false, the loop ends |
| step | i++ | after every lap |
Go’s official rules say the same: the start runs once before the first question, and the step runs after each run of the group (“the init statement is executed once before evaluating the condition for the first iteration; the post statement is executed after each execution of the block”).
A small test: how many Salom?
Test yourself. This is salom/main.go (Salom means “hello”):
package main
import "fmt"
func main() {
for i := 1; i <= 4; i++ {
fmt.Println("Salom")
}
}i starts at 1, and the question is i <= 4. The loop group prints the word Salom. How many times does Salom
appear on the screen? A) 3 · B) 4 · C) 5
Answer
B: 4 times. When i is 1, 2, 3 and 4, the answer is true, and at 5 it is false. If you chose “3”, you
thought 4 <= 4 is false. If you chose “5”, you counted the last question, when i is 5, as a lap too: the
question was asked, but the answer was false and the group did not run.
go run shows the same:
$ go run salom/main.go
Salom
Salom
Salom
SalomCounting with a loop: a sum
Now let’s calculate something with a loop: the sum of all the numbers from 1 to 5, that is 1 + 2 + 3 + 4 + 5. This
is yigindi/main.go (yigindi means “sum”):
package main
import "fmt"
func main() {
jami := 0
for i := 1; i <= 5; i++ {
jami = jami + i
}
fmt.Println(jami)
}| Line | Code | What it does |
|---|---|---|
| 6 | jami := 0 | before the loop: a new label jami (“total”), value 0 |
| 7 | for i := 1; i <= 5; i++ { | the counter i counts from 1 to 5 |
| 8 | jami = jami + i | on every lap: let jami now be jami + i, so the next number is added |
| 9 | } | the end of the loop group |
| 10 | fmt.Println(jami) | after the loop, prints jami |
What does the program print? Hint: is Println inside the curly braces or outside? A) 5 · B) 15 ·
C) 1 3 6 10 15 (jami on every lap)
Answer
B: 15, only one line. fmt.Println(jami) is after the } on line 9, so it is outside the loop group. That is
why it runs once, when the loop has finished. If you chose “1 3 6 10 15”, you thought Println is inside the
group. If you chose “5”, you thought jami only gets the last i: but on every lap the value is added to the
previous jami.
$ go run yigindi/main.go
15How did the computer add 1 to 5? There are two labels here: jami and the counter i. Line 8 follows the rule
from GO-0.02 every time: first the right side is calculated, then the result goes under jami.
| Lap | i | Question | Line 8: jami + i | Under jami |
|---|---|---|---|---|
| before the loop | — | — | — | 0 |
| 1 | 1 | 1 <= 5 → true | 0 + 1 | 1 |
| 2 | 2 | 2 <= 5 → true | 1 + 2 | 3 |
| 3 | 3 | 3 <= 5 → true | 3 + 3 | 6 |
| 4 | 4 | 4 <= 5 → true | 6 + 4 | 10 |
| 5 | 5 | 5 <= 5 → true | 10 + 5 | 15 |
| — | 6 | 6 <= 5 → false | the loop ended | 15 → 15 on screen |
In short: the label jami stayed where it was; only the value under it changed: 0, 1, 3, 6, 10, 15.
If you forget the step: an infinite loop
Now the most common mistake with loops. This is cheksiz/main.go (cheksiz means “endless”). It is the sanash
program itself, except that the step is missing from the for line: there is no i++.
package main
import "fmt"
func main() {
for i := 1; i <= 3; {
fmt.Println(i)
}
fmt.Println("tamom")
}On line 6 the second semicolon is in place, and nothing comes after it. Go accepts this code: there is no compile
error (translation error). By Go’s official rules, any part of the for line may be empty, but the semicolons must
stay.
What does this program print? Hint: when does i increase? A) 1 2 3 tamom · B) it prints 1 without
stopping · C) it prints nothing
Answer
B: it prints 1 without stopping. Only the step would increase i, and there is no step. There is always 1
under i, so the answer to 1 <= 3 is always true. A loop like this, which never ends, is called an infinite
loop. If you chose “1 2 3 tamom”, you thought i grows by itself: only the step increases it. If you chose
“nothing”: the answer to the question is true, so the group runs and prints 1.
If we run it with go run, the screen fills with ones. On this computer, hundreds of thousands of 1s came out in
one second (more than 890,000 in the measurement), and tamom never appeared. To stop the program, hold the Ctrl
key and press C (on a Mac it is also Ctrl, not Cmd):
$ go run cheksiz/main.go
1
1
1
…
1
1
^Csignal: interruptHere … marks a cut: in reality there are hundreds of thousands of 1 lines there. Let’s read the last line:
^Cis how the terminal shows the key you pressed: the^character means Ctrl.signal: interruptmeans “a signal stopped it”. Ctrl+C makes the terminal send the program a “stop” signal, and the program ended because of that signal. This line was printed bygo run.
On Windows the last line may look different (not tested there). On go.dev/play you don’t need
Ctrl+C: the server stops the program by itself after a few seconds and prints timeout running program (“the program
ran for too long”).
What happened in memory:
| What ran | Under i | Question | On screen |
|---|---|---|---|
start i := 1 | 1 | 1 <= 3 → true | 1 |
| no step | 1 | 1 <= 3 → true (again) | 1 |
| no step | 1 | 1 <= 3 → true (again) | 1 |
| … | 1 | always true | 1 1 1 … |
The fix is easy: put the step back. Line 6 becomes for i := 1; i <= 3; i++ { again, and the program prints 1,
2, 3, tamom and stops by itself. That is exactly sanash/main.go.
Project: how many months to save for a phone
Now let’s put everything from the Go 0 lessons into one project. A phone costs 900 thousand soʻm (the Uzbek currency). You save 200 thousand soʻm every month. After how many months do you have enough?
We write the loop for one year, that is 12 months. But it must stop in the month when the money is enough: there is no need to go through all 12 months. For that we need a rule for leaving the loop.
Think of the stadium again. Before running, you set yourself a rule: “if my water runs out, I leave the track, even if the laps are not finished”. You do this leaving yourself, from the inside. Ctrl+C is something else: someone presses it from the outside, by force.
🏃 lap (loop)
│
▼
💧 water gone? (if) ── yes ──► 🚪 leave the track
│ no (break · from the inside)
▼
next lap
Ctrl+C ── from the outside, by forceLeaving a loop like this, from the inside and at once, is called break.
The code looks like the sum program. The money is written in thousands: 200 is 200 thousand soʻm. This is
loyiha/main.go (loyiha means “project”):
package main
import "fmt"
func main() {
jami := 0
for oy := 1; oy <= 12; oy++ {
jami = jami + 200
fmt.Println(oy, "oy:", jami)
if jami >= 900 {
break
}
}
}| Line | Code | What it does |
|---|---|---|
| 6 | jami := 0 | before the loop: 0 saved |
| 7 | for oy := 1; oy <= 12; oy++ { | the counter oy (“month”) counts from 1 to 12, that is, up to one year |
| 8 | jami = jami + 200 | on every lap (every month), 200 is added to jami |
| 9 | fmt.Println(oy, "oy:", jami) | prints the month number, the text oy: (“month:”) and jami on one line |
| 10 | if jami >= 900 { | the question: is jami greater than or equal to 900? |
| 11 | break | if the answer is true: the whole loop ends at once |
| 12 | } | the end of the if group |
| 13 | } | the end of the loop group |
| 14 | } | the end of main and of the program |
On line 9 a number and a text stand side by side. This is not the error from GO-0.03: there is no
plus here, the values are separated by commas. Println prints them one after another and puts a space between them
by itself.
The break on line 11 is inside the if group. Even so, it ends not just the if group but the whole loop.
Go’s official rule: break ends the nearest for it is in (if does not stop it).
Which is the last line on the screen? A) 4 oy: 800 · B) 5 oy: 1000 · C) 12 oy: 2400
Answer
B: 5 oy: 1000. In month 4, jami is 800: 800 >= 900 → false, the money is not enough yet. In month 5,
jami is 1000, that is a million soʻm: 1000 >= 900 → true, and break stopped the loop. If you chose
“4 oy: 800”: 800 does not reach 900 yet. If you chose “12 oy: 2400”, you forgot the break. 1000 was printed
too, because Println (line 9) comes before the if (line 10).
$ go run loyiha/main.go
1 oy: 200
2 oy: 400
3 oy: 600
4 oy: 800
5 oy: 1000In memory:
oy | jami after line 8 | On screen | jami >= 900 |
|---|---|---|---|
1 | 200 | 1 oy: 200 | false |
2 | 400 | 2 oy: 400 | false |
3 | 600 | 3 oy: 600 | false |
4 | 800 | 4 oy: 800 | false |
5 | 1000 | 5 oy: 1000 | true → break, the loop ended |
6 … 12 | — | — | these months never ran |
In these eight lines, all of Go 0 worked together: variables, numbers and text, if and for. Congratulations, the
Go 0 season is over! Next step: GO-1.01 · Why does Go exist? What is inside go build.
Going deeper (optional): counting from zero and for {
This section is not in the video; you can skip it.
Counting from zero. Experienced programmers usually count from 0: for i := 0; i < 3; i++ also runs three laps.
This example is not in the course folder; we ran it separately:
package main
import "fmt"
func main() {
for i := 0; i < 3; i++ {
fmt.Println(i)
}
}$ go run noldan/main.go
0
1
2Counting from zero is needed when you work with lists (slices); we cover them in GO-1.08.
for {. You may write no parts at all in the for line: for {. By Go’s official rules, if the question is not
written, it always counts as true (“If the condition is absent, it is equivalent to the boolean value true”). So
this is an infinite loop written on purpose, and the only way out of it is break.
The picture is simplified. In the tables we drew one i whose value grows 1 → 2 → 3. In fact, since Go 1.22 a
new i is created on every lap and the previous value is copied into it. For the programs in this lesson the result
is the same. We will see when the difference shows in GO-1.05.
Common mistakes
1. The step is forgotten: an infinite loop. The most classic loop mistake. Go gives no error, and the program
does not stop: the answer to the question is always true. The full example is above, in the section “If you forget
the step: an infinite loop”. The sign: the screen fills with the same lines and the program does not end by itself.
Fix: stop it with Ctrl+C, then put the step (i++) back in the for line.
2. Removing the step together with its semicolon. The step may be empty, but the semicolon must stay. This example is not in the course folder:
package main
import "fmt"
func main() {
for i := 1; i <= 3 {
fmt.Println(i)
}
}$ go run nuqtali/main.go
# command-line-arguments
nuqtali/main.go:6:21: syntax error: unexpected {, expected semicolon or newline
nuqtali/main.go:7:17: syntax error: unexpected newline, expected { after for clauseLet’s read the first line: the file nuqtali/main.go, line 6, character 21 (the Tab at the start of the line
counts too), which is where the { is. syntax error: unexpected {, expected semicolon or newline means “a writing
error: the { was not expected; a semicolon or a new line was expected”. After the question, Go waited for the
second ;. The second line is a result of the same mistake. Fix: write the step (i <= 3; i++ {). Both messages
go away.
3. < instead of <=: one lap too few. With i < 3, 3 < 3 → false, so 3 is not printed. This example is
not in the course folder:
package main
import "fmt"
func main() {
for i := 1; i < 3; i++ {
fmt.Println(i)
}
fmt.Println("tamom")
}$ go run kichik/main.go
1
2
tamomGo gives no error; only the result is wrong. Fix: if you need the last number too, write <= (i <= 3). After
you write a loop, ask yourself: what is i on the first lap, and on the last one?
4. Println left inside the loop group. If you move fmt.Println(jami) in the yigindi program before the },
into the group, it runs on every lap. This example is not in the course folder either:
package main
import "fmt"
func main() {
jami := 0
for i := 1; i <= 5; i++ {
jami = jami + i
fmt.Println(jami)
}
}$ go run ichkarida/main.go
1
3
6
10
15This is not a Go error either, but we only wanted 15. Fix: write what you need once after the }. The rule: a
line inside the group runs on every lap; a line after the group runs once.
Check yourself
Answer first, then open the answer.
1. What are the three parts of the for line, and when does each one run?
Answer
The start (i := 1) runs once, at the very beginning. The question (i <= 3) runs before every lap. The step
(i++) runs after every lap.
2. What happens if you forget the step, and how do you fix it?
Answer
The counter does not grow, the answer to the question is always true, and the loop becomes infinite. Ctrl+C stops
the program. The fix: put the step (i++) back.
3. From the second lesson: how does jami = jami + i run?
Answer
It is an assignment. First the right side is calculated (the value under jami plus i), then the result goes under
the label jami, and the old value is gone.
4. What does this code print?
jami := 0
for i := 1; i <= 4; i++ {
jami = jami + i
}
fmt.Println(jami)Answer
10. Under jami: 0 → 1 → 3 → 6 → 10. When i is 5, 5 <= 4 → false, the loop ends, and Println prints 10
once.
5. What is the difference between break and Ctrl+C?
Answer
break is a rule inside the program: the loop ends by itself, from the inside, and the program goes on to the line
after the loop. Ctrl+C works from the outside, by force: the whole program is stopped.
6. From the previous lesson: what is the difference between if and for?
Answer
if asks its question once and runs its group once (or not at all). for asks its question again and again, before
every lap, and repeats the group while the answer is true.
Homework
The starter files and answers are in the lesson folder on GitHub: lessons/000-05-for .
🟢 Easy (uyga/oson/main.go): this file is the same as loyiha/main.go. Save 300 every month instead of
200. Before you run it, guess: after how many months does it reach 900?
🟡 Medium (uyga/orta/main.go): print the multiplication table of 7 with a loop: 7 x 1 = 7 … 7 x 10 = 70.
In code, the star character * multiplies: 7 * 3 → 21. Put one line of the file inside a loop.
🔴 Hard (uyga/qiyin/main.go): this program should calculate 1 + 2 + 3 + 4 + 5, but it prints 5. Find the
hidden mistake. Tip: draw on paper what happens under the label jami on every lap.
Answers: the javob/ folder
(javob means “answer”). Try it yourself first.
Answer
🟢 javob/oson/main.go: line 8 becomes jami = jami + 300.
$ go run javob/oson/main.go
1 oy: 300
2 oy: 600
3 oy: 900After 3 months. 900 >= 900 is “greater than or equal”, so it is true when the numbers are equal too. With >,
the loop would stop in month 4 (1200).
🟡 javob/orta/main.go:
package main
import "fmt"
func main() {
for i := 1; i <= 10; i++ {
fmt.Println(7, "x", i, "=", 7*i)
}
}$ go run javob/orta/main.go
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70"x" is a text, just a letter shown on the screen. The real multiplication is done by 7*i. Println puts a space
between the values given with commas by itself.
🔴 In uyga/qiyin/main.go there is a line jami = 0 inside the loop group. On every lap it sets jami back to zero,
so at the end only the last i, that is 5, is left:
i | After jami = 0 | After jami = jami + i |
|---|---|---|
1 | 0 | 1 |
2 | 0 | 2 |
| … | … | … |
5 | 0 | 5 |
Fix: delete the line jami = 0 inside the loop: the jami := 0 before the loop, once, is enough
(javob/qiyin/main.go).
$ go run javob/qiyin/main.go
15This is one of the most common loop mistakes beginners make: the starting value is set before the loop.
Sources
- Go’s official rules (Go spec, go1.26): For statements ,
IncDec statements (
++adds one), Break statements - The fmt.Println documentation: “Spaces are always added between operands and a newline is appended.”
- A Tour of Go: For : “Go has only one looping construct, the
forloop.” - The os/signal documentation: “A SIGHUP, SIGINT, or SIGTERM signal causes the program to exit.” · The POSIX terminal rule: Ctrl+C (INTR) sends the program a SIGINT signal (The Open Group )
- Harvard CS50x, lecture 1 notes (“meow” three times, the infinite loop and control-C)
- MIT 6.100L finger exercises (“hello world” N times)
- All results were produced with
go1.26.8 linux/amd64on 2026-11-26.
Video and code
▶️ The video is coming soon. This page teaches the whole lesson, so you don’t have to wait for it.
💻 Code and homework answers on GitHub
← Previous lesson: GO-0.04 · Next lesson: GO-X.01 → · All lessons
comments