Programming Algorithms
Senior 4 · ICT
Programming Algorithms
Lesson 4 of 5
25 min
When you send money by mobile money, the system does not simply move it. It first asks questions: is the PIN correct? Is the balance enough to cover the amount and the fee? Only if every answer is yes does the transfer go ahead; otherwise it shows a message and stops. Each question is a condition, and the part of the algorithm that chooses what to do next is a selection (conditional) statement. The algorithms in lessons 2 and 3 always did the same steps; from now on they can choose.
Definition
Condition: a Boolean expression, TRUE or FALSE, such as mark ≥ 50 or balance ≥ amount AND pinOK.
Selection: a control structure that runs different steps depending on a condition: IF…THEN (one-way), IF…THEN…ELSE (two-way), ELSE IF or CASE (multi-way).
Nested condition: an IF statement placed inside another IF, so the inner question is asked only when the outer one has a certain answer.
IF…THEN does something only when the condition is TRUE, and nothing otherwise. IF…THEN…ELSE always does exactly one of two things.
The lines between THEN and ELSE run when the condition is TRUE; the lines between ELSE and ENDIF run when it is FALSE. Indent them so the structure is visible.
When there are more than two outcomes, test the conditions one after another with ELSE IF. The first TRUE condition wins and the rest are skipped. A school's grade bands (illustrative) make a good example.
Because the tests go from the highest boundary down, a mark of 72 fails the first test, passes the second and gets B; the remaining tests are never asked. When the choice depends on one value matching fixed cases (such as a menu option 1, 2 or 3), many languages offer CASE choice OF 1: … 2: … OTHERWISE: … ENDCASE instead.
Worked example 1: test with boundary data
Good testing uses three kinds of data: normal (typical values), boundary (values exactly at and just beside each limit) and erroneous (values that should be rejected).
| Test mark | Kind | Expected grade | Path taken |
|---|---|---|---|
| 65 | normal | C | fails ≥ 80 and ≥ 70, passes ≥ 60 |
| 80 | boundary | A | passes ≥ 80 at once |
| 79 | boundary | B | fails ≥ 80, passes ≥ 70 |
| 50 | boundary | D | passes ≥ 50 |
| 49 | boundary | F | fails every test, ELSE |
| −5 or 130 | erroneous | should be rejected | the algorithm above gives F and A: it needs a validity check |
The last row exposes a bug. Fix it by checking validity first: IF mark < 0 OR mark > 100 THEN OUTPUT "Invalid mark" ELSE … the grading … ENDIF.
In the Gregorian calendar a year is a leap year if it divides by 4, except that century years (dividing by 100) are leap years only if they also divide by 400. Each question only makes sense after the one before has been answered, so the IFs are nested.
Each inner IF sits inside a branch of the outer one and has its own ENDIF. Count them: three IFs, three ENDIFs.
Worked example 2: trace the leap-year rule, then shorten it
| year | MOD 4 = 0? | MOD 100 = 0? | MOD 400 = 0? | leap |
|---|---|---|---|---|
| 2026 | No (remainder 2) | not asked | not asked | FALSE |
| 2024 | Yes | No | not asked | TRUE |
| 1900 | Yes | Yes | No (remainder 300) | FALSE |
| 2000 | Yes | Yes | Yes | TRUE |
The same rule fits in one compound condition, using AND and OR from Unit 2: Nesting shows the reasoning step by step; the compound condition is shorter. Both give the same output for every year.
Python writes MOD as %, comparison as ==, and marks each branch by indentation after a colon instead of ENDIF. Try 2026, 2024, 1900 and 2000 and compare with the trace table. Unit 4 teaches this syntax properly.
Mr Long Education: Selection Programming, Nested If Statements. Watch how an IF placed inside another IF handles several scenarios, and how the indentation shows which ELSE belongs to which IF.
Warning
The order of ELSE IF tests matters. If the grade algorithm tested mark >= 50 first, every mark from 50 to 100 would get D, because the first TRUE condition wins. Test from the most demanding condition down.
Match every IF with its ENDIF. In nested IFs a missing ENDIF makes it unclear which ELSE belongs to which IF. Indent each level.
Watch the boundary symbol. mark > 50 and mark >= 50 differ for exactly one mark, 50, and that is the one that gets tested.
Tip
Draw the number line first. For any banded decision (grades, fares, tax rates), sketch the ranges on a line, mark which side owns each boundary, and choose test data on both sides of every boundary.
Practice
age >= 18 where valid ages are 0 to 120, and label each normal, boundary or erroneous.>= 50, >= 60, >= 70, >= 80. Evaluate the algorithm by tracing the marks 45 and 85.Key points
Sources and credits
This lesson adapts CC BY-SA material and is shared under CC BY-SA 4.0.
// One-way
IF balance < 1000 THEN
OUTPUT "Warning: low balance"
ENDIF
// Two-way
INPUT mark
IF mark >= 50 THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIFINPUT mark
IF mark >= 80 THEN
grade ← "A"
ELSE IF mark >= 70 THEN
grade ← "B"
ELSE IF mark >= 60 THEN
grade ← "C"
ELSE IF mark >= 50 THEN
grade ← "D"
ELSE
grade ← "F"
ENDIF
OUTPUT gradeINPUT year
IF year MOD 4 = 0 THEN
IF year MOD 100 = 0 THEN
IF year MOD 400 = 0 THEN
leap ← TRUE
ELSE
leap ← FALSE
ENDIF
ELSE
leap ← TRUE
ENDIF
ELSE
leap ← FALSE
ENDIF
OUTPUT leap# The leap-year rule in Python. Press Run and type a year.
year = int(input("Year: "))
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
leap = True
else:
leap = False
else:
leap = True
else:
leap = False
print(year, "is a leap year:", leap)Quick check
With the tests ordered ≥ 80, ≥ 70, ≥ 60, ≥ 50, ELSE, which grade does a mark of 70 get?