GCSEPaper 2AlgorithmsDesigning, creating and refining algorithms
2.1.2

Designing, creating and refining algorithms

Inputs, processes and outputs plus pseudocode, flowcharts, trace tables and common errors.

Learning Objectives

  • 1Identify the inputs, processes and outputs of a given problem
  • 2Produce simple structure diagrams and flowcharts to represent a problem
  • 3Create, interpret, correct, complete and refine algorithms using pseudocode, flowcharts and a reference/high-level language
  • 4Identify syntax and logic errors in code and suggest fixes
  • 5Create and use trace tables to follow an algorithm, including with nested selection and iteration

Inputs, processes and outputs

Every algorithm can be broken down into three parts: inputs (the data it receives), processing (what it does with that data), and outputs (the result it produces). Being able to spot these three parts in a problem is the first step to designing an algorithm.
Any algorithm can be described using three stages: inputs (the data the algorithm receives, e.g. from a user or a file), processing (how that data is transformed, calculated or checked), and outputs (the result produced, usually shown to the user). Before writing any code, it helps to identify these three stages for the problem you are solving.
Decomposing a problem into inputs, processing and outputs is a standard first step in algorithm design: it clarifies exactly what data the algorithm needs, what transformation or computation must occur, and what result must ultimately be produced or displayed. Exam questions often ask you to identify all three stages for a given scenario, so answers should be specific to the scenario rather than generic.

Structure diagrams

A structure diagram is a simple diagram that shows how a problem is broken down into smaller subsections, and how those subsections link together. It looks a bit like a family tree, with the main problem at the top branching down into its parts.
A structure diagram shows the structure of a problem: the main task at the top, with lines connecting down to its subsections, and further lines showing how those subsections link to their own smaller parts. It is a visual way of representing decomposition, useful for planning a solution before writing any code.
A structure diagram represents the decomposition of a problem as a hierarchy: a top-level task connected to its constituent subsections, which may themselves be further subdivided. You should be able to produce a simple structure diagram showing the structure of a given problem and the links between its subsections, this is the same idea as the decomposition tree used earlier, applied formally as a diagram.
Run a cafeTake orderCook foodTake paymentEach sub-problem is smaller andEasier to solve on its ownSub-problems can be broken down further too

Flowcharts

A flowchart uses shapes connected by lines to show the steps of an algorithm and the order they happen in. Each shape has a specific meaning, for example a diamond always means a yes/no decision.
A flowchart is a diagram made of standard symbols connected by lines that show the flow of an algorithm. You need to know six symbols: terminal (oval, start/stop), input/output (parallelogram), process (rectangle), decision (diamond), sub-program (rectangle with two vertical lines), and the line connecting them and showing direction.
Flowcharts represent an algorithm visually using six standardised symbols. Using the correct symbol for each step is required: a decision (diamond) always represents a branch based on a yes/no or True/False condition, leading to two different paths; a sub-program (rectangle with double vertical lines) represents a call to a predefined procedure or function rather than an inline process step.
Start / StopTerminalInput / OutputData in/outProcessCalculationDecisionYes/NoSub-programSub-routineLine: shows the flow

Writing and refining algorithms

In the exam, you can write algorithms in pseudocode (OCR's Exam Reference Language), as a flowchart, or in a high-level programming language you know, like Python. All three are valid ways to represent the same algorithm.
Algorithms can be created, interpreted, completed or refined using pseudocode (OCR's Exam Reference Language), flowcharts, or a high-level programming language such as Python. You are also expected to use nesting, placing one selection or iteration structure inside another, for example an IF statement inside a FOR loop.
You should be comfortable working across all three representations, writing new algorithms, completing gaps in given algorithms, correcting faulty ones, and refining working ones to be clearer or more efficient. Nesting selection inside iteration (or vice versa) is explicitly required, for example checking a condition on every pass of a loop.
Python· Print only the even numbers from 1 to 10 (nested selection inside iteration)
for i in range(1, 11):
    if i % 2 == 0:
        print(i)
OCR ERL· Print only the even numbers from 1 to 10 (nested selection inside iteration)
for i = 1 to 10
    if i MOD 2 == 0 then
        print(i)
    endif
next i

Identifying common errors

There are two main types of error in code. A syntax error breaks the rules of the language, so the program won't even run, like a missing colon. A logic error lets the program run, but it produces the wrong result, like using + instead of *.
A syntax error breaks the grammatical rules of the programming language and prevents the program from running at all, for example a missing keyword or incorrect punctuation. A logic error does not stop the program running, but causes it to produce an incorrect result, for example using the wrong comparison operator or the wrong variable. Logic errors are usually harder to spot because the program appears to work.
Distinguishing a syntax error (the program fails to run because it breaks the language's rules) from a logic error (the program runs but produces an incorrect result due to a flaw in the algorithm's design) is a required skill. When two conditions are combined with AND or OR, each condition must independently reference the variable being tested, e.g. num >= 1 AND num <= 100 is correct, but num >= 1 AND <= 100 is a syntax error since the second condition has no variable to compare.

Trace tables

A trace table is a table used to track how the value of each variable changes as an algorithm runs, one line at a time. It helps you follow what a program actually does and spot mistakes.
A trace table tracks the values of variables and any output as an algorithm executes, step by step, usually one row per line of code (or per pass of a loop) executed. Trace tables help you understand how an algorithm works, check for logical errors, and predict the final output of a program.
Trace tables systematically record the value of every relevant variable, and any output produced, at each step of execution. Leave a cell truly blank if no output occurs on that line. Examiners are told to treat anything written in the output column as a claimed output, including a dash, a cross or a zero, so a placeholder will be marked as if you had output that value. Loops (including FOR loops) must be traced completely, including how the counter variable updates on each pass.
Tracing a loop, one row per pass1total = 02for i = 1 to 33 total = total + i4next i5print(total)The loop body runs three timesitotaloutput0Line 111Loop pass 123Loop pass 236Loop pass 36Line 5Leave a cell blank if that variable did not change on that line.Only write a value in the output column when something is actually printed.
Python· Trace this algorithm for total = 0, then a loop from 1 to 3
total = 0
for i in range(1, 4):
    total = total + i
print(total)
OCR ERL· Trace this algorithm for total = 0, then a loop from 1 to 3
total = 0
for i = 1 to 3
    total = total + i
next i
print(total)

Exam Phrasing: Use These Exact Phrases

  • ✓A syntax error breaks the rules of the programming language and the program will not run
  • ✓A logic error allows the program to run but produces an incorrect result
  • ✓A trace table tracks the value of each variable as the algorithm executes, line by line
  • ✓A structure diagram shows the subsections of a problem and how they link together
  • 💡When combining two conditions with AND or OR, always repeat the variable in both parts, e.g. num >= 1 AND num <= 100, not num >= 1 AND <= 100.
  • 💡Use >= and <= in Section B answers, not the mathematical ≥ / ≤ symbols, they aren't available on a standard keyboard and won't be credited.
  • 💡In a trace table, leave the output cell genuinely empty when nothing is output. Examiners treat any mark there as an output, including a dash, a cross or a zero, so a placeholder costs you the mark.
  • 💡A while loop repeats WHILE its condition is true, it stops as soon as the condition becomes false, not the other way round.
  • 💡Practise tracing FOR loops carefully, remember to update the counter variable on every single pass, including the final one that ends the loop.

Wrong:

if num >= 1 AND <= 100 then

Right:

if num >= 1 AND num <= 100 then

Why it matters: Each condition joined by AND/OR must independently reference the variable, the second part on its own is not a valid comparison.

Wrong:

A while loop repeats UNTIL the condition becomes true

Right:

A while loop repeats WHILE the condition is true, and stops as soon as it becomes false

Why it matters: This is a documented, common misconception, confusing while loops with do-until loops leads to wrongly predicting how many times a loop runs.

Wrong:

Writing "x" or a guess in a trace table cell when no output actually occurs on that line

Right:

Leave the cell blank when no output occurs

Why it matters: Examiners cannot tell if an entry means "no output" or is meant to be actual output, so ambiguous entries are not credited.

Question:

Trace the algorithm below for the values shown, completing a trace table for the variables total and count. [4]

Python· Trace this algorithm
total = 0
count = 1
while count <= 3:
    total = total + count
    count = count + 1
print(total)
OCR ERL· Trace this algorithm
total = 0
count = 1
while count <= 3
    total = total + count
    count = count + 1
endwhile
print(total)

💡 Attempt this before revealing the answer, practice makes perfect!

Model Answer:

total starts at 0 and count starts at 1. On each pass of the loop, total is increased by count, and count increases by 1. The loop runs while count <= 3, so it executes for count = 1, 2, and 3. After the loop, total = 1 + 2 + 3 = 6, and this is what gets printed.

Mark Scheme:

  • 1total correctly starts at 0 and count correctly starts at 1
  • 2total is correctly updated to 1, then 3, then 6 across the three passes
  • 3count is correctly updated to 2, 3, then 4 (causing the loop to stop)
  • 4Final printed output is correctly identified as 6

Tap a card to flip it. Use the arrow keys to move and space to flip.

Known 0 Still learning 0
1 / 12

Exam-style questions. Have a go, then reveal the mark scheme to check your answer.

1.A student writes the condition "if age >= 5 AND <= 16 then" to check that age is between 5 and 16 inclusive. Identify the error and rewrite the condition correctly.

[2]

2.Identify the input, processing, and output for an algorithm that asks the user for two numbers and prints their sum.

[3]

3.A school wants an algorithm that prints "Pass" for a mark of 40 or more, and "Fail" otherwise. Write this algorithm. Nested selection is not required here, but make sure your boundary condition is correct.

[3]
Python· Model answer
mark = int(input("Enter mark: "))
if mark >= 40:
    print("Pass")
else:
    print("Fail")
OCR ERL· Model answer
mark = int(input("Enter mark: "))
if mark >= 40 then
    print("Pass")
else
    print("Fail")
endif

4.Explain the difference between a syntax error and a logic error, using an example of each.

[4]

5.Complete a trace table for the algorithm below, showing the value of total after each pass of the loop and the final output. total = 0, for i = 1 to 4, total = total + i, next i, print(total)

[4]
Python· Trace this algorithm
total = 0
for i in range(1, 5):
    total = total + i
print(total)
OCR ERL· Trace this algorithm
total = 0
for i = 1 to 4
    total = total + i
next i
print(total)

6.Describe how a trace table is used to check an algorithm, and explain why this is useful before a program is written.

[6]

The examiners' reports are the most useful and least read of the three: they say what candidates actually got wrong each year. All files are hosted by OCR.

Past-paper questions grouped by topic

The same OCR questions, sorted by topic rather than by year (via Physics & Maths Tutor).

2025
Paper 2

A common misconception was that a while loop repeats until its condition becomes True. In fact a while loop is condition controlled and repeats WHILE the condition evaluates to True, stopping as soon as it becomes False, this was flagged as a precision issue affecting whether candidates could correctly predict loop behaviour.

Paper 2

When two conditions were joined with AND, a common mistake was writing the second condition without referencing the variable again (e.g. "num >= 1 AND <= 100"), which is not a syntactically valid comparison. The variable must be included in both parts of the condition.

2024
Paper 2

In trace table questions, candidates should leave a cell blank if no output is produced on that line, writing an ambiguous character instead (e.g. "x") meant examiners could not tell if it represented "no output" or an actual value, so no mark could be awarded.

Paper 2

The mathematical ≥ and ≤ symbols are not available on a standard keyboard and so are not accepted in written responses, candidates must use >= and <= instead, written the correct way around.