Sub topic 3
Expressing Sequence, Selection, and Iteration
This chapter assumes you can already write variables, conditionals, and loops in at least one programming language. It also assumes you know the assignment arrow (←) and the indentation rules from the previous chapter. Every algorithm, however large, is built from three control structures. Sequence runs steps one after another. Selection picks between paths. Iteration repeats steps. If you write each one in a fixed form, readers and tracers know exactly where each block starts and ends.
Sequence
A sequence is a list of statements that run top to bottom, each one exactly once. Order matters because each line can depend on values set above it.
width ← 4
height ← 3
area ← width × height
OUTPUT areaA common error is to use a variable before it is assigned, for example writing area ← width × height above the line that sets width. Read every sequence top to bottom and confirm that each variable has a value before it is used.
Selection
Selection runs one block out of several, chosen by a condition. Close every IF with END IF so the reader never has to guess which ELSE belongs to which IF.
IF score ≥ 90 THEN
grade ← "A"
ELSE IF score ≥ 80 THEN
grade ← "B"
ELSE
grade ← "C"
END IFConditions are tested in order, and the first true one wins. If you reverse the tests and check score ≥ 80 first, a score of 95 gets a "B". When ranges overlap, put the most restrictive test first.
Iteration
Pseudocode usually has three loop forms. They differ in when the condition is checked.
| Form | Condition checked | Minimum runs | Typical use |
|---|---|---|---|
| FOR i ← a TO b | Before each pass, using a fixed count | 0 if a > b | Known number of repetitions |
| WHILE cond DO | Before each pass | 0 | Repeat while something holds |
| REPEAT ... UNTIL cond | After each pass | 1 | Do once, then check |
count ← 0
FOR i ← 1 TO 5 DO
count ← count + 1
END FOR
n ← 37
steps ← 0
WHILE n > 1 DO
n ← n DIV 2
steps ← steps + 1
END WHILE
REPEAT
INPUT guess
UNTIL guess = secretIn this tutorial, FOR i ← 1 TO 5 includes both ends, so the body runs five times. Write the bounds explicitly. Wrong bounds cause off-by-one errors, which come up again in the desk-checking chapter.
Combining structures
Real algorithms nest these forms inside each other: a selection inside a loop, or a loop inside a branch. Indent each nested block one level deeper and close it with its own END keyword. The block structure should then be clear from the layout alone.
Exercise
Write pseudocode that outputs the sum of the even numbers from 1 to n inclusive. Use one FOR loop and one IF.
Show answer
total ← 0; FOR i ← 1 TO n DO; IF i MOD 2 = 0 THEN total ← total + i END IF; END FOR; OUTPUT total. Written on separate lines, the IF block goes inside the FOR block, one indent deeper, and closes with END IF before END FOR. For n = 6 it outputs 2 + 4 + 6 = 12.
Exercise
You need to read input values until the user enters 0, and you must process at least one value. Which loop form fits best, and why?
Show answer
REPEAT ... UNTIL value = 0. It checks the condition after the body, so the body always runs at least once and you don't need a duplicate INPUT before the loop.