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

Programming Fundamentals
Sequence, Selection & Iteration

The three building blocks of every program — in OCR ERL and Python

CSZone OCR GCSE Computer Science J277
Learning Objectives

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

Define sequence and explain why the order of statements in a program matters
Write and interpret selection using IF / ELSEIF / ELSE / ENDIF and SWITCH / CASE in OCR ERL — and know the Python equivalents
Write and interpret iteration using FOR, WHILE and DO...UNTIL — knowing when to choose each one
Identify the differences between pre-test (WHILE) and post-test (DO...UNTIL) loops — including the minimum number of iterations
Avoid classic exam mistakes: wrong loop type, Python range() off-by-one, DO...UNTIL exit condition direction, and infinite loops
⚡ Every program ever written uses a combination of these three constructs. Master them and half the programming exam questions become straightforward.
Sequence

Sequence — statements execute top to bottom, in order

DEFINITION
Sequence means that statements are executed one after another, in the exact order they appear. No statement is skipped or repeated — every line runs exactly once.
ORDER MATTERS — WRONG ORDER EXAMPLE
❌ Wrong order — prints before total is calculated print(total) ← total doesn't exist yet totala + b
CORRECT SEQUENCE
aint(input("Enter a: ")) ← 1st bint(input("Enter b: ")) ← 2nd totala + b ← 3rd print(total) ← 4th ✓
⚡ In a program with only sequence, every line runs from top to bottom without deviation. Sequence is the simplest of the three constructs — but getting the order wrong causes incorrect results or runtime errors.
Selection

Selection — IF / ELSEIF / ELSE

DEFINITION
Selection allows the program to choose between different paths based on a condition. Only the branch whose condition is True will execute.
USE IF WHEN...
Conditions involve ranges or complex expressions, or when different variables are being compared. If testing one variable against several fixed values, SWITCH / CASE is better.
OCR ERL STRUCTURE
IF score >= 70 THEN print("Distinction") ELSEIF score >= 40 THEN print("Pass") ELSE print("Fail") ENDIF
PYTHON EQUIVALENT
if score >= 70: print("Distinction") elif score >= 40: print("Pass") else: print("Fail")
⚠ KEY DIFFERENCE
OCR ERL uses ELSEIF (one word, no colon) and ends with ENDIF. Python uses elif with a colon and indentation — never write ELSEIF in Python.
Selection

Selection — SWITCH / CASE

WHEN TO USE SWITCH / CASE
Use SWITCH / CASE when you are testing one variable against several specific fixed values. It is cleaner than a long chain of ELSEIF statements for this situation.
OCR ERL STRUCTURE
SWITCH grade: CASE "A": print("Excellent") CASE "B": print("Good") CASE "C": print("Satisfactory") DEFAULT: print("Below C") ENDSWITCH
PYTHON EQUIVALENT
Python uses if / elif chains — there is no SWITCH keyword. In Python 3.10+ match/case exists, but the OCR exam only expects if/elif.
if grade == "A": print("Excellent") elif grade == "B": print("Good") elif grade == "C": print("Satisfactory") else: print("Below C")
⚡ Always include a DEFAULT case in SWITCH to handle unexpected values — just like else in Python. The OCR mark scheme will look for it.
Iteration

FOR loop — count-controlled iteration

WHEN TO USE
Use a FOR loop when you know exactly how many iterations are needed before the loop starts. The loop counter is incremented automatically.
OCR ERL SYNTAX
FOR i = 1 TO 5 print(i) NEXT i ← must write NEXT i
PYTHON — range() IS EXCLUSIVE AT THE END
for i in range(1, 6): # 1,2,3,4,5 — 6 NOT included print(i)
SUMMING 5 NUMBERS — ERL EXAMPLE
total0 FOR i = 1 TO 5 numint(input("Enter number: ")) totaltotal + num NEXT i print(total)
⚠ PYTHON RANGE() OFF-BY-ONE
ERL FOR i = 1 TO 5 gives 1,2,3,4,5. Python range(1, 5) gives 1,2,3,4 only — the end is excluded. You need range(1, 6).
Iteration

WHILE loop — condition-controlled, pre-test

WHEN TO USE
Use WHILE when the number of iterations is unknown and the loop body might never run at all (condition could be False from the start). Condition is tested before each iteration — this is a pre-test loop.
OCR ERL SYNTAX
WHILE condition DO <statements> ENDWHILE
COUNTDOWN EXAMPLE — ERL
count5 WHILE count > 0 DO print(count) countcount - 1 ENDWHILE
INPUT VALIDATION EXAMPLE
numint(input("Enter 1–10: ")) WHILE num < 1 OR num > 10 DO print("Invalid!") numint(input("Enter 1–10: ")) ENDWHILE
⚡ If the condition is False at the start, the body never executes. This means WHILE can run zero times — you must always initialise the variable used in the condition before the WHILE statement.
Iteration

DO...UNTIL — post-test, always runs at least once

WHEN TO USE
Use DO...UNTIL when the loop body must run at least once — e.g. getting user input that you need before you can validate. The condition is tested after each iteration — this is a post-test loop. Exits when condition becomes True.
OCR ERL SYNTAX & EXAMPLE
DO passwordinput("Enter password: ") UNTIL password == "secret123"
PYTHON SIMULATION (no DO...UNTIL keyword)
while True: password = input("Enter password: ") if password == "secret123": break
WHILE vs DO...UNTIL — COMPARISON TABLE
Feature WHILE DO...UNTIL
Test positionPre-test (before body)Post-test (after body)
Min iterations01
Exits when...Condition FalseCondition True
Choosing Constructs

Choosing the right construct

Sequence
Use when every step must always happen, in a fixed order. No branching or repetition needed.
Selection
Use when the program must make a decision. Use IF for range/complex conditions. Use SWITCH when comparing one variable to several exact values.
Iteration
FOR — known number of iterations
WHILE — unknown count, might run 0 times
DO...UNTIL — must run at least once
DECISION GUIDE TABLE
Situation Use
Repeat 10 times exactlyFOR
Keep going while validWHILE
Get input then validateDO...UNTIL
Branch on a score rangeIF/ELSEIF
Worked Example

All three constructs — number guessing game

THE SCENARIO
Player gets 3 attempts to guess a secret number. After each guess, tell them Too High, Too Low, or Correct. Use a constant for the secret number.
CONSTRUCTS USED:
FOR — 3 attempts IF/ELSEIF — feedback
OCR ERL PSEUDOCODE
const SECRET7 FOR attempt = 1 TO 3 guessint(input("Guess: ")) IF guess == SECRET THEN print("Correct!") ELSEIF guess > SECRET THEN print("Too high") ELSE print("Too low") ENDIF NEXT attempt
Exam Practice

Sequence, selection & iteration — exam questions

Question 1 — 2 marks
State one difference between a WHILE loop and a DO...UNTIL loop in OCR pseudocode.
Answer — Q1 (1 mark for any one)
WHILE tests condition before each iteration (pre-test); DO...UNTIL tests after each iteration (post-test) [1] — A WHILE loop may never execute if the condition is initially False; DO...UNTIL always executes at least once [1]
Question 2 — 1 mark
A program needs to repeat a set of statements exactly 10 times. Which type of loop should be used? Give a reason.
Answer — Q2
FOR loop — because the number of iterations is known in advance (exactly 10) so a count-controlled loop is appropriate [1]
Question 3 — 4 marks
Write OCR ERL pseudocode for a program that asks the user for 5 numbers using a FOR loop, keeps a running total, then outputs the total and average. Use correct OCR ERL syntax throughout.
Exam Answers

Question 3 — answer and mark scheme

Q3 MODEL ANSWER
total0 ← [1] total initialised FOR i = 1 TO 5 ← [1] FOR loop 1 TO 5 numint(input("Enter number: ")) totaltotal + num NEXT i averagetotal / 5 ← [1] average calculated print(total) ← [1] both outputs print(average)
COMMON MARK LOSSES
• Forgetting to initialise total ← 0 before the loop
• Missing NEXT i at the end of the FOR loop
• Putting the average calculation inside the loop
• Using = instead of for assignment
LOOP SELECTION GUIDE
Loop Use when Min runs
FORKnown count1
WHILEUnknown, may skip0
DO..UNTILMust run once first1
WHILE INPUT VALIDATION — FULL PATTERN
numint(input("Enter 1–10: ")) ← prime WHILE num < 1 OR num > 10 DO print("Invalid, try again") numint(input("Enter 1–10: ")) ENDWHILE
This pattern is very common in exam questions — always prime the variable before the WHILE condition is tested for the first time.
Common Mistakes

Common mistakes — avoid these in the exam

MISTAKE 1 — Using FOR when the count is unknown
Using a FOR loop when repeating "until the user enters a valid value" — the count is unknown, so FOR is wrong
✓ Use WHILE (or DO...UNTIL if input must be collected at least once before validating)
MISTAKE 2 — Python range() off-by-one error
Writing range(1, 5) expecting 1,2,3,4,5 — but range() excludes the upper bound, giving 1,2,3,4
✓ Use range(1, 6) to get 1,2,3,4,5  ·  ERL FOR i = 1 TO 5 includes 5
MISTAKE 3 — Forgetting to update the loop variable (infinite loop)
In a WHILE loop, forgetting to update the variable that controls the condition — loop never terminates because the condition never becomes False
✓ Always include a statement inside the loop body that moves toward the exit condition
MISTAKE 4 — DO...UNTIL exit condition wrong direction
Writing UNTIL num < 1 OR num > 10 — this exits on invalid input, the opposite of what you want
✓ DO...UNTIL exits when condition is True — write the condition as what you want: UNTIL num >= 1 AND num <= 10
Summary

Key points — 2.2.1b

Sequence — statements run top to bottom, in order; getting the order wrong causes incorrect results or crashes
SelectionIF/ELSEIF/ELSE/ENDIF for ranges; SWITCH/CASE/DEFAULT/ENDSWITCH for exact values; Python uses elif not ELSEIF
FOR — known count, FOR i = 1 TO n ... NEXT i; Python range() excludes the end — use range(1, n+1)
WHILE — pre-test; condition checked before body; may run 0 times; always prime the variable first
DO...UNTIL — post-test; body runs first then condition checked; always runs at least once; exits when condition is True
⚡ Next topic: 2.2.1c — Arrays — storing and accessing multiple values under one variable name.
2.2.1b Complete

Sequence, Selection
& Iteration

Get the full resource pack at CSZone.co.uk

📄
Marked Worksheet
CSZone.co.uk
Quiz
CSZone.co.uk
📊
Slides
CSZone.co.uk
Next Up
2.2.1c — Arrays