Correcting errors

Common Errors in Algorithms

Writing a correct algorithm is harder than it looks. Many errors are subtle: the algorithm almost works, producing correct results for most inputs while silently failing for a small number of specific values. The categories below cover the most frequent sources of logical errors in GCSE-level pseudocode. Recognising these patterns makes both writing and checking algorithms much faster.

Error typeTypical appearanceEffect
Wrong comparison operator < used when <= was needed Off-by-one at the boundary: the algorithm fails for exactly one value while working for all others.
Flipped assignment x ← y when the intent was y ← x Overwrites the wrong variable. Common in swap operations - the value to be saved is silently lost.
Wrong variable name Using i (loop counter) where number (input) was intended Reads or updates the wrong value. The algorithm runs without error but produces wrong results.
Wrong loop limit FOR i ← 0 TO 5 instead of FOR i ← 1 TO 5 Loop runs one too many or too few iterations. Totals and averages are then calculated on the wrong count.

Errors in Practice

The algorithm below reads five integers, computes their total, and outputs the average. Each tab introduces more errors. Read the pseudocode carefully, identify what is wrong, then reveal the explanation.

One Error - Wrong Loop Limit

total ← 0
FOR i ← 0 TO 5           ◀ error
    INPUT number
    total ← total + number
ENDFOR
average ← total / 5
OUTPUT average
total ← 0
FOR i ← 1 TO 5           ◀ corrected
    INPUT number
    total ← total + number
ENDFOR
average ← total / 5
OUTPUT average

Two Errors - Loop Limit + Wrong Variable

total ← 0
FOR i ← 0 TO 5           ◀ error 1
    INPUT number
    total ← total + i    ◀ error 2
ENDFOR
average ← total / 5
OUTPUT average
total ← 0
FOR i ← 1 TO 5
    INPUT number
    total ← total + number
ENDFOR
average ← total / 5
OUTPUT average

Three Errors - Loop, Variable + Wrong Output

total ← 0
FOR i ← 0 TO 5           ◀ error 1
    INPUT number
    total ← total + i    ◀ error 2
ENDFOR
average ← total / 5
OUTPUT total                  ◀ error 3
total ← 0
FOR i ← 1 TO 5
    INPUT number
    total ← total + number
ENDFOR
average ← total / 5
OUTPUT average

Deep Dive: The Comparison Trap

Choosing between < and <= (or > and >=) is one of the most frequent sources of off-by-one errors. The bug only affects exactly one value - the boundary itself - so it goes unnoticed unless a test specifically uses that value.

Intended to award "Pass" for any score of 50 or above:

INPUT score
IF score > 50 THEN         ◀ should be >=
    OUTPUT "Pass"
ELSE
    OUTPUT "Fail"
ENDIF

Scores of 51, 60, 75 correctly give "Pass". Score of 49 correctly gives "Fail". But score of exactly 50 gives "Fail" - the algorithm is wrong for the one value it most needs to handle correctly.

Corrected with >=:

INPUT score
IF score >= 50 THEN        ◀ corrected
    OUTPUT "Pass"
ELSE
    OUTPUT "Fail"
ENDIF

Now 49 gives "Fail", 50 gives "Pass", 51 gives "Pass" - all correct. One character changed; one boundary error eliminated.

 Key Takeaways

  • The four most common logical errors in algorithms are: wrong comparison operator, flipped assignment, wrong variable name, and wrong loop limit.
  • A wrong comparison operator (< vs <=) produces an off-by-one error that only affects the exact boundary value - making it easy to miss without targeted testing.
  • A flipped assignment (e.g. x ← y instead of y ← x) overwrites the wrong variable and silently loses a value - most dangerous inside swap operations.
  • A wrong variable name causes the algorithm to read or update the wrong value. The pseudocode runs without error but produces incorrect results throughout.
  • A wrong loop limit causes the loop to execute one too many or too few times, producing errors in any total, count, or average computed inside it.