You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
This lesson introduces the concept of algorithms and how they can be represented, covering the key knowledge required for OCR J277 Section 2.1. Understanding algorithms is fundamental to computational thinking and is central to both Paper 1 and Paper 2 of the OCR GCSE Computer Science exam.
This lesson mainly develops AO1 (recalling what an algorithm is and its defining properties) and AO3 (designing and expressing a step-by-step solution as pseudocode or a flowchart), with some AO2 when you apply the idea to describe a process for a given task.
An algorithm is a finite sequence of well-defined, step-by-step instructions designed to perform a specific task or solve a particular problem. Algorithms are not computer programs — they are abstract descriptions of a process that can be implemented in any programming language or even followed by a person.
Key properties of an algorithm:
| Property | Description |
|---|---|
| Finite | It must eventually terminate after a finite number of steps |
| Definite | Each step must be precisely and unambiguously defined |
| Inputs | It may have zero or more inputs |
| Outputs | It must produce at least one output |
| Effective | Each step must be basic enough to be carried out |
OCR Exam Tip: If asked to "state what is meant by an algorithm", always mention that it is a set of step-by-step instructions to solve a problem or complete a task. The word "step-by-step" is crucial for full marks.
Before writing any code, programmers plan their solutions using algorithms. This is part of computational thinking, which involves:
By planning with algorithms first, programmers can identify potential issues early and ensure their solution is logical before committing to code.
Pseudocode is a way of writing algorithms using a structured, English-like syntax that resembles a programming language but is not tied to any specific one. OCR uses its own pseudocode reference language in exams.
Here is an example of a simple algorithm in OCR pseudocode that finds the largest of three numbers:
a = input("Enter first number: ")
b = input("Enter second number: ")
c = input("Enter third number: ")
if a >= b AND a >= c then
print("Largest is " + a)
elseif b >= a AND b >= c then
print("Largest is " + b)
else
print("Largest is " + c)
endif
And the equivalent in Python:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
if a >= b and a >= c:
print("Largest is", a)
elif b >= a and b >= c:
print("Largest is", b)
else:
print("Largest is", c)
OCR Exam Tip: In the exam, you may be asked to write, complete, or interpret pseudocode. You do not need to write perfect Python — OCR's pseudocode reference language is accepted. However, you must be consistent in whichever style you use.
A flowchart is a graphical representation of an algorithm using standard symbols:
| Symbol | Shape | Purpose |
|---|---|---|
| Terminator | Rounded rectangle (oval) | Start or end of the algorithm |
| Process | Rectangle | An instruction or action to perform |
| Decision | Diamond | A yes/no or true/false question |
| Input/Output | Parallelogram | Data being entered or displayed |
| Flow line | Arrow | Shows the direction of flow |
A flowchart for this algorithm would flow as follows:
| Feature | Pseudocode | Flowchart |
|---|---|---|
| Format | Text-based | Graphical/visual |
| Best for | Longer, complex algorithms | Shorter algorithms or showing decision logic |
| Ease of writing | Quick to write | Can be time-consuming to draw neatly |
| Exam usage | Very common in Paper 2 | Common for shorter questions |
| Modification | Easy to edit lines | May require redrawing |
Problem: Write an algorithm that asks the user for a password. If the password is correct ("secret123"), display "Access granted". Otherwise, display "Access denied".
OCR Pseudocode:
password = input("Enter password: ")
if password == "secret123" then
print("Access granted")
else
print("Access denied")
endif
Python:
password = input("Enter password: ")
if password == "secret123":
print("Access granted")
else:
print("Access denied")
OCR Exam Tip: Always draw flowchart symbols correctly — use diamonds for decisions (not rectangles), and remember to label the Yes/No branches on decision diamonds. Incorrect symbols will lose marks even if the logic is correct.
Let us take a realistic problem and work through decomposition, abstraction and algorithm design.
Problem: A school canteen wants a simple program that calculates the cost of a meal. Pupils choose a main course, a drink and a dessert. Each item has a fixed price. Pupils entitled to free school meals pay nothing; all others pay the total. The program must display the final amount owed.
Step 1 — Decomposition. Break the problem into sub-problems:
Step 2 — Abstraction. Ignore irrelevant details such as the canteen's colour scheme, the pupil's year group, or the day of the week. These do not affect the calculation.
Step 3 — Algorithm in OCR pseudocode.
mainPrice = input("Enter main course price: ")
drinkPrice = input("Enter drink price: ")
dessertPrice = input("Enter dessert price: ")
freeMeals = input("Free school meals? (yes/no): ")
subtotal = mainPrice + drinkPrice + dessertPrice
if freeMeals == "yes" then
print("Amount owed: 0")
else
print("Amount owed: " + str(subtotal))
endif
Step 4 — Trace table for a sample run. Main course £3.20, drink £0.80, dessert £1.00, not entitled to free meals.
| Step | mainPrice | drinkPrice | dessertPrice | freeMeals | subtotal | Output |
|---|---|---|---|---|---|---|
| 1 | 3.20 | |||||
| 2 | 3.20 | 0.80 | ||||
| 3 | 3.20 | 0.80 | 1.00 | |||
| 4 | 3.20 | 0.80 | 1.00 | "no" | ||
| 5 | 3.20 | 0.80 | 1.00 | "no" | 5.00 | |
| 6 | 3.20 | 0.80 | 1.00 | "no" | 5.00 | "Amount owed: 5.00" |
Notice how a trace table forces you to think about each line in turn. This is exactly how the exam board wants you to approach programming questions.
Study this algorithm carefully:
number = input("Enter a number: ")
if number > 0 then
print("Positive")
elseif number < 0 then
print("Negative")
endif
What is wrong? The algorithm does not handle the case where number equals zero. A robust algorithm must cover every possible input.
Corrected version:
number = input("Enter a number: ")
if number > 0 then
print("Positive")
elseif number < 0 then
print("Negative")
else
print("Zero")
endif
Write an algorithm that asks the user to enter their age and tells them whether they can vote in a UK general election (age 18 or over).
Model solution:
age = input("Enter your age: ")
if age >= 18 then
print("You can vote")
else
print("You are too young to vote")
endif
Many students confuse these two terms. They are related but distinct:
You can write one algorithm and then implement it in many different programming languages. If an exam question asks you to "write an algorithm", you can choose to use pseudocode, a flowchart or a programming language. Pseudocode is usually quickest and safest for marks.
The examiner marks flowcharts by shape as well as by logic. Common mistakes that lose marks:
When drawing a flowchart under time pressure, sketch it once in pencil, check the symbols, then go over in pen.
OCR uses precise command verbs that tell you exactly what kind of answer is expected.
| Command Word | What It Means | Typical Marks |
|---|---|---|
| State | Give a brief factual answer, usually one word or phrase | 1 mark each |
| Identify | Point out a specific item, often from a diagram or list | 1 mark each |
| Describe | Give a step-by-step or feature-by-feature account | 2–4 marks |
| Explain | Give reasons — often linked with "because", "so that", "therefore" | 3–6 marks |
| Compare | Show similarities and differences between two items | 3–6 marks |
| Analyse | Examine in detail, break into parts, judge significance | 4–8 marks |
Always match your level of detail to the command word. Writing a four-sentence essay when the question says "state" wastes time; writing one sentence when the question says "explain" loses marks.
Exam-style question: Describe what is meant by an algorithm and explain why algorithms are written before code is produced. [4 marks]
Grade 3–4 response:
An algorithm is a set of instructions. People use algorithms to help them plan a program before they write it in Python or another language.
Examiner comment: Identifies the basic idea but is vague. Misses "step-by-step" and "solves a problem". Reason for using algorithms is present but not developed. Likely 1–2 marks.
Grade 5–6 response:
An algorithm is a step-by-step sequence of instructions designed to solve a problem or complete a task. Algorithms are written before the code because it allows the programmer to plan the logic of the solution without worrying about the syntax of a specific programming language. This helps them find errors in the logic early.
Examiner comment: Clear definition using "step-by-step" and "problem". Two developed reasons for planning. Would achieve 3 marks.
Grade 7–9 response:
An algorithm is a finite, unambiguous sequence of step-by-step instructions that solves a specific problem or completes a defined task. Algorithms are developed prior to writing code for three main reasons. First, the programmer can focus on the logic (using pseudocode or flowcharts) without being distracted by the syntactic rules of any one programming language. Second, a well-designed algorithm can be implemented in multiple languages, giving portability. Third, trace tables and dry-runs on the algorithm allow logical errors to be detected and corrected before expensive coding and debugging begins, which is particularly valuable in large software projects.
Examiner comment: Precise definition with mark-scheme key words. Three clearly-linked reasons. Uses subject-specific terminology ("syntactic rules", "trace tables", "portability"). Full 4 marks.
Every algorithm you will ever write is built from just three control structures. Recognising them helps you read any piece of pseudocode.
if, elseif and else. Selection is drawn as a diamond in a flowchart.for i = 0 to n) when you know how many repeats are needed, and a condition-controlled loop (while condition) when you repeat until something becomes true or false.The following flowchart shows a count-controlled loop that prints the numbers 1 to 5. Notice the decision diamond that sends flow back up while the condition holds.
flowchart TD
A([Start]) --> B["count = 1"]
B --> C{"count <= 5?"}
C -- Yes --> D["print count"]
D --> E["count = count + 1"]
E --> C
C -- No --> Z([End])
Being able to name whether a step is sequence, selection or iteration is exactly the kind of understanding OCR expects when it asks you to describe or complete an algorithm.
Choosing between the two loop types is a common exam decision. Use a count-controlled for loop when the number of repetitions is known in advance — for example, "process all 30 pupils in a class" or "repeat exactly 12 times". Use a condition-controlled while loop when you cannot know in advance how many repeats are needed — for example, "keep asking for a password until the user gets it right" or "keep reading data until the end of the file is reached". Picking the wrong loop type is a frequent source of lost marks: a while loop written where a fixed for loop is expected still works, but a for loop cannot naturally express "repeat an unknown number of times".
Specimen question modelled on the OCR J277 paper format.
A cinema charges £8 for an adult ticket and £5 for a child ticket. A child is anyone aged under 15.
(a) Write an algorithm, using pseudocode or a flowchart, that asks the user for their age, works out the correct ticket price, and outputs it. [4 marks]
(b) State the name of the control structure used to choose between the two prices. [1 mark]
Mark scheme (modelled on the OCR format):
Part (a) — award up to 4 marks:
if ... then ... else) that tests the age against 15. (1)Part (b): selection (accept "conditional statement" / "IF statement"). (1)
Top-band response (5/5):
age = input("Enter your age: ")
if age < 15 then
price = 5
else
price = 8
endif
print("Ticket price: " + str(price))
Examiner-style commentary: This response earns all four marks in part (a) — the input, the selection, the correct boundary handling (< 15 means a 15-year-old pays £8), and the output are all present. The single most common mark-loss here is the boundary: writing age <= 15 would wrongly charge a 15-year-old the child price. Always test the exact edge value the question gives you.
Some students think a flowchart and pseudocode represent different algorithms. They do not — they are two notations for the same algorithm. The same logic (input, selection, output) can be drawn as a flowchart or written as pseudocode, and both should produce the same result when followed. Choose whichever the question asks for; if the choice is yours, pseudocode is usually faster to produce under time pressure.
Pseudocode is written for humans to read and plan with. A computer cannot run pseudocode — it must first be translated into real source code in a language such as Python, and then that code is translated into machine code before it executes. Pseudocode sits at the planning stage, one step before real code.
This content is aligned with OCR GCSE Computer Science (J277) specification section 2.1 Algorithms. For the most accurate and up-to-date information, please refer to the official OCR specification document.