Programming Guides
Beginner Programming Lesson: If / Else Conditions
Learn how programs make decisions with if, else, and elif using simple real-life examples, Python code, comparison operators, true/false logic, and beginner exercises.
💡Key Takeaways
- Learn how programs make decisions with if, else, and elif using simple real-life examples, Python code, comparison operators, true/false logic, and beginner exercises.
Prepared on: June 5, 2026
Audience: Absolute beginners with no programming background
Level: Very basic
Main topic: Conditional statements, if, else, true/false logic
SEO/GEO focus: what is if else in programming, conditionals for beginners, learn programming from zero, programming decisions
Short description
This lesson explains how a program makes decisions. When learning programming, you will often see situations such as: if the user is old enough, allow registration; if the password is wrong, show an error; if the shopping cart is empty, do not allow checkout. These are all conditional decisions.
The most common conditional structure is if / else.
1. Start with real life, not code
In everyday life, we make decisions using “if... then... otherwise...”.
Example:
If it rains, bring an umbrella.
Otherwise, do not bring an umbrella.
Another example:
If you are at least 18 years old, you can register.
Otherwise, you cannot register.
A computer can work in a similar way, but the condition must be written clearly.
Example:
If age >= 18, print "Adult".
Otherwise, print "Minor".
This is the basic idea of if / else.
2. What is if?
if means check a condition.
In programming, if checks whether something is true. If it is true, the program runs the code inside the if block.
Python example:
age = 18
if age >= 18:
print("Adult")
How to read it:
Assign 18 to the variable age.
If age is greater than or equal to 18,
print "Adult".
Because age is 18, the condition age >= 18 is true, so the program prints:
Example
3. What is a condition?
A condition is a question that the computer can answer with true or false.
Examples:
| Condition | Meaning | Result when age = 18 |
|---|---|---|
age >= 18 | Is age at least 18? | True |
age < 18 | Is age less than 18? | False |
age == 18 | Is age exactly 18? | True |
age != 18 | Is age not equal to 18? | False |
Important note:
=usually means assign a value.==usually means compare for equality.
Example:
age = 18
This assigns 18 to age.
age == 18
This checks whether age equals 18.
4. Visual explanation
Image source: Original PNG diagram created for this lesson, not SVG.
Image purpose: Shows how a program chooses one branch based on whether a condition is true or false.
5. What is else?
else means otherwise.
If the condition in the if statement is false, the program runs the else block.
Example:
age = 15
if age >= 18:
print("Adult")
else:
print("Minor")
How to read it:
If age is greater than or equal to 18,
print "Adult".
Otherwise,
print "Minor".
Because age is 15, the condition age >= 18 is false, so the program prints:
Example
6. Only one branch usually runs
With if / else, the program usually chooses one of two branches.
Example:
is_raining = True
if is_raining:
print("Bring an umbrella")
else:
print("No umbrella needed")
If is_raining is True, the program prints:
Example
The else block does not run.
If you change it to:
is_raining = False
The program prints:
Example
The if block does not run.
7. What is elif?
elif can be read as otherwise, check another condition.
Use elif when there are more than two choices.
Example: grading a score.
score = 8
if score >= 9:
print("Excellent")
elif score >= 7:
print("Good")
elif score >= 5:
print("Average")
else:
print("Needs practice")
How the program checks it:
score >= 9? False.
score >= 7? True.
Print "Good".
Stop.
Result:
Example
Important idea: when one condition is true, the later branches usually do not run.
8. Easy example: password check
Assume the correct password is "abc123".
password = "abc123"
if password == "abc123":
print("Login successful")
else:
print("Wrong password")
If password is correct, the program prints:
Example
If you change it to:
password = "wrong"
The program prints:
Example
9. Practical example: shopping cart check
cart_total = 0
if cart_total > 0:
print("Allow checkout")
else:
print("The cart is empty")
How to read it:
If the cart total is greater than 0,
allow checkout.
Otherwise,
say that the cart is empty.
Result:
Example
10. Common comparison operators
| Operator | Meaning | Example |
|---|---|---|
> | greater than | age > 18 |
< | less than | age < 18 |
>= | greater than or equal to | age >= 18 |
<= | less than or equal to | age <= 18 |
== | equal to | age == 18 |
!= | not equal to | age != 18 |
Beginners often confuse = and ==. This is a very common mistake.
11. Combining multiple conditions
You can check more than one condition at the same time.
Example: a person can buy a ticket only if they are old enough and have enough money.
age = 20
money = 100000
if age >= 18 and money >= 50000:
print("Can buy ticket")
else:
print("Not allowed")
and means both conditions must be true.
age >= 18 True
money >= 50000 True
Because both are true, the result is:
Example
Example with or:
is_member = False
has_coupon = True
if is_member or has_coupon:
print("Gets a discount")
else:
print("No discount")
or means at least one condition must be true.
Because has_coupon is True, the program prints:
Example
12. Common beginner mistakes
Mistake 1: Confusing = with ==
Wrong in many languages:
if age = 18:
print("Exactly 18")
Correct:
if age == 18:
print("Exactly 18")
= assigns a value. == compares values.
Mistake 2: Forgetting : in Python
Wrong:
if age >= 18
print("Adult")
Correct:
if age >= 18:
print("Adult")
In Python, if, elif, and else lines usually need a colon at the end.
Mistake 3: Wrong indentation in Python
Wrong:
if age >= 18:
print("Adult")
Correct:
if age >= 18:
print("Adult")
In Python, indentation tells the program which lines belong inside the if block.
Mistake 4: Making the condition too complex
Beginners sometimes write one condition that is too long:
if age >= 18 and money >= 50000 and is_active == True and country == "VN":
print("Allowed")
You can split it into smaller names:
is_adult = age >= 18
has_enough_money = money >= 50000
is_valid_user = is_active == True
is_in_vietnam = country == "VN"
if is_adult and has_enough_money and is_valid_user and is_in_vietnam:
print("Allowed")
This is longer, but easier to read for beginners.
13. How to read an if / else block
When you see conditional code, ask four questions:
- Which variable is being checked?
- What is the condition?
- What runs if the condition is true?
- What runs if the condition is false?
Example:
temperature = 35
if temperature >= 30:
print("Hot")
else:
print("Not hot")
Answer:
Variable being checked: temperature.
Condition: temperature >= 30.
If true: print "Hot".
If false: print "Not hot".
Because temperature = 35, the program prints:
Example
14. Small exercises
Exercise 1
Given this code:
age = 16
if age >= 18:
print("Adult")
else:
print("Minor")
What does the program print?
Exercise 2
Fill in the blank. If score is greater than or equal to 5, print "Pass". Otherwise, print "Fail".
score = 7
if ______:
print("Pass")
else:
print("Fail")
Exercise 3
Write a program that checks temperature:
- If
temperature >= 30, print"Hot" - Otherwise, print
"Cool"
Exercise 4
Predict the result:
money = 30000
if money >= 50000:
print("Can buy ticket")
else:
print("Not enough money")
Exercise 5
Write a program that checks whether a number is positive, negative, or zero.
Hint:
number = 0
if number > 0:
...
elif number < 0:
...
else:
...
15. Answers
Answer 1
Result:
Example
Because 16 >= 18 is false.
Answer 2
score = 7
if score >= 5:
print("Pass")
else:
print("Fail")
Answer 3
temperature = 32
if temperature >= 30:
print("Hot")
else:
print("Cool")
Answer 4
Result:
Example
Because 30000 >= 50000 is false.
Answer 5
number = 0
if number > 0:
print("Positive")
elif number < 0:
print("Negative")
else:
print("Zero")
16. Quick memory notes
if means “check this condition”.
else means “otherwise”.
elif means “otherwise, check another condition”.
A condition must produce a true or false result.
= assigns a value. == compares values.
For beginners, first read code as normal human logic: “if this is true, do that; otherwise, do something else.” After that, the code becomes much easier to understand.
17. What to learn next
After understanding if / else, the next topic should be loops. Loops let a program do something many times, such as printing 10 lines, checking many products in a cart, or processing every user in a list.
References
- Python Docs — More Control Flow Tools /
ifstatements: https://docs.python.org/3/tutorial/controlflow.html - Python Docs — Compound statements: https://docs.python.org/3/reference/compound_stmts.html
- MDN Web Docs — JavaScript
if...else: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else - MDN Web Docs — Making decisions in your code — conditionals: https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Scripting/Conditionals
- Harvard CS50x — Conditional Statements: https://cs50.harvard.edu/x/shorts/conditional_statements/
- Harvard CS50’s Introduction to Programming with Python: https://cs50.harvard.edu/python/
Written by PixelRouter Editorial Team
We publish deep, authoritative guides on AI infrastructure, API gateway security, cloud financial management, and system optimizations for developers.
FAQ
What is if / else in programming?
if / else is a conditional structure that lets a program make a decision. The if part checks whether a condition is true. If it is true, the program runs the if block. Otherwise, it runs the else block.
What is a condition?
A condition is a question the computer can answer with true or false, such as age >= 18, age < 18, age == 18, or age != 18.
What is the difference between = and ==?
In many programming languages, = assigns a value to a variable, while == compares two values to check whether they are equal.
What does elif mean?
elif means otherwise, check another condition. It is used when there are more than two possible choices.
What are common beginner mistakes with if / else in Python?
Common mistakes include confusing = with ==, forgetting the colon after if, elif, or else, using wrong indentation, and writing conditions that are too complex.