- Define an algorithm and its characteristics (1.2.1)
- Express algorithms as flowcharts, written descriptions and code, with benefits and drawbacks (1.2.2–1.2.5)
- Use sequence, selection and iteration (1.2.6)
- Trace, find and fix errors, translate and design algorithms (1.2.7–1.2.12)
What an algorithm is
An algorithm is a precise, ordered set of steps that solves a problem or completes a task. A good algorithm is precise (each step is clear and unambiguous) and finite (it always finishes). Its purpose is to describe a solution so that a person or a computer can follow it and get the right result every time.
Three ways to express an algorithm
Flowcharts
| Symbol | Meaning |
|---|---|
| Terminator (rounded rectangle) | Start or end |
| Rectangle | A process |
| Rectangle with double side lines | A sub-process (a separate, defined process) |
| Diamond | A decision (yes/no) |
| Parallelogram | Input or output |
| Arrow | The flow of logic |
| Label | Text on an arrow, such as Yes or No |
The specification's Appendix 2 also includes a connector symbol for linking parts of a flowchart that cannot easily be joined with an arrow.
Benefits: visual and easy to follow, good for non-technical audiences, shows decisions clearly. Drawbacks: slow to draw and edit, large algorithms become hard to read.
Written descriptions
Numbered steps using hierarchical markers, for example 1, 1.1, 1.2, 2, to show sequence and which steps belong inside others.
Benefits: quick to write, needs no special tools, easy for anyone to read. Drawbacks: everyday language can be ambiguous, and long descriptions are hard to follow.
Code
Python 3.10 or later, using the commands in Appendix 2 of the specification.
Benefits: precise, can be run and tested. Drawbacks: needs programming knowledge to read, and syntax details can hide the logic.
Controlling the order of steps
- Sequence: steps run one after another in order.
- Selection: a decision chooses which steps run (
if,elif,else,match/case). - Iteration: steps repeat (
for,while).
The order matters. A correct set of steps in the wrong order gives the wrong result.
Working with algorithms
- Determine the purpose: read the steps and say what problem they solve.
- Trace: work through the steps with a given input and record each value to find the output.
- Find errors: look for steps in the wrong order, wrong conditions, missing steps or loops that never end.
- Correct errors: change the step or condition so it gives the right result.
- Translate: turn a flowchart into code, code into a written description, and so on.
- Design: build your own algorithm from actions in sequence, with selection and iteration.
# Trace this with mark = 72
mark = int(input("Mark: "))
if mark >= 70:
print("Distinction")
elif mark >= 50:
print("Pass")
else:
print("Fail")
# Output: Distinction