Representing Algorithms
Learn to read and write pseudocode and flowcharts - the two universal tools for representing algorithms before coding. Toggle between pseudocode styles to match what appears on your exam papers.
Before writing a single line of real code, professional developers design their algorithms in a language-neutral form. Pseudocode lets you think through the logic without worrying about Python syntax rules. Flowcharts turn the same logic into a visual diagram where loops and branches are immediately visible. Both appear heavily in exam questions - you must be able to read them, trace them, write them and correct errors in them.
Pseudocode sits between natural language and real code. It is precise enough to describe an algorithm unambiguously, but flexible enough that you do not need to worry about compiler rules. Two widely-used styles in UK exams differ mostly in capitalisation and the assignment operator.
| Concept | AQA style | OCR style |
|---|---|---|
| Assignment | x ← 5 | x = 5 |
| Input | x ← USERINPUT | x = input("Enter:") |
| Output | OUTPUT x | print(x) |
| If/Else | IF cond THEN ... ELSE ... ENDIF | if cond then ... else ... end if |
| Count loop | FOR i ← 1 TO 10 ... ENDFOR | for i = 1 to 10 ... next i |
| Condition loop | WHILE cond ... ENDWHILE | while cond ... end while |
| Subroutine | SUBROUTINE name() ... ENDSUBROUTINE | procedure name() ... endprocedure |
| Function | FUNCTION name() ... ENDFUNCTION | function name() ... endfunction |
| Return | RETURN value | return value |
Your exam uses one consistent pseudocode style throughout. Focus on the one matching your specification. The logic is always identical across both styles; only the notation differs. Toggle between styles on this page to see this clearly - every algorithm is logically the same.
Example 1: Find the larger of two numbers
// Find the larger of two numbers (AQA style) OUTPUT "Enter first number: " a ← USERINPUT OUTPUT "Enter second number: " b ← USERINPUT IF a > b THEN OUTPUT "Larger: " + a ELSE OUTPUT "Larger: " + b ENDIF
# Find the larger of two numbers (OCR style) print("Enter first number: ") a = input() print("Enter second number: ") b = input() if a > b then print("Larger: " + a) else print("Larger: " + b) end if
Example 2: Sum numbers 1 to 10 using a FOR loop
total ← 0 FOR i ← 1 TO 10 total ← total + i ENDFOR OUTPUT total // outputs 55
total = 0 for i = 1 to 10 total = total + i next i print(total) # outputs 55
Example 3: Validation loop - keep asking until the user enters a positive number
number ← -1 WHILE number <= 0 OUTPUT "Enter a positive number: " number ← USERINPUT ENDWHILE OUTPUT "Valid: " + number
number = -1 while number <= 0 print("Enter a positive number: ") number = input() end while print("Valid: " + number)
The WHILE condition is checked before the loop body executes. By initialising number to -1 (a value that will definitely fail the condition), we guarantee the loop body runs at least once and actually asks the user for input. Without this initialisation, number would be undefined when the condition is first checked.
Flowcharts show the same algorithm as pseudocode but in visual form. Each shape has a precise, standardised meaning and must not be used for anything else. Arrows show the direction of execution. Loops are shown by arrows pointing back up to an earlier shape.
| Appearance | Shape name | Meaning |
|---|---|---|
| Text | Oval | Start or End (terminal) - every flowchart must have exactly one Start and one End |
| Text | Rectangle | Process: any action such as assignment, calculation, updating a variable |
| Diamond | Decision: a condition with exactly two exits - Yes and No (or True and False) | |
| Parallelogram | Input from the user or output to the screen | |
| → | Flow arrow | Shows the direction of execution between shapes |
IF statement: becomes a diamond with two branches (Yes/No) that rejoin below the diamond.
WHILE loop: becomes a diamond where the No exit leads downward (out of the loop) and the Yes exit leads into a process box, with an arrow looping back up to the diamond.
FOR loop: initialise counter (process box), check counter (diamond), body (process box), increment counter (process box), arrow back to the diamond.
Common exam tasks: (1) Draw a flowchart for a given algorithm. (2) Convert a flowchart to pseudocode. (3) Trace a flowchart with given inputs and write the output. (4) Identify and correct an error in a flowchart. Always use the correct shapes and label both exits of every diamond.
A trace table (or dry run) tracks the values of all variables as an algorithm executes, one statement at a time. It is how you verify an algorithm is correct without running it - and it appears in almost every algorithms exam question.
Rules: one column per variable plus one for output. One row per significant state change. Never skip steps. If a variable does not change in a particular step, leave the cell blank (examiners read the last value written in that column).
Trace this algorithm with a = 14 and b = 4:
a ← USERINPUT // 14 b ← USERINPUT // 4 WHILE a > b a ← a - b ENDWHILE OUTPUT a
a = input() # 14 b = input() # 4 while a > b a = a - b end while print(a)
| Step | a | b | a > b? | Output |
|---|---|---|---|---|
| Input | 14 | 4 | - | - |
| Check 1 | 14 | 4 | True | - |
| Iteration 1 | 10 | 4 | True | - |
| Iteration 2 | 6 | 4 | True | - |
| Iteration 3 | 2 | 4 | False - exit | - |
| Output | 2 | 4 | - | 2 |
The algorithm repeatedly subtracts b from a until a is smaller than b. This computes 14 MOD 4 = 2 (the remainder when 14 is divided by 4).
total ← 0 count ← 1 WHILE count <= 4 total ← total + count count ← count + 1 ENDWHILE OUTPUT total
total = 0 count = 1 while count <= 4 total = total + count count = count + 1 end while print(total)
What is the final value output by this algorithm?
Test yourself
1. Which flowchart shape represents a decision?
2. In AQA pseudocode, how do you store the value 10 in a variable called score?
3. What is the purpose of a trace table?
4. What happens when execution reaches ENDFOR (AQA) / next i (OCR)?
5. A validation loop must keep asking for input until the user enters a value between 1 and 10. Which loop type is most appropriate?
The following pseudocode is intended to sum all odd numbers from 1 to 99. Trace the first three iterations and determine whether it is correct. If not, identify the error and write a corrected version.
total ← 0 FOR i ← 1 TO 99 IF i MOD 2 = 0 THEN total ← total + i ENDIF ENDFOR OUTPUT total
i=1: 1 MOD 2 = 1 (not 0) - condition FALSE, total stays 0.
i=2: 2 MOD 2 = 0 - condition TRUE, total = 0+2 = 2.
i=3: 3 MOD 2 = 1 - condition FALSE, total stays 2.
Error: The condition
i MOD 2 = 0 selects even numbers, not odd. The algorithm sums 2+4+6+...+98 = 2450, not 1+3+5+...+99 = 2500.Fix: Change the condition to
i MOD 2 = 1 (or i MOD 2 <> 0).Practice what you have learned
Three levelled worksheets. Download, print and complete offline.