Computer ScienceExam code: 0984

Pseudocode and Flowcharts

Algorithm Design and Problem Solving

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

Exam tip

A WHILE condition keeps going, an UNTIL condition stops

WHILE gives the condition for continuing, so a loop that re-asks for an out-of-range length reads WHILE Length < 10 OR Length > 50 DO; REPEAT … UNTIL gives the condition for stopping, so the same loop ends UNTIL Length >= 10 AND Length <= 50. The scheme accepted either loop and gave a separate mark for a correct exit condition. Out-of-range tests join with OR, in-range tests with AND; swap the keyword without flipping the condition and the loop rejects valid values and accepts an invalid one. Error-finding questions plant exactly this: WHILE for UNTIL, OR for AND.

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.

Common exam question

Describing changes to an algorithm without rewriting it

Question: Describe or explain how to change a corrected algorithm to use a count-controlled loop, validate its inputs or filter its output, without rewriting it (4–5 marks).

Asked in 3 of the 17 papers, one mark per change described: where (which line to replace or insert between), which construct (FOR, REPEAT, WHILE or IF), its condition or limits and the tidy-up.

Making a WHILE loop count-controlled earned a mark each for replacing the WHILE line, for FOR, for its limits, for removing the counter update and for swapping ENDWHILE for NEXT; and because a FOR loop runs a fixed number of times, an input the WHILE version skipped must now be validated and re-input inside the loop. For a range check, either insert a WHILE loop after the INPUT (repeating while out of range, with an error message and re-input) or wrap the prompt and INPUT in REPEAT … UNTIL the value is in range; both were credited.

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.

Build on this topic