🔀
Coding🚀 Ages 7-10Beginner 6 min read

Making Decisions with If

A primary-school coding lesson on if statements: learn how code makes choices using if, else, and conditions, with Scratch and Python examples and a quiz.

Key takeaways

  • An 'if' statement lets code make a decision
  • It checks a condition that is either true or false
  • If the condition is true, the code inside runs
  • 'else' tells the code what to do when the condition is false

Code that chooses

So far, code has run every step in order. But real programs need to make choices.

Should the game say "You win"? Only if the score is high enough. Should the door open? Only if you have the key.

In coding, we make choices with an if statement.

How 'if' works

An if statement checks a condition. A condition is a yes-or-no question that is either true or false.

If the condition is true, the code inside runs. If it's false, that code is skipped.

Here it is in Python:

age = 8
if age >= 7:
    print("Big kid!")

The condition is age >= 7, which means "is age greater than or equal to 7?" Since age holds 8, the answer is true, so it prints Big kid!.

If age were 5, the condition would be false, and nothing would print. The indented line only runs when the condition is true. (Remember variables? We met them in Variables: Boxes for Information.)

Adding 'else'

What if we want something to happen when the condition is false? We add else:

age = 5
if age >= 7:
    print("Big kid!")
else:
    print("Keep growing!")

Now the code has two paths:

  • If age >= 7 is true → print Big kid!
  • Otherwise (else) → print Keep growing!

Since age is 5, the condition is false, so this prints Keep growing!. Exactly one path always runs.

Comparing things

Conditions often compare two values. Here are the symbols Python uses:

SymbolMeans
==is equal to
!=is not equal to
>is greater than
<is less than
>=greater than or equal to
<=less than or equal to

Notice that "is equal to" uses two equals signs (==). One equals sign (=) is for putting a value in a variable. Mixing these up is a common bug to watch for when debugging!

Decisions in Scratch

Scratch has if statements too. In the Control blocks you'll find:

if <touching edge?> then
  turn 180 degrees

The diamond-shaped slot holds the condition. Scratch fills it with blue boolean blocks like touching edge? or key space pressed?. There's also an if-then-else block with two openings, one for true and one for false. You can place these inside loops so the program keeps checking again and again.

Why decisions matter

Without if statements, every program would do the same thing every time. The if statement is what makes code feel smart: it can react, choose, and respond.

This idea grows all the way up to artificial intelligence, where computers make millions of decisions. But it all starts with one simple question: is this true or false?

Quick quiz

Test yourself and earn XP

What does an 'if' statement do?

A condition is always...

What does this print? ``` age = 8 if age >= 7: print("Big kid!") ```

What is 'else' used for?

FAQ

A condition is a question that is either true or false, like is the score above 10. The if statement uses the answer to decide what to do.

No. You can use if on its own. The else part is optional and is only there if you want something different to happen when the condition is false.