CognitoCoding All posts
24 June 2026 2 min read by Eris Taylor

What Is an If Statement in Coding? (How Your Code Learns to Make Decisions)

pythonlearn-to-codeconditionalsbeginnerscoding-concepts

The Concept in One Sentence

An if statement tells your code to do something only when a condition is true.

Add an else, and your code knows what to do when it's not.

That's the whole idea. Two paths. Code picks one.


Before the Code — A Simple Analogy

You're walking past a field on a farm.

You ask yourself: "Is this crop ready to harvest?"

  • If it is — you pick it.
  • Else — you walk past.

You do this automatically, for every field, all day long. If/else does exactly this in your code: it checks something, then takes a path based on the answer.


Here It Is in Python

crop_ready = True

if crop_ready:
    print("Harvest the field")
else:
    print("Walk past, come back tomorrow")

Try it. Then change crop_ready to False and run it again.

Watch the output swap. That's the whole concept clicking into place.


What's Actually Happening

Python looks at the condition — the thing after if — and asks: is this True or False?

Is crop_ready True?
├── YES → run the first block (harvest)
└── NO  → run the else block (walk past)

Only one block ever runs. Your code takes one path and skips the other.


A Bit More Real

Let's score a quiz:

score = 72

if score >= 60:
    print("You passed!")
else:
    print("Not quite — try again.")

Change the score. Change the threshold. You're writing a program that responds to data — not just prints the same thing every time.


What If There Are More Than Two Options?

Use elif — short for "else if":

score = 45

if score >= 70:
    print("Distinction")
elif score >= 50:
    print("Pass")
else:
    print("Not yet — keep going")

Three possible paths. Python picks exactly one and skips the rest.


Where You'll Spot This in Every Build

Once you know if/else, you'll see it everywhere:

  • Gamesif lives == 0: game over
  • The Farmer series — checking whether a pumpkin crop has hit its spoil timer before harvesting
  • Apps — if someone is logged in, show the dashboard; if not, send them to the login page
  • Chatbots — if the message contains "help", reply with the FAQ

This is the backbone of code that responds to the world. Without it, every program just runs the same straight line, every time, with no awareness of what's actually happening.


Watch the Short

Here's the 60-second version of this concept:

▶ If / Else — One Concept One Board (YouTube Short)

Watch it first if you like the visual — then come back and type the Python examples above yourself. The combination of seeing it and typing it is where it actually sticks.


What Comes Next

You now know the three core building blocks:

  • Variables — store a value
  • Loops — repeat an action
  • If/else — make a decision

These three show up in almost every program ever written. The next step is combining them: a loop that checks a condition on every pass, a variable that changes based on what the condition finds. That's where coding starts to feel like actual power.