Following and Writing Algorithms

advanced35 min

Learning objectives

  • Interpret and trace algorithms expressed in pseudocode
  • Construct algorithms in pseudocode from a problem specification
  • Translate pseudocode into working Python, preserving its logic exactly

Learn

AQA 4.4.2 — Following and writing algorithms

Retrieval: the previous lesson analysed what a problem requires. This lesson covers the step in between that analysis and a working Python program: expressing the solution's logic precisely in pseudocode first.

Key vocabulary

  • Pseudocode — a structured, language-independent way of writing an algorithm's logic, without committing to any specific programming language's exact syntax.
  • Trace table — a table recording how each variable's value changes as an algorithm executes, step by step.

Understand — why pseudocode is a genuine abstraction in its own right

Pseudocode deliberately hides a specific language's exact syntax (Python's colons and indentation, or another language's brackets and semicolons) so that the algorithm's logic — the actual sequence of decisions and steps — can be reasoned about on its own terms. This is exactly the same abstraction principle this sequence keeps returning to: choosing which details matter for the task at hand, and pseudocode's whole purpose is to defer syntax detail until the logic itself is confirmed correct.

See it — tracing a pseudocode algorithm

INPUT value
SET total = 0
FOR i = 1 TO value
    total = total + i
ENDFOR
OUTPUT total

Tracing this with value = 5:

Stepitotal
Start—0
i=111
i=223
i=336
i=4410
i=5515

Output: 15.

See it — translating to Python

value = int(input())
total = 0
for i in range(1, value + 1):
    total = total + i
print(total)

Reason about what a correct translation must preserve

Translating pseudocode into Python must preserve the algorithm's logic exactly — the same decisions, the same order of operations, the same final result for every input. What's allowed to change is purely syntax: FOR i = 1 TO value becomes range(1, value + 1) (note the deliberate off-by-one adjustment, since Python's range excludes its upper bound — a genuine, common translation pitfall), SET becomes plain assignment, ENDFOR becomes indentation. A translation that changes the logic — even subtly, like looping one time too few — is simply wrong, no matter how syntactically valid the resulting Python is.

Common mistake

Assuming a direct, mechanical word-for-word substitution from pseudocode to Python is always safe. As the FOR ... TO example shows, some pseudocode constructs don't map onto Python syntax in an exactly literal way — genuinely understanding what the pseudocode means is what makes a correct translation possible, not simply swapping keywords.

Apply it — trace a second algorithm

INPUT n
SET result = 1
SET count = 1
WHILE count <= n
    result = result * count
    count = count + 1
ENDWHILE
OUTPUT result

Trace this with n = 4, showing count and result after each iteration, and state the output.

(Start: result=1, count=1. Iteration 1 (1<=4): result=1x1=1, count=2. Iteration 2 (2<=4): result=1x2=2, count=3. Iteration 3 (3<=4): result=2x3=6, count=4. Iteration 4 (4<=4): result=6x4=24, count=5. Check 5<=4 is false, loop ends. Output: 24 - this algorithm computes 4 factorial.)

Check your understanding

Trace the pseudocode from "See it" above (the running-total loop) with value = 7. State the output. (2 marks)

(28 - the sum of 1 through 7.)

Challenge

Write pseudocode for an algorithm that takes a list of numbers and outputs the largest one, then translate your pseudocode into working Python.

Looking ahead: the next lesson deepens the abstraction concept pseudocode itself relies on — examining precisely what makes an abstraction valid, and what it deliberately hides.

Practise

Apply what you've just learned in the Coding Lab.

Open Coding Lab

Test yourself

Check your understanding with exam-style questions.

Go to Exam Practice
Log in to track this lesson on your progress dashboard.
Log in