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.2. Understanding algorithms is fundamental to computational thinking and is central to both Paper 1 and Paper 2 of the OCR GCSE Computer Science exam.
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.
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.