if: the program makes a decision
Comparing two numbers, true and false, a discount with if / else and a grade with else if: for people who have never written code.
🌐 Oʻzbekcha · Русский · English
Lesson GO-0.04 · 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
When your purchase in a shop reaches 100,000 soʻm, the till gives the discount by itself. The cashier calculates nothing. Who made the decision? A program. In this lesson you write your first program that makes a decision like that on its own.
One idea: the program compares two numbers and asks a question. The answer is only yes (true) or no (false),
and if uses that answer to choose exactly one of two paths.
By the end of the lesson you will be able to:
- read a question like
xarid >= 100000in plain words and give its answer (trueorfalse); - write a discount program with
if/elseand a grade program withelse if; - say what a program will print before you run it;
- recognise the two most common mistakes: a single
=insideif, and the wrong order ofelse ifquestions.
Today there are three new words:
| Sign or word | Plain meaning | English term |
|---|---|---|
comparison (>, <, >=, <=, ==, !=) | comparing two numbers to ask a question | comparison |
true / false | the answer to the question: yes / no | true / false |
if / else | ”if …” / “otherwise” | if / else |
Two helper words also appear: bool (the yes-no type) and else if (“otherwise, if”).
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 stuck to a value;
:=creates a new label, and a single=reads “let it be” (assignment). - GO-0.03 · Numbers and text: every value has a type: a number (
int) or text (string). - You don’t need Go on your computer: copy any program on this page into go.dev/play and press Run.
- If you downloaded the course code, open a terminal in the lesson folder (
lessons/000-04-if): the commands below run from there.
First, let’s recall the previous lesson. What does fmt.Println("3" + "3") print? A) 6 · B) an error ·
C) 33
Answer
C: 33. "3" in quotes is text, that is, a string. A plus between two strings joins them side by side (string concatenation):
3 and 3 → 33. Today we compare two numbers with each other instead.
The till’s question: comparison
The till asks every purchase one question: “is the purchase greater than or equal to 100,000 soʻm?” There are only two kinds of answer: if yes, it gives the discount; if no, it takes the full price.
purchase ≥ 100,000 ?
/ \
yes no
| |
discount given full priceIn a question like this, two numbers are compared. This is called a comparison. Go uses the “greater than” and
“less than” signs from school. A keyboard has no ≥ or ≤ key, so we type them with two characters: first “greater”
(or “less”), then “equal”.
| Sign | Read as | Example | Answer |
|---|---|---|---|
> | greater than | 5 > 3 | yes |
< | less than | 3 < 5 | yes |
>= | greater than or equal (≥) | 5 >= 5 | yes: also when equal |
<= | less than or equal (≤) | 4 <= 5 | yes |
>= and <= are one sign each: there is no space between > and =. There are two more signs (== and !=); we
meet them just before the mistakes.
The answer is a value too: true and false
The answer of a comparison is also a value, just like a number or a text. But Go says it with English words:
| The till’s answer | In Go |
|---|---|
| yes | true |
| no | false |
Every comparison gives one of these two. There is no third answer.
Because the answer is a value, we can stick a label on it. Look at these three lines (the inside of a program):
xarid := 120000
katta := xarid >= 100000
fmt.Println(katta)- Line 1: a new label
xarid(“purchase”), value 120,000. - Line 2: there is a question on the right. As in the previous lesson, Go computes the right side first:
120000 >= 100000? Yes, the answer istrue. Then a new labelkatta(“bigger”) is stuck to that answer. - Line 3: prints the value under
katta:true.
Memory picture:
| After line | xarid | katta | On screen |
|---|---|---|---|
1 xarid := 120000 | 120000 (int) | — (not yet) | |
2 katta := xarid >= 100000 | 120000 (int) | true (bool) | |
3 fmt.Println(katta) | 120000 | true | true |
The value under katta has a type too. Its type is bool, the yes-no type: it has only two values, true and
false. Next to int (number) and string (text) from the previous lesson, we now have a third type.
Now we change the first line: the purchase is 50,000. This is taqqos/main.go from the course folder:
package main
import "fmt"
func main() {
xarid := 50000
katta := xarid >= 100000
fmt.Println(katta)
}| Line | Code | What it does |
|---|---|---|
| 1–5 | package main … func main() { | the familiar template from the first lesson |
| 6 | xarid := 50000 | a new label xarid, value 50,000 |
| 7 | katta := xarid >= 100000 | the question: 50000 >= 100000? The answer goes under the new label katta |
| 8 | fmt.Println(katta) | prints the answer under katta |
| 9 | } | the end of main and of the program |
Before running it, predict: what will be printed? A) false · B) true · C) 50000
Answer
A: false. 50,000 is less than 100,000, so the answer to “greater than or equal?” is no, that is, false.
If you chose true, you remembered the old 120,000: line 6 now holds 50,000. If you chose 50000: the label katta
is stuck to the answer of the question, not to the purchase itself.
go run translates the program and runs it right away:
$ go run taqqos/main.go
falseIn short: a comparison asks a question, and its answer is true or false.
if and else: one path out of two
Knowing the answer is not enough: the program must act on it. Let’s write the till’s rule as a sentence: if the purchase is greater than or equal to 100,000, give the discount; otherwise, take the full price.
In Go, the word if means “if”, and else means “otherwise”.
First the if part. The purchase is exactly 100,000:
xarid := 100000
if xarid >= 100000 {
fmt.Println("chegirma bor")
}- Line 1: a new label
xarid, value 100,000. - Line 2:
if, then the question (xarid >= 100000), then a curly brace{. As in the first lesson,{is the start of a group. - Line 3: the line inside the group. It prints
chegirma bor(“there is a discount”), but it runs only if the answer to the question istrue. The space (a tab) at the start of the line shows that it is inside the group. - Line 4:
}is the end of the group.
Now we add a second group. After the } that closes the first group, on the same line, come else and a new
group:
xarid := 100000
if xarid >= 100000 {
fmt.Println("chegirma bor")
} else {
fmt.Println("chegirma yoʻq")
}- Line 4:
} else {: the first group ended, the “otherwise” group begins. - Line 5: prints
chegirma yoʻq(“no discount”), but it runs only if the answer isfalse. - Line 6:
}is the end of the second group.
The full program is chegirma/main.go from the course folder:
package main
import "fmt"
func main() {
xarid := 100000
if xarid >= 100000 {
fmt.Println("chegirma bor")
} else {
fmt.Println("chegirma yoʻq")
}
}| Line | Code | What it does |
|---|---|---|
| 6 | xarid := 100000 | a new label xarid, value 100,000 |
| 7 | if xarid >= 100000 { | asks the question; if the answer is true, it enters line 8 |
| 8 | fmt.Println("chegirma bor") | runs only when the answer is true |
| 9 | } else { | if the answer is false, the program enters line 10 from here |
| 10 | fmt.Println("chegirma yoʻq") | runs only when the answer is false |
| 11 | } | the end of the else group |
| 12 | } | the end of main |
The purchase is exactly 100,000. What will be printed? A) chegirma bor · B) chegirma yoʻq · C) both
Answer
A: chegirma bor. 100,000 is equal to 100,000, and the >= sign gives true also when the numbers are equal.
So the if group ran. If you chose “no discount”, you read >= as a plain “greater than”. If you chose “both”: out of
the two groups, only one ever runs.
$ go run chegirma/main.go
chegirma borHow the program went:
| Line | What happened | On screen |
|---|---|---|
6 xarid := 100000 | 100000 under xarid | |
7 if xarid >= 100000 { | 100000 >= 100000 → true | |
8 fmt.Println("chegirma bor") | ran | chegirma bor |
9–11 } else { … } | skipped |
xarid >= 100000 ? → true
/ \
if group ✔ else group ✘
"chegirma bor" (skipped)In short: out of the two groups, only one ever runs. Answer true: the if group; answer false: the else group.
A small test: > and >=
Now we put only > (greater than) instead of >= in line 7. The purchase is again 100,000. This is sinov/main.go:
package main
import "fmt"
func main() {
xarid := 100000
if xarid > 100000 {
fmt.Println("chegirma bor")
} else {
fmt.Println("chegirma yoʻq")
}
}The only difference from chegirma/main.go is line 7: xarid > 100000 instead of xarid >= 100000.
What will be printed now? A) chegirma bor · B) chegirma yoʻq · C) an error
Answer
B: chegirma yoʻq. 100,000 is not greater than 100,000; it is equal to it. The answer is false, so the
else group ran. If you chose “an error”: > is also a valid comparison sign, and the program runs fine.
$ go run sinov/main.go
chegirma yoʻq| Question | Answer | Which group |
|---|---|---|
100000 >= 100000 | true | if: discount |
100000 > 100000 | false | else: no discount |
One sign decides the fate of a purchase that is exactly on the border. Does the rule say “reaches 100,000” or
“is more than 100,000”? That tells you whether to choose >= or >.
else if: questions one after another
Sometimes there are not two answers but three. For example, a test score (this is our example, not an official grading scale):
- 90 and above:
aʼlo(“excellent”); - 70 and above:
yaxshi(“good”); - in all other cases:
qayta topshiring(“retake the test”).
We put the questions one after another: else if, that is, “otherwise, if”. The last else runs if no answer
was true. This is baho/main.go:
package main
import "fmt"
func main() {
ball := 75
if ball >= 90 {
fmt.Println("aʼlo")
} else if ball >= 70 {
fmt.Println("yaxshi")
} else {
fmt.Println("qayta topshiring")
}
}The new lines:
| Line | Code | What it does |
|---|---|---|
| 6 | ball := 75 | a new label ball (“score”), value 75 |
| 7–8 | if ball >= 90 { … "aʼlo" | question 1: if the answer is true, prints aʼlo |
| 9–10 | } else if ball >= 70 { … "yaxshi" | question 2: asked only if answer 1 is false |
| 11–12 | } else { … "qayta topshiring" | runs if both answers are false |
| 13 | } | the end of the last group |
If the score is 75, what will be printed? A) yaxshi · B) aʼlo · C) qayta topshiring
Answer
A: yaxshi. 75 is less than 90 but greater than 70. If you chose aʼlo, check question 1 again: 75 >= 90 is
false. qayta topshiring is printed only when the answer to both questions is false.
$ go run baho/main.go
yaxshiGo checks the questions from top to bottom, one at a time:
| Step | Line | Question | Answer | What happens |
|---|---|---|---|---|
| 1 | 7 | 75 >= 90 | false | moves on to the next question |
| 2 | 9 | 75 >= 70 | true | line 10: prints yaxshi |
| 3 | 11 | else | — | not checked |
In short: once the first true is found, the rest is not checked at all.
Is it equal? == and !=
In the previous lesson, a single = meant “let it be”: it puts a value under a label and asks nothing. To ask
whether two numbers are equal, Go uses two equals signs: ==. There is also the opposite question: an exclamation
mark and an equals sign, !=, that is, “is it not equal?”.
| Written | Read as | What it does |
|---|---|---|
a = 4 | let a be 4 | assignment: puts 4 under a |
a == 4 | is a equal to 4? | a question: the answer is true or false |
a != 4 | is a not equal to 4? | a question: the answer is true or false |
This small program is not in the course folder; we ran it separately for this page:
package main
import "fmt"
func main() {
a := 4
fmt.Println(a == 4, a != 4)
a = 5
fmt.Println(a == 4, a != 4)
}- Line 6: a new label
a, value 4. - Line 7: two questions:
a == 4? anda != 4?Printlnprints both answers on one line with a space between them. - Line 8: a single
=: letanow be 5. - Line 9: the same two questions, but now 5 is under
a.
Predict: A) true false, then false true · B) true true, then false false ·
C) true false, then true false
Answer
A. First, 4 is under a: “equal to 4?” is true, “not equal?” is false. After line 8, 5 is under a: now
“equal?” is false, “not equal?” is true. == and != always give opposite answers. “B” is impossible: the two
questions can’t both be true at once. If you chose “C”, you forgot a = 5 in line 8.
$ go run teng/main.go
true false
false trueGoing deeper (optional): four more facts about if
This part is not in the video; it comes from the README of the course folder.
- The question needs no round brackets, but the curly braces are required:
if xarid >= 100000 {(A Tour of Go ). elseis optional.tuzat/main.goin the next section has only anif: if the answer isfalse, the program prints nothing.- You don’t need to add “
ballis less than 90” toelse if ball >= 70. That question is reached only when the previous answer,ball >= 90, wasfalse. Harvard’s CS50 asks the same thing: “not all these lines of code are required”. - Programmers also call the question after
ifa condition. In this lesson we kept one word: “question”.
Common mistakes
1. A single = inside if (the most common mistake). In this program the student’s answer is 4. If the answer is
4, the program should print toʻgʻri (“correct”). Remember: a question comes after if. But in line 7, a single =
was written by mistake, and that is not a question. This is xato/main.go, wrong on purpose:
package main
import "fmt"
func main() {
javob := 4
if javob = 4 {
fmt.Println("toʻgʻri")
}
}What does this program do? A) prints toʻgʻri · B) stays silent · C) gives an error
Answer
C: an error. The compiler does not translate the program, so it does not run at all. if needs a question, and a
single = is not a question: it puts a value. If you chose toʻgʻri, you read = as the “equals” of mathematics.
$ go run xato/main.go
# command-line-arguments
xato/main.go:7:11: syntax error: cannot use assignment javob = 4 as valueWe read the message piece by piece, as in the first lesson:
| Piece | Meaning |
|---|---|
xato/main.go | the file |
7 | line 7 |
11 | the 11th character in the line: exactly the single = (tab 1, if 2–3, space 4, javob 5–9, space 10, = 11) |
syntax error | a spelling error in the code: the compiler could not read it |
cannot use assignment javob = 4 as value | ”the assignment javob = 4 cannot be used as a value (an answer)” |
This mistake is so common that the Go compiler has a special message for it. The compiler’s code has this comment: “A common syntax error is to write ’=’ instead of ’==’”.
The fix: write two equals signs. This is tuzat/main.go:
package main
import "fmt"
func main() {
javob := 4
if javob == 4 {
fmt.Println("toʻgʻri")
}
}Only line 7 changed: javob == 4, “is the answer (javob) equal to 4?”. This program has no else: if the answer
were false, it would print nothing.
$ go run tuzat/main.go
toʻgʻri2. The wrong order of else if questions. Here the compiler is silent, and the program gives a wrong answer. This
is the same grade program, only the two questions have swapped places: first it asks about 70, then about 90. And the
score is 95. This is tartib/main.go:
package main
import "fmt"
func main() {
ball := 95
if ball >= 70 {
fmt.Println("yaxshi")
} else if ball >= 90 {
fmt.Println("aʼlo")
} else {
fmt.Println("qayta topshiring")
}
}What is printed for a score of 95? A) aʼlo · B) yaxshi · C) both
Answer
B: yaxshi. Go checks the questions from top to bottom and stops at the first true. 95 >= 70 is true, so
yaxshi is printed, and the aʼlo question never gets its turn. If you chose aʼlo, you thought Go looks at all the
questions and picks the best one. “Both” is impossible: after these questions only one group runs.
$ go run tartib/main.go
yaxshi| Step | Line | Question | Answer | What happens |
|---|---|---|---|---|
| 1 | 7 | 95 >= 70 | true | line 8: prints yaxshi |
| 2 | 9 | 95 >= 90 | — | not checked (it would have been true) |
| 3 | 11 | else | — | not checked |
The fix: put the strictest question, >= 90, first. That is exactly the order in baho/main.go (the section
“else if: questions one after another” above).
3. Mixing up > and >= at the border. This is a silent mistake too: the rule says “reaches 100,000”, but the
code says >. As a result, a purchase of exactly 100,000 gets no discount. Details: the section “A small test” above.
The fix: for a rule that says “reaches” or “and above”, write >=.
4. Writing else on a new line. This program is not in the course folder; we ran it separately for this page:
package main
import "fmt"
func main() {
xarid := 100000
if xarid >= 100000 {
fmt.Println("chegirma bor")
}
else {
fmt.Println("chegirma yoʻq")
}
}$ go run elsequr/main.go
# command-line-arguments
elsequr/main.go:10:2: syntax error: unexpected keyword else, expected }Line 10, character 2: exactly the word else. unexpected keyword else means Go did not expect the word else here;
expected } means it expected a } at this place. Go treats a } at the end of a line as the end of a statement, so
the else on the next line is left alone. The fix: write else on the same line as the } before it:
} else {.
5. Comparing text with a number. This one is not in the course folder either; it was run separately:
package main
import "fmt"
func main() {
yosh := "15"
fmt.Println(yosh == 15)
}$ go run matn/main.go
# command-line-arguments
matn/main.go:7:22: invalid operation: yosh == 15 (mismatched types string and untyped int)This is a relative of the error from the previous lesson: invalid operation, and in brackets the reason: the types
do not match, one is a string, the other is a number. Under yosh (“age”) there is "15" in quotes, that is, text,
while 15 is a number. A comparison, like a plus, works only with values of the same type. The fix: write the
number without quotes: yosh := 15.
Check yourself
Answer first, then open.
1. What is the answer of 5 < 3?
Answer
false, because 5 is not less than 3.
2. What is the difference between a single = and a double ==?
Answer
A single = puts a value under a label (assignment). A double == asks a question, “is it equal?”, and its answer is
true or false.
3. When does the else group run?
Answer
Only when the answer to the question is false. When questions come one after another with else if: when the answer
to every question above it is false.
4. What does this code print?
xarid := 99999
if xarid >= 100000 {
fmt.Println("chegirma bor")
} else {
fmt.Println("chegirma yoʻq")
}Answer
chegirma yoʻq. 99999 >= 100000 is false: 99,999 did not reach 100,000, so the else group runs.
5. From the first lesson: what does the first number (7) in the message xato/main.go:7:11 mean?
Answer
The line number: the error is on line 7. The second number (11) is the position of the character in that line.
Homework
The starter files are in the lesson folder on GitHub: lessons/000-04-if .
🟢 Easy (uyga/oson/main.go): make the discount start at 50,000 soʻm instead of 100,000. Before you change it,
predict: what does it print now, and what after the change?
🟡 Medium (uyga/orta/main.go): add one more level, qoniqarli (“satisfactory”), to the grade program for 55
points and above (ball := 60 → qoniqarli). Where should the new question go?
🔴 Hard (uyga/qiyin/main.go): check a bank card PIN. If the entered code (kiritilgan, “entered”) is not equal
to the PIN, print notoʻgʻri kod (“wrong code”); if it is equal, print xush kelibsiz (“welcome”). Use only the !=
sign (is it not equal?).
The answers are in the “Uy vazifasi” (homework) part of the lesson folder’s README . Try it yourself first.
Answer
🟢 The current file prints chegirma yoʻq, because xarid holds 70,000, which is less than 100,000:
$ go run uyga/oson/main.go
chegirma yoʻqIf you write if xarid >= 50000 {, then 70000 >= 50000 is true, and the program prints chegirma bor.
🟡 Put the new else if after >= 70 and before else (the strictest question first):
if ball >= 90 {
fmt.Println("aʼlo")
} else if ball >= 70 {
fmt.Println("yaxshi")
} else if ball >= 55 {
fmt.Println("qoniqarli")
} else {
fmt.Println("qayta topshiring")
}With ball := 60: 60 >= 90 is false, 60 >= 70 is false, 60 >= 55 is true → qoniqarli. If you put
>= 55 at the very top, even 95 points becomes qoniqarli: that is the order mistake from this lesson.
🔴 Instead of the line fmt.Println(pin, kiritilgan):
pin := 2580
kiritilgan := 1111
if kiritilgan != pin {
fmt.Println("notoʻgʻri kod")
} else {
fmt.Println("xush kelibsiz")
}1111 != 2580 is true, so notoʻgʻri kod. If you write kiritilgan := 2580, then 2580 != 2580 is false, and
the else group prints xush kelibsiz.
Sources
- The Go specification: Comparison operators
(“Comparison operators compare two operands and yield an untyped boolean value”),
Boolean types , If statements ,
Semicolons (after a
}at the end of a line, the statement ends) - The Go compiler’s message: syntax/parser.go (go1.26.8)
- A Tour of Go: If · Go by Example: If/Else
- Harvard CS50x, Week 1 notes (
==and=,else if) - University of Helsinki, Python MOOC: Conditional statements
- 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.03 · Next lesson: GO-0.05 → · All lessons
comments