Published by Patrick Mutisya · 14 days ago
Use pseudocode to write a count‑controlled loop.
A count‑controlled loop repeats a block of statements a known number of times. The loop variable is initialised, tested against a limit, and updated each iteration.
| Component | Description |
|---|---|
| Initialisation | Set the loop counter to its starting value. |
| Test condition | Check whether the counter satisfies the loop continuation condition. |
| Body | Statements that are executed each time the condition is true. |
| Update | Modify the counter (usually increment or decrement) so that the loop will eventually terminate. |
The most common representation in Cambridge A‑Level pseudocode is the FOR construct:
FOR i ← start TO end STEP step DO
// loop body
END FOR
Where:
This example demonstrates a count‑controlled loop that adds the numbers \$1\$ through \$10\$.
DECLARE sum, i : INTEGER
sum ← 0
FOR i ← 1 TO 10 DO
sum ← sum + i
END FOR
OUTPUT "The sum is ", sum
sum to \$0\$.i to \$1\$ (the start value).i to sum.i by \$1\$ (the default step).sum.FOR i ← 10 DOWNTO 1 DO … END FORFOR i ← 0 TO 20 STEP 5 DO … END FOR< instead of ≤) can cause the loop to execute one fewer time than intended.Write pseudocode to display the multiplication table for \$7\$ (i.e., \$7 \times 1\$ up to \$7 \times 12\$) using a count‑controlled loop.
DECLARE i, product : INTEGER
FOR i ← 1 TO 12 DO
product ← 7 * i
OUTPUT 7, " × ", i, " = ", product
END FOR
FOR … TO … STEP … DO structure is the standard pseudocode format.