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.
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.
IF ... ELSE: two paths
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
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
| Keyword | Meaning |
|---|---|
| IF ... THEN | Test a condition; run the block if True |
| ELSE | Run this block if the condition was False |
| ELSE IF | Test another condition when earlier ones failed |
| ENDIF | Marks where the whole IF finishes |
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".
= 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.= 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".