0984

Pseudocode and Flowcharts

Algorithm Design and Problem Solving · 4 question types

Exam Frequency Analysis

Past paper frequency (2018 to 2024)

This topic accounts for approximately 11% of your exam marks.

stable
Medium
Stable11%

The most marks-dense topic. Both papers test pseudocode writing and trace tables every sitting.

CIE pseudocode supports three loop constructs. Each one fits a different situation; getting the right loop for the job makes algorithms cleaner and easier to read.

FOR loop

A runs a block of code a fixed number of times, with a counter that takes each value in a stated range.

Syntax:

FOR i ← 1 TO 10
   OUTPUT i
NEXT i

Use a FOR loop when:

  • You know in advance how many times the loop should run.
  • A counter is naturally needed for the work (e.g. processing each element in an array).

Variations:

  • FOR i ← 1 TO 10 STEP 2 advances the counter by 2 each iteration (1, 3, 5, 7, 9).
  • FOR i ← 10 TO 1 STEP -1 counts down.

WHILE loop

A runs a block of code as long as a condition is true. The condition is checked at the top before each iteration.

Syntax:

WHILE Answer <> "stop" DO
   INPUT Answer
ENDWHILE

Use a WHILE loop when:

  • You do not know in advance how many iterations will be needed.
  • It may be appropriate to run the loop zero times if the condition is already false at the start.

REPEAT-UNTIL loop

A runs a block of code repeatedly until a condition becomes true. The condition is checked at the bottom after each iteration.

Syntax:

REPEAT
   INPUT Number
UNTIL Number > 0

Use a REPEAT-UNTIL loop when:

  • You do not know in advance how many iterations will be needed.
  • The loop body must run at least once.

Side-by-side comparison

FeatureFORWHILEREPEAT-UNTIL
Number of iterationsKnown in advanceNot knownNot known
Condition checkedn/a (counter-controlled)Before each iteration (top of loop)After each iteration (bottom of loop)
Will it run at least once?Only if the counter range is non-empty (start ≤ end for a STEP +1 loop; start ≥ end for a negative step)No (zero iterations possible)Yes (always at least one iteration)
Counter variableYes, the counterOptional; the programmer manages itOptional; the programmer manages it
Typical useTraversing an array of known size; printing N lines; countingProcessing input until a sentinel value arrives; waiting for a conditionValidating user input; menu loops that should always run once

Choosing the right loop

Two questions decide which loop fits a given task:

  1. Is the number of iterations known before the loop starts? Yes → use FOR. No → continue to question 2.
  2. Should the loop body always run at least once? Yes → use REPEAT-UNTIL (the condition is checked at the bottom). No → use WHILE (the condition is checked at the top, and zero iterations are allowed).

Two quick examples of the pattern in everyday programming. Printing the first twenty multiples of three has a known iteration count, so a FOR loop is the right pick. Asking the user to type a positive number and re-prompting if they type a negative one needs the input to be read at least once before any check happens, so REPEAT-UNTIL fits. Waiting for a sensor reading to drop below a threshold has unknown duration and may already be below at the start, so WHILE fits.

Avoiding common loop pitfalls

  • Off-by-one errors: forgetting that FOR i ← 1 TO 10 runs 10 times, not 9 or 11.
  • Infinite loops: forgetting to update the variable in the loop's condition (e.g. forgetting to read fresh input inside a WHILE Answer <> "stop" loop).
  • Wrong loop choice: using a REPEAT-UNTIL when zero iterations should be possible, or a WHILE when at least one iteration is mandatory.
  • Counter inside the loop body: in CIE pseudocode, you generally should not modify the FOR loop's counter from inside the loop body.
Exam tip

Converting a WHILE loop to a FOR loop

What comes up: describe the changes needed to rewrite a condition-controlled (WHILE) loop as a count-controlled (FOR) loop — a recurring "modify this algorithm" task.

Write: (1) replace the WHILE line with a FOR line that sets the counter's start and end limits; (2) remove the manual counter update from inside the loop body; (3) replace ENDWHILE with NEXT <counter>.

Watch out: each of those three changes is a separate mark — the one most often missed is leaving the manual counter update inside the loop by mistake.