Use pseudocode to write: a 'count-controlled' loop

Published by Patrick Mutisya · 14 days ago

Cambridge A‑Level Computer Science 9618 – Topic 11.2 Constructs

Topic 11.2 – Constructs

Objective

Use pseudocode to write a count‑controlled loop.

What is 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.

General Pseudocode Syntax

ComponentDescription
InitialisationSet the loop counter to its starting value.
Test conditionCheck whether the counter satisfies the loop continuation condition.
BodyStatements that are executed each time the condition is true.
UpdateModify the counter (usually increment or decrement) so that the loop will eventually terminate.

Standard Form (FOR‑style)

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:

  • \$i\$ is the loop counter.
  • \$\text{start}\$ is the initial value of \$i\$.
  • \$\text{end}\$ is the final value at which the loop stops (inclusive).
  • \$\text{step}\$ is the amount added to \$i\$ each iteration (default is 1).

Example: Sum of the First 10 Natural Numbers

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

Step‑by‑Step Explanation

  1. Initialise sum to \$0\$.
  2. Set the loop counter i to \$1\$ (the start value).
  3. Check the condition \$i \le 10\$; if true, execute the body.
  4. Inside the body, add the current value of i to sum.
  5. Increment i by \$1\$ (the default step).
  6. Repeat steps 3‑5 until \$i\$ becomes \$11\$, at which point the condition fails and the loop ends.
  7. Output the final value of sum.

Common \cdot ariations

  • Decrementing loops: FOR i ← 10 DOWNTO 1 DO … END FOR
  • Non‑unit steps: FOR i ← 0 TO 20 STEP 5 DO … END FOR
  • Using a WHILE loop as count‑controlled: initialise a counter, test a condition, and update inside the loop.

Potential Pitfalls

  • Forgetting to update the counter leads to an infinite loop.
  • Using the wrong relational operator in the test (e.g., < instead of ) can cause the loop to execute one fewer time than intended.
  • Off‑by‑one errors are common when the start or end values are mis‑interpreted.

Practice Exercise

Write pseudocode to display the multiplication table for \$7\$ (i.e., \$7 \times 1\$ up to \$7 \times 12\$) using a count‑controlled loop.

Suggested diagram: Flowchart showing initialisation, test, body, update, and loop back for a count‑controlled loop.

Solution Sketch

DECLARE i, product : INTEGER

FOR i ← 1 TO 12 DO

product ← 7 * i

OUTPUT 7, " × ", i, " = ", product

END FOR

Key Take‑aways

  • A count‑controlled loop repeats a known number of times.
  • The FOR … TO … STEP … DO structure is the standard pseudocode format.
  • Always ensure the loop counter is correctly initialised, tested, and updated.
  • Check boundary conditions to avoid off‑by‑one errors.