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
Structure diagrams
Flowcharts
Writing and refining algorithms
for i in range(1, 11):
if i % 2 == 0:
print(i)for i = 1 to 10
if i MOD 2 == 0 then
print(i)
endif
next iIdentifying common errors
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
total = 0
for i in range(1, 4):
total = total + i
print(total)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]
total = 0
count = 1
while count <= 3:
total = total + count
count = count + 1
print(total)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.
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]mark = int(input("Enter mark: "))
if mark >= 40:
print("Pass")
else:
print("Fail")mark = int(input("Enter mark: "))
if mark >= 40 then
print("Pass")
else
print("Fail")
endif4.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]total = 0
for i in range(1, 5):
total = total + i
print(total)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).
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.
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.
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.
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.