Selection (IF statements)

💻 Computer Science Years 6–10 Foundation

How programs make decisions with IF, ELSE and ELSE IF, and how to trace which branch runs.

Open this lesson, with practice and a printable worksheet →

So far your programs run straight down the page, one line after another. But real programs need to make decisions: unlock the door only if the password matches; award a bonus only if the score is high enough. Selection — the humble IF statement — is how a program chooses between different paths.

Why this matters: Without selection, every program would behave identically every time. Selection lets code respond to its input — the same program can congratulate one user and warn another. It is one of the three building blocks of all programming, alongside sequence and iteration.
age >= 18 ? True False "adult" "child"
A condition splits the flow into two paths — True or False.

The basic IF

An IF statement tests a condition — anything that is either True or False. If it is True, the indented block runs; if it is False, that block is skipped.

IF age >= 18 THEN
    OUTPUT "You may enter"
ENDIF

Think of a nightclub bouncer with one rule: 18 or over, you are in; otherwise, nothing happens and you walk on by. The condition is the bouncer's question, and the indented block is what happens only when the answer is "yes".

Conditions can be combined with the logical operators AND, OR and NOT. AND needs both sides True; OR needs just one; NOT flips True to False. For example IF age >= 13 AND age <= 19 THEN OUTPUT "teenager" only runs when both parts hold.

ANDORTrueFalseFalseboth neededTrueFalseTrueone is enough
AND is True only if both parts are; OR is True if either part is.

IF ... ELSE: two paths

password =="swordfish"?TrueFalseAccessgrantedAccessdeniedexactly one path always runs
With ELSE, one of the two paths always runs — never both, never neither.

Often you want something to happen either way. ELSE gives the second path — the one taken when the condition is False.

IF password == "swordfish" THEN
    OUTPUT "Access granted"
ELSE
    OUTPUT "Access denied"
ENDIF

ELSE IF: more than two choices

mark >= 70 ?DistinctionFalsemark >= 50 ?PassFalseELSEFailfirst True test wins; rest skipped
An ELSE IF ladder is checked top-down and stops at the first True test.

For several possibilities in a row, chain conditions with ELSE IF. The program checks them from the top and stops at the first one that is True.

IF mark >= 70 THEN
    grade = "Distinction"
ELSE IF mark >= 50 THEN
    grade = "Pass"
ELSE
    grade = "Fail"
ENDIF
KeywordMeaning
IF ... THENTest a condition; run the block if True
ELSERun this block if the condition was False
ELSE IFTest another condition when earlier ones failed
ENDIFMarks where the whole IF finishes
Worked trace with mark = 55:
Check mark >= 70 → 55 >= 70 is False, skip.
Check mark >= 50 → 55 >= 50 is True, so grade = "Pass".
The chain stops here — the ELSE is never reached.
Final: grade = "Pass".
Common mistake: using = instead of == inside a condition. IF x = 5 tries to assign 5 to x; you almost always mean IF x == 5, which tests whether x equals 5.
=put value in box5x = 5==ask: are they equal?True?x == 5
= stores a value; == asks a True/False question.

Order matters in an ELSE IF chain. Put the strictest condition first — if you test mark >= 50 before mark >= 70, a mark of 90 would wrongly stop at "Pass".

Get the printable worksheet →
← Variables, Data Types & Operators → Iteration (Loops)
← Sasta Basta home All subjects