SLIDE 1
CSZone.co.uk
Click to reveal · Arrow keys also work
OCR J277 · Component 2 · Topic 2.1.2b

Designing Algorithms
Pseudocode

Keywords · Sequence · Selection · Iteration · Trace Tables · Errors

CSZone OCR GCSE Computer Science J277
Learning Objectives

By the end of this video you will be able to...

Write pseudocode using correct OCR keywords and syntax — including proper indentation, assignment, comparison operators and all three programming constructs
Create pseudocode for algorithms involving sequence, selection and iteration — including nested IF statements and both FOR and WHILE loops
Complete partial pseudocode by identifying what the missing lines must do from the surrounding context, and refine existing algorithms by adding input validation and improving robustness
Create and use trace tables to manually step through pseudocode, tracking every variable change, to determine the output of an algorithm or find a logic error
Identify syntax errors and logic errors in pseudocode, state which type each is, explain why, and suggest the correct fix
⚡ Pseudocode writing and trace tables appear in virtually every OCR J277 Component 2 exam paper — usually worth 6–10 marks combined.
Pseudocode Basics

What is pseudocode and how does OCR use it?

WHAT IS PSEUDOCODE?
Pseudocode is a structured, English-like notation for writing algorithms. It is not a real programming language — it cannot be compiled or run. It is a design tool that is precise enough to be translated into any real language. OCR use their own version called the Exam Reference Language (ERL), but any clear pseudocode is accepted in the exam.
OCR KEYWORD REFERENCE
INPUTOUTPUTIFTHENELSE IFELSEEND IFWHILEEND WHILEFORTONEXTANDORNOTMODDIV
OPERATORS IN OCR PSEUDOCODE
ASSIGNMENT
x = 5
COMPARISON
==   !=   <   >   <=   >=
INTEGER DIVIDE
7 DIV 2 = 3
REMAINDER
7 MOD 2 = 1
GOLDEN RULES FOR WRITING PSEUDOCODE
Indent everything inside a loop or IF block — one level per nesting level. This shows the examiner the structure.
Close every block — every IF needs END IF, every WHILE needs END WHILE, every FOR needs NEXT.
Keywords in capitals — INPUT, OUTPUT, IF, WHILE, FOR and so on. Variables in lowercase.
Use = for both assignment and comparison — OCR pseudocode uses = for both. Context makes it clear which is meant.
⚡ You don't have to use OCR's exact keywords — the mark scheme accepts any clear, unambiguous pseudocode. But using keywords like IF/THEN/END IF and WHILE/END WHILE is strongly recommended because the examiner immediately recognises the structure.
Sequence

Sequence — steps in strict order

WHAT IS SEQUENCE?
Sequence means lines of pseudocode execute one after another, in the exact order they are written. There are no branches and no repetition. Every program — no matter how complex — contains at least some sequence. The order of lines matters: you cannot output a calculated result before calculating it.
EXAMPLE 1 — AREA OF A RECTANGLE
// Input length and width, output area INPUT length INPUT width area = length * width OUTPUT "Area: ", area
EXAMPLE 2 — CELSIUS TO FAHRENHEIT
// Convert temperature INPUT celsius fahrenheit = (celsius * 9 / 5) + 32 OUTPUT "Fahrenheit: ", fahrenheit
Note the brackets around the multiplication and division — without them, operator precedence would calculate celsius * (9/5+32) which is wrong.
EXAMPLE 3 — SWAP TWO VARIABLES
// Swap the values of a and b INPUT a INPUT b temp = a // save a a = b // a gets b's value b = temp // b gets old a OUTPUT a, b
The swap algorithm requires a temporary variable. Without temp, assigning a = b first destroys the original value of a. Order matters.
⚡ The swap algorithm is a classic exam question. Remember: you always need a temporary variable to hold one value while you overwrite it. Writing a = b then b = a is a logic error — it sets both to b's original value.
Selection

Selection — IF, ELSE IF, ELSE, END IF

BASIC IF — ONE CONDITION
INPUT age IF age >= 18 THEN OUTPUT "Access granted" ELSE OUTPUT "Access denied" END IF
CHAINED ELSE IF — MULTIPLE CONDITIONS
INPUT score IF score >= 90 THEN OUTPUT "Grade A" ELSE IF score >= 70 THEN OUTPUT "Grade B" ELSE IF score >= 50 THEN OUTPUT "Grade C" ELSE OUTPUT "Fail" END IF
The chained ELSE IF tests conditions in order. As soon as one is true, its block runs and all remaining conditions are skipped. The ELSE at the end is the catch-all — it runs only if every condition above was false.
NESTED IF — IF INSIDE AN IF
INPUT age INPUT hasTicket IF age >= 18 THEN IF hasTicket == TRUE THEN OUTPUT "Entry allowed" ELSE OUTPUT "Buy a ticket first" END IF ELSE OUTPUT "Too young" END IF
Notice the double indentation inside the nested IF. Each level of nesting gets one more indent. This makes the structure clear to both the programmer and the examiner.
⚡ Every IF needs one END IF — and nested IFs need one END IF each. A common mistake is to write a nested IF but only close it with a single END IF, leaving the outer IF unclosed. Count your IFs and count your END IFs — they must match.
Iteration — FOR

FOR loops — count-controlled iteration

COUNT-CONTROLLED ITERATION
A FOR loop repeats a fixed, known number of times. You specify the start value, the end value, and the loop variable. It's called count-controlled because the number of iterations is determined before the loop begins. Use FOR when you know exactly how many times to repeat.
STRUCTURE
FOR i = start TO end // body — runs (end - start + 1) times NEXT i
EXAMPLE — 5 TIMES TABLE
FOR i = 1 TO 10 OUTPUT i, " x 5 = ", i * 5 NEXT i // Outputs: 1 x 5 = 5, 2 x 5 = 10 ... 10 x 5 = 50
EXAMPLE — SUM OF 1 TO N
INPUT n total = 0 FOR i = 1 TO n total = total + i NEXT i OUTPUT total
Note: total = 0 must be set before the loop — this is called initialisation.
EXAMPLE — INPUT 5 NUMBERS AND FIND AVERAGE
total = 0 FOR i = 1 TO 5 INPUT num total = total + num NEXT i average = total / 5 OUTPUT "Average: ", average
KEY POINTS ABOUT FOR LOOPS
The loop variable (i) automatically increments by 1 each time
NEXT marks the end of the loop body — put it at the same indent level as FOR
Anything after NEXT only runs once, after all iterations are complete
FOR = count-controlled. Use it when you know in advance exactly how many times the loop runs. If the number of repetitions depends on user input or a condition that may vary, use a WHILE loop instead.
Iteration — WHILE

WHILE loops — condition-controlled iteration

CONDITION-CONTROLLED ITERATION
A WHILE loop keeps repeating as long as a condition is true. The condition is tested at the start of each iteration — before the body runs. If the condition is false on the first check, the body never runs at all. Use WHILE when you don't know in advance how many repetitions are needed.
STRUCTURE
WHILE <condition is true> // body — runs while condition holds END WHILE
EXAMPLE — PASSWORD CHECKER
password = "" WHILE password != "Secret99" OUTPUT "Enter password:" INPUT password END WHILE OUTPUT "Access granted"
The loop repeats until the user enters the correct password. We can't use a FOR loop here because we have no idea how many attempts it will take. When the condition becomes false, execution continues after END WHILE.
EXAMPLE — COUNT-UP WITH WHILE (compare to FOR)
// Same as FOR i = 1 TO 5 i = 1 WHILE i <= 5 OUTPUT i i = i + 1 // must increment manually END WHILE
EXAMPLE — KEEP ASKING UNTIL VALID INPUT
INPUT age WHILE age < 0 OR age > 120 OUTPUT "Invalid. Enter age (0–120):" INPUT age END WHILE OUTPUT "Age accepted: ", age
WHILE = condition-controlled. Critical: if you use WHILE as a manual count-up, you must initialise the counter before the loop and increment it inside the loop — or it loops forever. Forgetting the increment inside a WHILE loop is one of the most common logic errors in the exam.
Writing Pseudocode

Writing pseudocode from scratch

THE PROCESS — 4 STEPS
Step 1: Identify IPO — what goes in, what the program must do, what comes out
Step 2: Write the sequence — inputs first, then calculations, then outputs
Step 3: Add selection if needed — IF/ELSE IF/ELSE/END IF for branching
Step 4: Add iteration if needed — FOR or WHILE depending on whether count is known
PROBLEM: find the highest of 3 numbers
IPO: IN — three numbers  ·  PROCESS — compare them  ·  OUT — the largest
SOLUTION
INPUT a INPUT b INPUT c largest = a IF b > largest THEN largest = b END IF IF c > largest THEN largest = c END IF OUTPUT "Largest: ", largest
PROBLEM: count how many numbers in a list are above average
IPO: IN — 5 numbers  ·  PROCESS — calculate average, then count how many exceed it  ·  OUT — the count
// Pass 1: calculate average total = 0 FOR i = 1 TO 5 INPUT nums[i] total = total + nums[i] NEXT i avg = total / 5 // Pass 2: count above average count = 0 FOR i = 1 TO 5 IF nums[i] > avg THEN count = count + 1 END IF NEXT i OUTPUT count
⚡ When a problem needs two passes through data, you often need two loops. Plan on paper first — identify which variables need initialising before each loop, and what each loop is responsible for calculating.
Completing & Refining

Completing and refining pseudocode

COMPLETING — FILL IN THE MISSING LINE
This algorithm should output the factorial of n (e.g. 4! = 1×2×3×4 = 24). One line is missing. Read the surrounding code and work out what must go there.
INPUT n result = 1 FOR i = 1 TO n ??? ← what goes here? NEXT i OUTPUT result
Answer:   result = result * i
COMPLETING — ANOTHER EXAMPLE
This should keep asking for a number until the user enters one between 1 and 10. Fill in the condition.
INPUT num WHILE ??? OUTPUT "Enter a number between 1 and 10" INPUT num END WHILE
Answer:   num < 1 OR num > 10
REFINING — IMPROVING AN EXISTING ALGORITHM
Refining means improving a working algorithm — adding validation, handling edge cases, or making it more efficient. Here the original algorithm has no validation.
ORIGINAL — no validation
INPUT percentage OUTPUT "You scored: ", percentage
REFINED — with input validation
INPUT percentage WHILE percentage < 0 OR percentage > 100 OUTPUT "Invalid. Enter 0–100:" INPUT percentage END WHILE OUTPUT "You scored: ", percentage
⚡ For completing questions: look at what the variables are, what the output should be, and what's already written. The missing line must be consistent with everything around it. For refining: the most common improvement is wrapping an INPUT in a WHILE loop that rejects values outside a valid range.
Trace Tables

Trace tables — tracking variables step by step

WHAT IS A TRACE TABLE?
A trace table is a technique for manually executing pseudocode line by line, recording every variable change in a table. Each variable gets a column. You add a new row every time a variable changes. Trace tables are used to: verify an algorithm produces the correct output, identify logic errors, and answer "what does this output?" exam questions.
HOW TO CREATE A TRACE TABLE — 4 STEPS
Step 1: Identify every variable in the algorithm. Each one gets a column. Add an OUTPUT column too.
Step 2: Execute each line in order, using the current values of variables.
Step 3: When a variable changes value, write the new value in its column on the current row.
Step 4: Leave cells blank when a variable hasn't changed. Only write a value when it changes.
⚡ Only write a value in a cell when it changes. Writing every variable's value on every row — even when it hasn't changed — is one of the most common trace table mistakes and makes the table harder to read.
THE ALGORITHM WE WILL TRACE
x = 1 total = 0 WHILE x <= 4 total = total + x x = x + 1 END WHILE OUTPUT total
THE BLANK TRACE TABLE — COLUMNS IDENTIFIED
Line / StepxtotalOUTPUT
Start
Initialise
Loop 1
Loop 2
Loop 3
Loop 4
End
Variables: x and total. One output. 4 iterations of the loop.
This algorithm adds up 1+2+3+4. Let's trace it completely on the next slide. Before we do — predict the answer: x starts at 1 and increments. total accumulates. When x reaches 5, the condition x <= 4 is false and the loop exits.
Trace Tables

Trace table — building it step by step

THE ALGORITHM
x = 1 total = 0 WHILE x <= 4 total = total + x x = x + 1 END WHILE OUTPUT total
COLOUR KEY
Yellow = value changed this row
Green = output produced
Blank = variable unchanged
The loop runs 4 times. When x reaches 5 the condition x <= 4 is FALSE — loop exits and total is output. Table builds click by click →
TRACE TABLE — BUILDING LIVE
StepxtotalOUTPUT
Initialise: x = 11
Initialise: total = 00
ITERATION 1 — check: 1≤4 ✓
total = total + x (0+1)1
x = x + 1 (1+1)2
ITERATION 2 — check: 2≤4 ✓
total = total + x (1+2)3
x = x + 1 (2+1)3
ITERATION 3 — check: 3≤4 ✓
total = total + x (3+3)6
x = x + 1 (3+1)4
ITERATION 4 — check: 4≤4 ✓
total = total + x (6+4)10
x = x + 1 (4+1)5
check: 5≤4 ✗ — EXIT LOOP → OUTPUT total10
COMMON EXAM QUESTIONS
"What does this algorithm output?" → 10
"What is the value of total after iteration 3?" → 6
⚡ Check the WHILE condition at the top of every iteration — the moment it's false, the loop exits immediately.
Exam-Style Questions

Pseudocode — 2.1.2b

Question 1
Write pseudocode for an algorithm that inputs a password repeatedly until the user enters "OpenSesame". Once correct, output "Welcome!".
3 marks
password = "" WHILE password != "OpenSesame" OUTPUT "Enter password:" INPUT password END WHILE OUTPUT "Welcome!"
Marks: WHILE with correct condition ✓ · INPUT inside loop ✓ · OUTPUT after END WHILE ✓
Question 2
Complete the pseudocode below. It should output the sum of all even numbers from 2 to 20.

total = 0
FOR i = 2 TO 20
  IF ??? THEN
    total = total + i
  END IF
NEXT i
OUTPUT total
2 marks
Answer: i MOD 2 == 0
MOD 2 == 0 means "divisible by 2" — i.e. even. Marks: correct use of MOD ✓ · == 0 ✓
Question 3
What is the output of this algorithm?

x = 10
count = 0
WHILE x > 1
  x = x DIV 2
  count = count + 1
END WHILE
OUTPUT count
3 marks
Iterationxcount
Init100
151
222
313
x=1: exit
Output: 3  ·  Marks: correct values of x after each DIV ✓ · correct count ✓ · correct final output ✓
Error Identification

Syntax errors vs logic errors

SYNTAX ERROR
A syntax error breaks the grammatical rules of the language. The program cannot run at all — the translator rejects it before execution. Think: a spelling or grammar error. The program never reaches the first line of output.
SYNTAX ERROR EXAMPLES
// Error 1 — misspelled keyword WILE x < 10 ← WHILE not WILE x = x + 1 END WHILE
// Error 2 — missing END IF IF score > 50 THEN OUTPUT "Pass" ← END IF missing — block unclosed
AFTER FIX
WHILE x < 10 ← WILE → WHILE ✓ x = x + 1 END WHILE IF score > 50 THEN OUTPUT "Pass" END IF ← added ✓
LOGIC ERROR
A logic error means the program runs without crashing, but produces the wrong output. The code is grammatically correct — the translator accepts it — but the algorithm is flawed. These are harder to find: you must trace the code or test it to catch them.
LOGIC ERROR EXAMPLES
// Should output 1 to 5 — what's wrong? FOR i = 0 TO 5 ← starts at 0, not 1 OUTPUT i // outputs 0,1,2,3,4,5 NEXT i
// Average — what's wrong? avg = a + b + c / 3 // only divides c by 3 — missing brackets // Fix: avg = (a + b + c) / 3
Key distinction for the exam: Syntax error = program cannot run. Logic error = program runs but gives wrong output. If asked to name the error type, always state which it is AND explain why — e.g. "This is a logic error because the program runs but outputs 6 incorrect values starting from 0 instead of 5 values starting from 1."
Summary

2.1.2b — Designing Algorithms: Pseudocode

PSEUDOCODE BASICS
Structured English-like notation — not a real language. OCR keywords: INPUT, OUTPUT, IF/THEN/ELSE/END IF, WHILE/END WHILE, FOR/TO/NEXT, AND, OR, NOT, MOD, DIV. Always indent inside blocks. Always close every block.
SELECTION
IF/THEN/ELSE IF/ELSE/END IF. Conditions tested in order — first true branch runs, rest skipped. ELSE is the catch-all. Every IF must close with END IF. Nested IFs need one END IF each.
ITERATION
FOR = count-controlled — use when you know how many times. WHILE = condition-controlled — use when you don't know in advance. With WHILE: initialise counter before, increment inside. Forgetting the increment = infinite loop.
TRACE TABLES
One column per variable + output column. Execute line by line. Write a value ONLY when it changes — leave blank otherwise. Used to verify correctness and find logic errors. Check the loop condition at the top of every iteration.
ERRORS
Syntax error — grammar broken, program cannot run. Examples: misspelled keyword, missing END IF, missing END WHILE. Logic error — program runs but produces wrong output. Examples: off-by-one in FOR loop, wrong operator, missing brackets, missing increment in WHILE. Always state error type and explain why.
2.1.2b Complete

That's 2.1.2b done!

Next up: 2.1.3a — Linear Search

📝
MARKED WORKSHEET
CSZone.co.uk
🎯
QUIZ
CSZone.co.uk
📊
SLIDES
CSZone.co.uk