Learn
8.1.4B.1 | SELECTION WITH IF, THEN AND ELSE
01 | WHEN A PROGRAM NEEDS TO CHOOSE
A sequence performs instructions in their specified order. But a program often needs different actions for different circumstances. A quiz should display a different message when a score reaches the pass mark. A booking system should behave differently when no places remain.
Selection chooses which instructions to execute according to a condition. It lets the algorithm respond to its data instead of following exactly the same path for every input.
An IF statement makes that choice explicit. The program evaluates a condition, follows the appropriate instructions and then continues.
The program is not guessing what you intended. Your comparison, branch contents and placement must describe the rule precisely.
02 | A CONDITION HAS A BOOLEAN RESULT
A condition is an expression evaluated as TRUE or FALSE. For example, Score >= 50 asks whether the current score is at least fifty.
| Score | Score >= 50 | Meaning |
|---|---|---|
| 49 | FALSE | Below the pass mark |
| 50 | TRUE | Exactly the pass mark is included |
| 75 | TRUE | Above the pass mark |
The condition checks Score; it does not change it. Assignment uses the arrow, while equality comparisons use equals.
“At least fifty” includes fifty, so use >=. “More than fifty” excludes fifty, so use >. Small differences in wording can change the branch at a boundary.
03 | IF AND THEN WITHOUT ELSE
An IF statement does not have to include an alternative branch. Use it when an action is needed only if a condition holds.
DECLARE Score : INTEGER
Score ← 75
IF Score >= 50
THEN
OUTPUT "Pass"
ENDIF
OUTPUT "Review complete"IF introduces the condition. THEN introduces the instructions executed when it is true. ENDIF closes the selection.
At seventy-five the algorithm outputs Pass, then Review complete. At forty-nine it skips the Pass output and still outputs Review complete.
A false condition does not stop the entire program. Without ELSE, the selected body is skipped and execution continues after ENDIF.
04 | ADD ELSE FOR AN ALTERNATIVE
When the task needs one outcome for true and another for false, use IF with ELSE.
IF Score >= 50
THEN
OUTPUT "Pass"
ELSE
OUTPUT "Not yet passed"
ENDIF
OUTPUT "Review complete"Assume Score is declared and has a valid value. A true condition executes the THEN branch and skips ELSE. A false condition executes ELSE and skips THEN.
Within this IF/ELSE statement, one branch runs for a particular evaluation, not both. Afterwards both paths rejoin and Review complete is output.
ELSE means the condition was false. It is not another condition and does not need an extra comparison written after it.
05 | TRACE THE BRANCH AND THE COMMON CODE
| Score | Condition result | Selected branch output | Output after ENDIF |
|---|---|---|---|
| 49 | FALSE | Not yet passed | Review complete |
| 50 | TRUE | Pass | Review complete |
| 75 | TRUE | Pass | Review complete |
Record only the statements that actually execute. Do not output both branch messages merely because both are visible in the pseudocode.
Look carefully at indentation and closing keywords. An output inside THEN is conditional. The same output after ENDIF runs on either path.
Selection changes the path through instructions; sequence still determines the order along that path.
06 | TRY THE DECISION AT ITS BOUNDARY
Predict the branch before entering a score. Try forty-nine, fifty and fifty-one, then compare the condition with the selected output.
The demo checks its form input. The score classification itself uses the rule Score >= 50. After either branch, the common instruction runs.
07 | CHOOSE THE COMPARISON TO MATCH THE RULE
| Operator | Meaning | Example true case |
|---|---|---|
| = | Equal to | Score = 50 when Score is 50 |
| <> | Not equal to | Score <> 50 when Score is 49 |
| > | Greater than | Score > 50 when Score is 51 |
| >= | Greater than or equal to | Score >= 50 when Score is 50 |
| < | Less than | Score < 50 when Score is 49 |
| <= | Less than or equal to | Score <= 50 when Score is 50 |
For “below fifty”, use < 50, not <= 50. For “no more than fifty”, use <= 50. Translating the wording correctly is part of constructing the algorithm.
Comparisons can also involve suitable text or character values, such as MenuChoice = 'A'. Use values appropriate to the declared type rather than comparing a number with unrelated text.
08 | AND REQUIRES BOTH CONDITIONS
Some rules have more than one requirement. A club accepts ages from thirteen to seventeen inclusive. The age must meet both its lower and upper limit.
IF Age >= 13 AND Age <= 17
THEN
OUTPUT "Age within club range"
ELSE
OUTPUT "Age outside club range"
ENDIFAssume Age is a declared INTEGER holding a whole-number age. Sixteen makes both comparisons true. Twelve fails the lower comparison, and eighteen fails the upper comparison.
AND makes the combined condition true only when both parts are true. Using OR here would admit ages outside the interval, because an age can satisfy one bound while violating the other.
Write the full comparisons. Age >= 13 AND <= 17 omits the value being compared in its second part.
09 | OR AND NOT EXPRESS DIFFERENT RULES
OR is useful when either condition is enough. An outside-range test can use Age < 13 OR Age > 17. An age below the minimum or above the maximum is outside the permitted interval.
IF Age < 13 OR Age > 17
THEN
OUTPUT "Age outside club range"
ENDIFNOT reverses a Boolean result. If Found is a declared Boolean search flag, IF NOT Found selects instructions when no match was found.
IF NOT Found
THEN
OUTPUT "Item not found"
ENDIFUse parentheses when grouping more complex conditions so the intended relationships are clear. For now, practise simple complete comparisons before combining many tests.
Remember that a BOOLEAN variable already holds a logical value. IF PaymentComplete can directly test that value; IF PaymentComplete = TRUE expresses the same decision explicitly.
10 | TWO SEPARATE IF STATEMENTS ARE TWO DECISIONS
Do not assume two IF statements behave like one IF/ELSE. Each separate IF evaluates its own condition.
IF Score >= 50
THEN
OUTPUT "Pass"
ENDIF
IF Score < 80
THEN
OUTPUT "Practise for a higher score"
ENDIFAt seventy-five, both conditions are true, so both messages appear. That can be correct if the program should congratulate a pass and offer further practice.
In an IF/ELSE version using Score >= 50, the ELSE message appears only below fifty. It cannot also appear at seventy-five.
Choose independent IF statements when independent actions may both be needed. Choose IF/ELSE when the condition should select one of two alternatives.
11 | NEST DECISIONS FOR THREE OUTCOMES
A nested IF is an IF inside a branch of another IF. Suppose scores of at least eighty get Distinction; scores from fifty to seventy-nine get Pass; lower scores get Not yet passed.
IF Score >= 80
THEN
OUTPUT "Distinction"
ELSE
IF Score >= 50
THEN
OUTPUT "Pass"
ELSE
OUTPUT "Not yet passed"
ENDIF
ENDIFThe second IF is reached only when the outer condition is false, so Score is already below eighty. It then checks whether Score is at least fifty.
| Score | Outer >= 80 | Inner >= 50 | Output |
|---|---|---|---|
| 85 | TRUE | Not reached | Distinction |
| 75 | FALSE | TRUE | Pass |
| 45 | FALSE | FALSE | Not yet passed |
Each IF needs its matching ENDIF. The inner ENDIF closes the pass decision; the outer ENDIF closes the distinction decision. Indentation helps show which statements belong to each branch.
12 | TEST THE MOST RESTRICTIVE CATEGORY FIRST
In the nested classification above, checking Score >= 50 first and immediately outputting Pass would also catch eighty-five. If the distinction check were only in that ELSE branch, eighty-five would never reach it.
For overlapping thresholds, check the higher category first, or use another arrangement that explicitly represents non-overlapping ranges. Do not rely on the output labels to make the conditions exclusive.
Test values forty-nine, fifty, seventy-nine and eighty. They expose mistakes at both category boundaries. Middle values alone may miss an incorrect > instead of >= comparison.
These thresholds are illustrative practice rules, not an official exam grade boundary.
13 | READ A COMPLETE INPUT AND SELECTION ALGORITHM
This program receives a valid whole-number score from zero to one hundred and outputs one category, followed by a common closing message. Assume input is valid; this example classifies it without a validation retry.
DECLARE Score : INTEGER
OUTPUT "Enter a whole-number score from 0 to 100"
INPUT Score
IF Score >= 80
THEN
OUTPUT "Distinction"
ELSE
IF Score >= 50
THEN
OUTPUT "Pass"
ELSE
OUTPUT "Not yet passed"
ENDIF
ENDIF
OUTPUT "Review complete"Input fifty leads through the outer ELSE to the inner THEN. The program outputs Pass, then Review complete.
A prompt describing the range is not validation. If the requirements also demand rejection and retry of invalid inputs, add that handling before classifying the accepted score.
Follow all required outputs in a trace, including the prompt and closing message, rather than recording only the selected category.
14 | AVOID THESE SELECTION MISTAKES
Running both branches: an IF/ELSE selects one branch for its evaluated condition.
Stopping after false: execution continues after ENDIF, whether a THEN body ran or not.
Missing equality: “at least” and “inclusive” require the boundary to be included.
AND confused with OR: an inside-range check needs both bounds; an outside-range check can accept either violation.
Overlapping independent tests: two separate IFs may both execute.
Nested statements not closed: match each IF with its own ENDIF and indent its contents.
Classification mistaken for validation: choosing a category does not automatically reject an invalid input.
15 | EXPLAIN THE PATH WITH A REASON
For input fifty: “The outer distinction condition is false because fifty is below eighty, so the program reaches the inner IF. Its condition is true because fifty meets the inclusive pass threshold, therefore Pass is output.”
For common code: “Review complete is after the outer ENDIF, so it runs regardless of the selected category.”
Use the exact condition and current value to explain a branch. A statement such as “it chooses the correct message” hides the reasoning students need to demonstrate.
The next lesson introduces CASE selection, another way to express suitable choices. Keep these habits: evaluate the condition, follow the selected path and continue at the correct point.