Loading…
Loading…
Loading…
OCR GCSE Computer Science · J277
OCR J277 Check the specification (PDF) (opens in a new tab)
An algorithm is a finite sequence of precise steps for carrying out a task. Before writing one, establish what data it receives, what processing it must perform and what result it should produce. For example, an admission algorithm receives an age, compares it with an age limit and outputs an appropriate message.
Algorithms combine three basic constructs: sequence carries out instructions in order; selection chooses a branch according to a condition; iteration repeats instructions. Choosing and arranging these constructs turns a description of a task into a solution that can be followed without guessing.
Pseudocode describes an algorithm using structured text. It is more precise than ordinary prose but does not have to follow one programming language's exact syntax. This algorithm allows access to people aged 18 or over:
INPUT age
IF age >= 18 THEN
OUTPUT "Welcome to the site"
ELSE
OUTPUT "Sorry, this site is for users 18 and over"
ENDIF
The comparison produces either true or false. Exactly one branch runs, and execution then continues after ENDIF. The >= matters: someone aged exactly 18 meets the requirement.
OCR uses an exam reference language to present algorithms consistently. It is a particular notation, rather than the only possible way to write pseudocode. Read the notation used in a question carefully and follow any instruction about the form of your answer.
A high-level programming language, such as Python, expresses the steps in executable code. The same admission algorithm can be written as:
age = int(input("Enter your age: "))
if age >= 18:
print("Welcome to the site")
else:
print("Sorry, this site is for users 18 and over")
Here, input obtains text and int converts it to a whole number for comparison. Python uses colons and indentation to mark the branches; it does not use ENDIF. The underlying algorithm is unchanged even though its notation is different.
A flowchart shows the same flow of control visually. Start and end symbols are oval or rounded; input and output use parallelograms; processing uses rectangles; and decisions use diamonds. Arrows show which instruction happens next. A decision has labelled exits, such as Yes and No, so its alternative paths are unambiguous.
Consider an algorithm that inputs four scores and counts how many are at least 50. It needs a counter for passes and a loop that processes exactly four scores.
A decision inside a loop checks each score. The final count is output only after all four scores have been processed.
Begin at Start and follow the arrows. The first decision checks whether another score remains to be processed. Its Yes path enters the loop. The second decision checks the current score: only its Yes path increases passes. Both score branches then join before the number of processed scores increases.
The arrow returning to the first decision creates repetition. Once four scores have been processed, its No path leads to the final output and End. This placement ensures that the result is output once, not after every score.
Placing one construct inside another is called nesting. The pass-counting algorithm nests selection inside iteration: every repetition makes a decision about the current score.
passes = 0
FOR student = 1 TO 4
INPUT score
IF score >= 50 THEN
passes = passes + 1
ENDIF
NEXT student
OUTPUT passes
Initialising passes before the loop allows it to accumulate across all four inputs. The input and comparison belong inside the loop because they must happen for every student. The output belongs outside it because the required result is the final count.
A dry run makes the behaviour concrete. With scores 42, 71, 65 and 38:
| Input score | Is score >= 50? | passes after the decision |
|---|---|---|
| 42 | False | 0 |
| 71 | True | 1 |
| 65 | True | 2 |
| 38 | False | 2 |
The output is therefore 2. To interpret an algorithm, track the current variable values and the branch actually taken rather than reading every branch as though it executes.
Nesting can also place a decision inside one branch of another decision. Suppose concert tickets cost US$20 each. A purchase of 10–19 tickets receives a 10% discount, and a purchase of 20–25 tickets receives a 20% discount. Assume the quantity has already been checked to be a whole number from 1 to 25.
IF numberOfTickets < 10 THEN
discount = 0
ELSE
IF numberOfTickets < 20 THEN
discount = 0.1
ELSE
discount = 0.2
ENDIF
ENDIF
cost = numberOfTickets * 20 * (1 - discount)
OUTPUT cost
The inner decision is reached only when the first condition is false. At that point, the quantity must be at least 10. Consequently, the inner true branch covers 10–19 tickets, while its false branch covers 20–25. Read nested selection from the outside in: earlier decisions establish what is already known when a later decision is reached.
Nested loops repeat a whole inner loop for each repetition of an outer loop. A small theatre with three rows and four seats per row could generate its seat positions like this:
FOR row = 1 TO 3
FOR seat = 1 TO 4
OUTPUT row, seat
NEXT seat
NEXT row
For row 1, the inner loop outputs seats 1, 2, 3 and 4. The outer loop then advances to row 2, and the inner loop starts again at seat 1. Row 3 is processed in the same way. There are twelve outputs in total: three outer repetitions, each containing four inner repetitions.
A count-controlled loop suits this task because the number of repetitions is known. A condition-controlled loop suits a task whose stopping point depends on input, such as receiving values until the user enters -1. If -1 is a stopping signal rather than data, it must be excluded from any total or count.
To complete an algorithm, identify the purpose of the missing instruction and what must be true before and after it. If passes = ____ is missing its initial value in the pass-counting algorithm, the value is 0 because no scores have yet been processed. If the increment is missing, it must add 1 only when the score meets the pass condition.
To correct an algorithm, compare its actual behaviour with the requirement. If the admission algorithm uses age > 18, tracing an age of 18 reveals that access is wrongly refused. Changing the comparison to age >= 18 restores the intended boundary. Retest after making the change.
To refine an algorithm, adapt its steps to an improved or changed requirement. If the admission message must greet the user by name, add an input for the first name and include that name in the welcome output. The age condition need not change. Meaningful variable names and clear nesting also make the solution easier to understand and maintain.
Testing should check the decisions that matter. For admission, ages just below, at and above 18 check the access boundary. For the ticket algorithm, quantities 9, 10, 19 and 20 check where the discount changes. These targeted dry runs provide a stronger check than repeatedly trying values that all follow the same path.
Get unlimited access to all revision notes, key terms, and exam tips.
>= includes equality; > excludes it.Get unlimited access to all revision notes, key terms, and exam tips.
Use the representation requested by the question. If a programming language is required, use its actual syntax rather than mixing it with pseudocode.
Check equality at boundaries: age >= 18 includes someone aged exactly 18; age > 18 does not.
Label both exits from each flowchart decision and show the direction of control with arrows.
Indent nested constructs clearly. In Python, indentation determines which statements belong to a branch or loop.
Initialise a counter before the loop if it must accumulate across all repetitions. Put a final output after the loop if it should appear only once.
After completing or changing an algorithm, trace it again, including values at which its conditions change.
Algorithm
A finite sequence of precise steps for carrying out a task or solving a problem.
Pseudocode
A structured, text-based description of an algorithm that is not tied to the exact syntax of a particular programming language.
Flowchart
A diagram that represents an algorithm using symbols connected by arrows showing the flow of control.
Sequence
Carrying out instructions in their stated order.
Selection
Choosing which instructions to execute according to whether a condition is true or false.
Iteration
Repeating a group of instructions, either a set number of times or according to a condition.
Nesting
Placing one programming construct inside another, such as a selection inside a loop or a loop inside another loop.
Dry run
Following an algorithm manually with chosen input values, recording changes to variables and outputs.
Refinement
Improving or adapting an algorithm so that it meets its requirements more effectively or meets changed requirements.
Put your knowledge into practice — try past paper questions for Computer Science
Algorithm
A finite sequence of precise steps for carrying out a task or solving a problem.
Pseudocode
A structured, text-based description of an algorithm that is not tied to the exact syntax of a particular programming language.
Flowchart
A diagram that represents an algorithm using symbols connected by arrows showing the flow of control.
Sequence
Carrying out instructions in their stated order.
Selection
Choosing which instructions to execute according to whether a condition is true or false.
Iteration
Repeating a group of instructions, either a set number of times or according to a condition.
Nesting
Placing one programming construct inside another, such as a selection inside a loop or a loop inside another loop.
Dry run
Following an algorithm manually with chosen input values, recording changes to variables and outputs.
Refinement
Improving or adapting an algorithm so that it meets its requirements more effectively or meets changed requirements.