Programming fundamentals
OCR J277 Paper 2 includes code-reading, code-writing and algorithm design questions. You must be comfortable with OCR's reference language (pseudocode) and be able to write, read and trace code involving variables, selection and iteration.
Variables and constants
- Variable: a named storage location whose value can change during program execution.
- Syntax:
name ← value(OCR pseudocode).
- Syntax:
- Constant: a named storage location whose value is fixed and cannot change.
- Syntax:
CONST name ← value
- Syntax:
- Why use constants? Easier to maintain code — change the value in one place; prevents accidental modification.
Sequence
Instructions executed one after another in the order they appear. The most fundamental concept in programming.
Selection (IF statements)
IF…THEN…ELSE
IF condition THEN
statements
ELSE IF condition THEN
statements
ELSE
statements
END IF
SWITCH/CASE (for multiple specific values)
SWITCH variable:
CASE value1: statements
CASE value2: statements
DEFAULT: statements
END SWITCH
Iteration (loops)
FOR loop (definite iteration — known number of repetitions)
FOR i ← 1 TO 10
OUTPUT i
END FOR
WHILE loop (indefinite — repeats while condition is true)
WHILE condition DO
statements
END WHILE
DO…WHILE (post-condition — always executes at least once)
DO
statements
WHILE condition
Key distinction: WHILE checks the condition before each iteration (may never execute); DO WHILE checks after (executes at least once).
Input and Output
name ← USERINPUT // get input from keyboard
OUTPUT "Hello, " + name // display to screen
Common OCR exam mistakes
- Using FOR when the number of iterations is unknown — you need WHILE.
- Off-by-one errors:
FOR i ← 1 TO 10loops 10 times (1, 2, …, 10);FOR i ← 0 TO 9also loops 10 times but indices are 0–9 (important for arrays). - Infinite loops: forgetting to update the loop variable inside a WHILE loop.
- Not terminating loops correctly — always make sure the condition can become false.
AI-generated · claude-opus-4-7 · v3-ocr-computer-science