Designing and Refining Algorithms
Inputs, Processes and Outputs
Before writing an algorithm, identify its inputs (data it receives), processes (operations it performs), and outputs (results it produces). This IPO model defines what the algorithm must do before any code is written.
Worked example — an algorithm that calculates the area of a rectangle:
| Component | Detail |
|---|---|
| Inputs | Length (number), Width (number) |
| Process | Multiply length by width |
| Output | Area (number) |
A structure diagram shows how a problem is decomposed into sub-problems as a hierarchy of boxes. Each box is a sub-task; lines show which sub-tasks belong to which parent task.
Calculate rectangle area
├── Get length from user
├── Get width from user
├── Calculate area = length × width
└── Display area
Producing this diagram before coding ensures every sub-task is accounted for. OCR exams may ask you to complete or extend a partially drawn structure diagram.
Pseudocode — Writing Algorithms in Plain Language
Pseudocode is a structured, language-independent way to write algorithms. It is not valid code in any specific language, but it is precise enough to translate directly into one.
Common pseudocode conventions used in OCR J277:
| Construct | Pseudocode example |
|---|---|
| Assignment | score = 0 |
| Output | OUTPUT "Enter a number: " |
| Input | x = INPUT() |
| Selection | IF x > 0 THEN ... ELSE ... END IF |
| Count-controlled loop | FOR i = 1 TO 10 ... END FOR |
| Condition-controlled loop | WHILE x != 0 ... END WHILE |
Worked example — algorithm to find the largest of three numbers:
largest = a
IF b > largest THEN
largest = b
END IF
IF c > largest THEN
largest = c
END IF
OUTPUT "Largest is " + largest
Pseudocode is read and written in exams. Practise converting between pseudocode, flowcharts, and actual code so you can move fluently between all three.
Flowcharts — Visualising Algorithm Flow
A flowchart uses standard symbols to show the sequence, decisions, and loops in an algorithm. OCR J277 requires knowledge of these six symbols:
| Symbol | Shape description | Used for |
|---|---|---|
| Terminal | Rounded rectangle (oval/stadium) | Start and End of the algorithm |
| Process | Rectangle | An action or calculation |
| Input / Output | Parallelogram | Reading input or displaying output |
| Decision | Diamond (rhombus) | A yes/no or true/false branch |
| Sub program | Rectangle with double vertical bars | Calling a named sub-program |
| Line (flow) | Arrow | Direction of execution |
Worked example — flowchart for "find the larger of two numbers A and B":
[START]
│
[INPUT A, B]
│
◇ A > B? ◇
YES╱ ╲NO
╱ ╲
[OUTPUT A] [OUTPUT B]
╲ ╱
╲ ╱
[ END ]
A decision diamond always has exactly two exit paths, labelled YES/NO or TRUE/FALSE. Every path through the flowchart must eventually reach the terminal.
Trace Tables — Following an Algorithm Step by Step
A trace table records the value of every variable after each step of an algorithm. Trace tables let you verify whether an algorithm produces correct output — and pinpoint exactly where it goes wrong if it does not.
Worked example — trace the algorithm below for input n = 5:
total = 0
i = 1
WHILE i <= n
total = total + i
i = i + 1
END WHILE
OUTPUT total
| Step | i | total | Note |
|---|---|---|---|
| Start | 1 | 0 | Initial values |
| Loop 1 | 1 | 1 | total = 0+1; i checked: 1≤5 |
| After inc | 2 | 1 | i = 1+1 |
| Loop 2 | 2 | 3 | total = 1+2 |
| After inc | 3 | 3 | |
| Loop 3 | 3 | 6 | total = 3+3 |
| After inc | 4 | 6 | |
| Loop 4 | 4 | 10 | total = 6+4 |
| After inc | 5 | 10 | |
| Loop 5 | 5 | 15 | total = 10+5 |
| After inc | 6 | 15 | i = 5+1; now 6>5, loop ends |
| Output | — | — | 15 |
Output: 15 (which equals 1+2+3+4+5). The algorithm correctly sums integers from 1 to n.
Worth saving these ideas?
Turn what you've read into instant revision cards. Free to get started.
Syntax Errors and Logic Errors
Two categories of error arise when algorithms are written as programs:
| Error type | What it is | Effect on the program |
|---|---|---|
| Syntax error | Breaks the grammatical rules of the programming language | The program cannot be run or translated |
| Logic error | The program runs but produces incorrect or unexpected output | The program runs but gives wrong results |
Syntax error example — missing THEN keyword:
IF score > 10 ← missing THEN
OUTPUT "Pass"
END IF
The translator cannot parse this. It will report an error before the program runs.
Logic error example — wrong comparison operator:
IF score > 10 THEN ← should be >= 10 for "pass at 10"
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
END IF
A student with score exactly 10 would incorrectly be told "Fail". The program runs without errors but produces wrong output for boundary inputs.
Syntax errors are caught before the program runs. Logic errors only reveal themselves when you test with carefully chosen data.
Nesting Selection and Iteration
Nesting means placing one structure inside another — an IF inside another IF, or a loop inside another loop. OCR J277 requires you to read, write, and trace nested structures.
Nested selection — award a grade based on score and bonus flag:
IF score >= 70 THEN
IF bonus == TRUE THEN
grade = "A*"
ELSE
grade = "A"
END IF
ELSE
IF score >= 50 THEN
grade = "B"
ELSE
grade = "C"
END IF
END IF
Nested iteration — print a multiplication table:
FOR row = 1 TO 5
FOR col = 1 TO 5
OUTPUT row * col
END FOR
END FOR
The inner loop runs completely for each iteration of the outer loop. For a 5×5 table, the inner loop body executes 25 times in total.
In a trace table, track every variable — inner loop counter, outer loop counter, and any accumulator — separately. Missing a variable is the most common trace-table error.
Common Exam Mistakes
1. Drawing flowchart decisions as rectangles
Decision diamonds must be diamond-shaped and have exactly two labelled exits (YES/NO). Using a rectangle for a decision loses marks even if the logic is correct.
2. Completing a trace table top to bottom without updating correctly
Update variables at the moment the line executes, not at the end of the loop. After i = i + 1, record the new value of i on the same row before moving on.
3. Confusing syntax and logic errors
If a program will not run, the error is a syntax error. If it runs but gives wrong output, the error is a logic error. Exam questions often ask you to classify an error — the key test is whether the program executes at all.
4. Using vague process boxes in flowcharts
A process box should contain a specific action: area = length * width, not "calculate area". The more precise the box, the closer the flowchart is to actual code.
| Mistake | Correction |
|---|---|
| Diamond has three exits | Decision must have exactly two paths (YES/NO) |
| Trace table skips intermediate values | Record every variable change as it happens |
| "It runs but gives wrong answer" classified as syntax error | That is a logic error — syntax errors prevent execution |
Generate revision on any topic you study
Type any topic you're studying and Aicademy generates a complete lesson, quiz, and flashcard set — personalised to your level.
Lessons on anything
Structured, level-matched lessons on any topic you study
Practice quizzes
Find out what you actually know before the exam does
Flashcard sets
Lock in key concepts with instant revision cards
Ask Aica
Stuck on something? Get a clear explanation, any time
Computational Thinking
Searching Algorithms
Related lessons
7 Slides
7 Slides
8 Slides
7 Slides