Skip to content
Happy Programming Guide
Start learning
Programming Basics

If Statements Explained for Beginners

An if statement lets your program choose between paths. Here is how conditions work, how to combine them, and why one equals sign is not the same as two.

An if statement lets your program decide. It checks whether something is true, and only runs a block of code when it is.

Python
temperature = 35

if temperature > 30:
    print("It is hot today.")

If the temperature is above 30, the message prints. If not, that line is skipped entirely and the program carries on.

Conditions are just questions with yes/no answers#

Everything inside an if boils down to a true or false value — a boolean. You can print one directly to see it:

Python
print(10 > 3)     # True
print(10 == 3)    # False
print("a" == "a") # True

The comparison operators you will use constantly:

Operator Means Example
== is equal to age == 18
!= is not equal to name != ""
> < greater / less than score > 50
>= <= greater / less than or equal age >= 18

Adding an alternative with else#

else covers every case the if did not.

Python
age = 15

if age >= 18:
    print("You can vote.")
else:
    print("Not yet — come back later.")

Exactly one of those two blocks runs. Never both, never neither.

More than two paths#

Use elif in Python (else if in JavaScript) to chain options. The first matching branch wins and the rest are skipped.

Python
score = 74

if score >= 90:
    grade = "A"
elif score >= 75:
    grade = "B"
elif score >= 60:
    grade = "C"
else:
    grade = "Needs work"

print(grade)   # C

Order matters. If you put score >= 60 first, a score of 95 would match it and stop there, and nobody would ever get an A.

The same thing in JavaScript:

JavaScript
let grade;

if (score >= 90) {
  grade = "A";
} else if (score >= 75) {
  grade = "B";
} else {
  grade = "Needs work";
}

Combining conditions#

  • and — both must be true
  • or — at least one must be true
  • not — flips true and false
Python
age = 22
has_ticket = True

if age >= 18 and has_ticket:
    print("Come in.")

if not has_ticket:
    print("You need a ticket.")

JavaScript uses &&, || and ! for the same three ideas.

Indentation and braces#

Python decides what belongs inside the if purely by indentation. This is why misaligned spaces cause IndentationError.

Python
if logged_in:
    print("Welcome")     # inside the if
print("Goodbye")         # always runs

JavaScript uses braces instead, and ignores your spacing. Omitting the braces around a single statement is legal but a classic source of bugs, so most teams always use them.

A worked example#

Python
password = input("Enter your password: ")

if len(password) < 8:
    print("Too short — use at least 8 characters.")
elif password.isdigit():
    print("Numbers only is easy to guess. Add letters.")
elif password.lower() == "password":
    print("Please, no.")
else:
    print("That will do.")

Notice the checks run cheapest-and-most-likely first. That is a habit worth building.

Questions people ask#

Can I nest an if inside another if?

Yes, and sometimes it is the clearest option. But three or four levels deep gets hard to read. Often you can flatten it by combining conditions with and, or by returning early from a function.

What counts as true if I do not use a comparison?

Most languages treat empty things as false: an empty string, an empty list, the number zero, and None or null. Everything else is true. So if name: is a compact way of asking “did they type anything?”.

What is a ternary or one-line if?

A short form for choosing between two values. Python writes it status = "adult" if age >= 18 else "minor". It is fine for simple choices and unreadable for complicated ones.

Why does my condition never run?

The usual cause is comparing text to a number — input() returns text, so "7" == 7 is false. Print the value and its type just before the if to check. That habit is covered in how to read programming errors.

Where to go next#

Next lessonLoops explained for beginners

Keep reading

Keep going — pick your next guide

The fastest way to improve is to read one guide, then build the thing it describes. Start with the basics, or jump straight to a project.

Ask a question or share what worked

Your email address will not be published. Required fields are marked *