📘 Cambridge 2210 — Pseudocode Notes

O Level Computer Science · Complete pseudocode syntax for Paper 2

🔵 O Level 2210 — all syntax covered here Based on Cambridge 2210 pseudocode guide & 9618 teacher guide
✏️
Writing Conventions
Rules that apply to all Cambridge pseudocode
O Level 2210
Keywords

All reserved words are written in UPPERCASE.

IF, WHILE, FOR, PROCEDURE, FUNCTION, OUTPUT
Identifiers

Variable and procedure names use camelCase — new words start with a capital letter.

numberOfStudents
totalScore
StudentName
Assignment

Use the arrow (not = which is only for comparison).

score ← 0
name "Alice"
total ← total + 1
Comments

Comments start with // and run to the end of the line.

// This is a comment
score ← 0   // initialise score
Indentation

Indent the body of each block by 2 spaces. Each nesting level adds another 2-space indent.

IF x > 0
  THEN
    OUTPUT "positive"   // 4 spaces in
ENDIF
Input / Output

INPUT reads a value into a variable.
OUTPUT prints to screen — multiple values separated by commas.

INPUT name
OUTPUT "Hello, ", name
OUTPUT score, " out of ", total
📦
Data Types & Declarations
Variable and constant declarations
O Level 2210
Core Types

Five primitive data types are used in 2210:

INTEGER   // whole numbers: -5, 0, 42
REAL      // decimals: 3.14, -0.5
CHAR      // single character: 'A', '9', '?'
STRING    // text: "hello", "abc123"
BOOLEAN   // TRUE or FALSE only
DECLARE

Declare a variable before using it with DECLARE.

DECLARE score : INTEGER
DECLARE average : REAL
DECLARE initial : CHAR
DECLARE name : STRING
DECLARE passed : BOOLEAN
CONSTANT

Constants are declared with a value that never changes.

CONSTANT Pi ← 3.14159
CONSTANT MaxSize ← 100
CONSTANT Grade 'A'
Exam tip: CONSTANT values cannot be changed with ← later in the program.
Literals

Strings use double quotes. Characters use single quotes. Booleans are written without quotes.

name "Alice"      // STRING
initial 'A'     // CHAR
passed TRUE    // BOOLEAN
score ← 42        // INTEGER
price ← 9.99      // REAL
Operators
Arithmetic, comparison, and logical
O Level 2210
Arithmetic
x + y      // addition
x - y      // subtraction
x * y      // multiplication
x / y      // real division  (17/5 = 3.4)
x DIV y    // integer division (17 DIV 5 = 3)
x MOD y    // remainder       (17 MOD 5 = 2)
DIV vs /: DIV always returns a whole number, discarding the remainder. Use when you need integer division (e.g., splitting into equal groups).
Comparison

All return TRUE or FALSE. Use = for comparison (not ==).

x = y      // equal to
x <> y     // not equal to
x < y      // less than
x > y      // greater than
x <= y     // less than or equal
x >= y     // greater than or equal
Logical

Combine Boolean expressions.

AND   // both must be TRUE
OR    // at least one must be TRUE
NOT   // inverts TRUE↔FALSE
IF age >= 18 AND hasID = TRUE
  THEN
    OUTPUT "Entry allowed"
ENDIF
🔤
String Functions
All string operations tested in 2210
O Level 2210
LENGTH()

Returns the number of characters in a string.

OUTPUT LENGTH("hello")   // outputs 5
DECLARE len : INTEGER
len LENGTH(name)
LCASE() / UCASE()

Convert a string to all lowercase or all uppercase.

LCASE("Hello")   // returns "hello"
UCASE("hello")   // returns "HELLO"
Common use: normalise user input before comparing — so "yes", "YES", and "Yes" all match.
IF LCASE(answer) = "yes"
  THEN
    OUTPUT "Confirmed"
ENDIF
SUBSTRING()

Extract part of a string. SUBSTRING(string, start, length) — positions start at 1.

SUBSTRING("Cambridge", 1, 3)  // "Cam"
SUBSTRING("Cambridge", 4, 6)  // "bridge"
SUBSTRING("Hello", 2, 3)     // "ell"
Parameters: (1) the string, (2) start position — counting from 1, (3) how many characters to extract.
Combining functions

String functions can be nested or used in expressions.

// Get first character, lowercased
first LCASE(SUBSTRING(name, 1, 1))

// Check a string is exactly 8 characters
IF LENGTH(password) = 8
  THEN
    OUTPUT "Valid length"
ENDIF
🔀
Selection — IF Statement
O Level 2210
Simple IF
IF score >= 50
  THEN
    OUTPUT "Pass"
ENDIF
IF … ELSE
IF score >= 50
  THEN
    OUTPUT "Pass"
  ELSE
    OUTPUT "Fail"
ENDIF
Nested IF

Each inner IF adds another 2-space indent. Every IF needs its own ENDIF.

IF score >= 70
  THEN
    OUTPUT "Distinction"
  ELSE
    IF score >= 50
      THEN
        OUTPUT "Pass"
      ELSE
        OUTPUT "Fail"
    ENDIF
ENDIF
Count your ENDIFs! One ENDIF per IF — if you open 3 IFs, you need 3 ENDIFs.
🗂️
Selection — CASE Statement
O Level 2210
CASE OF

Tests one variable against discrete values. First matching case executes; the rest are skipped.

CASE OF grade
  'A' : OUTPUT "Excellent"
  'B' : OUTPUT "Good"
  'C' : OUTPUT "Average"
  OTHERWISE OUTPUT "Below average"
ENDCASE
OTHERWISE (no colon) is the fallback if nothing matches — equivalent to the final ELSE.
Limitation: CASE only works for exact values, not ranges (> 50). For ranges, use nested IF.
CASE with integers
CASE OF dayNumber
  1 : OUTPUT "Monday"
  2 : OUTPUT "Tuesday"
  3 : OUTPUT "Wednesday"
  OTHERWISE OUTPUT "Invalid day"
ENDCASE
IF vs CASE
CASE One variable tested against exact discrete values
IF Ranges, inequalities, or multiple variables in conditions
🔁
Iteration — All Three Loop Types
O Level 2210
FOR … NEXT

Use when the number of repetitions is known in advance. Counter variable is updated automatically.

FOR i ← 1 TO 10
  OUTPUT i
NEXT i

Use STEP to change the increment (can be negative):

FOR i ← 10 TO 1 STEP -1
  OUTPUT i
NEXT i                       // counts 10, 9, 8 … 1

FOR i ← 0 TO 20 STEP 5
  OUTPUT i
NEXT i                       // outputs 0, 5, 10, 15, 20
WHILE … ENDWHILE

Checks condition before each iteration. May execute zero times if condition is false at the start.

count ← 1
WHILE count <= 5 DO
  OUTPUT count
  count ← count + 1
ENDWHILE
Use WHILE when: the loop might not run at all (e.g., validating input that might already be valid).
REPEAT … UNTIL

Checks condition after each iteration. Always executes at least once.

REPEAT
  INPUT age
UNTIL age >= 0
Use REPEAT when: the body must run at least once — e.g. asking for input until a valid value is entered.
Choosing a loop
FOR Number of repetitions known
WHILE Condition checked first; might not run at all
REPEAT Must run at least once; condition checked after
🗂️
Arrays
1D and 2D arrays
O Level 2210
1D Declaration

Format: DECLARE name : ARRAY[lower:upper] OF type

DECLARE scores : ARRAY[1:10] OF INTEGER
DECLARE names  : ARRAY[1:30] OF STRING
DECLARE flags  : ARRAY[0:4]  OF BOOLEAN
Arrays can be zero-indexed ([0:9]) or one-indexed ([1:10]) — the declaration sets it.
Access elements
scores[1] ← 85              // assign
OUTPUT scores[3]             // read
scores[i] ← scores[i] + 1    // update in loop
Loop through 1D
FOR i ← 1 TO 10
  OUTPUT scores[i]
NEXT i
2D Declaration

Format: DECLARE name : ARRAY[r1:r2, c1:c2] OF type — rows first, then columns.

DECLARE grid : ARRAY[1:3, 1:4] OF INTEGER
// 3 rows, 4 columns — 12 cells total
Access & loop 2D
grid[2, 3] ← 99            // row 2, column 3

// loop through every cell
FOR row ← 1 TO 3
  FOR col ← 1 TO 4
    OUTPUT grid[row, col]
  NEXT col
NEXT row
⚙️
Procedures
Named blocks that DO something — no return value
O Level 2210
No parameters
PROCEDURE ShowMenu()
  OUTPUT "1. Start"
  OUTPUT "2. Quit"
ENDPROCEDURE

CALL ShowMenu()    // call it
With parameters
PROCEDURE Greet(userName : STRING)
  OUTPUT "Hello, ", userName
ENDPROCEDURE

CALL Greet("Alice")
Multiple parameters
PROCEDURE PrintRange(start : INTEGER, finish : INTEGER)
  FOR i ← start TO finish
    OUTPUT i
  NEXT i
ENDPROCEDURE

CALL PrintRange(1, 5)    // outputs 1 2 3 4 5
In 2210, you do not need to specify BYREF or BYVALUE — parameters are simply listed with their type.
🔧
Functions
Named blocks that RETURN a value
O Level 2210
Define & call

Functions use RETURNS in the header and RETURN to send back a value.

FUNCTION Square(n : INTEGER) RETURNS INTEGER
  RETURN n * n
ENDFUNCTION

result Square(5)        // result = 25
OUTPUT Square(4)           // outputs 16
Key difference from procedure: functions RETURN a value and can be used inside expressions. Procedures cannot — call them with CALL.
Multiple params
FUNCTION Add(a : INTEGER, b : INTEGER) RETURNS INTEGER
  RETURN a + b
ENDFUNCTION

OUTPUT Add(3, 7)    // outputs 10
Procedure vs Function
Feature Procedure Function
Returns a value? No Yes — with RETURN
How to call CALL Name() x ← Name()
Header keyword PROCEDURE FUNCTION … RETURNS type
📋
2210 Quick Reference
Every construct covered in the O Level syllabus
O Level 2210
Syntax / Construct What it does Example
DECLARE x : TYPEDeclare a variableDECLARE score : INTEGER
CONSTANT k ← valueDeclare an unchanging constantCONSTANT Pi ← 3.14
x ← valueAssign a value to a variablescore ← 0
INPUT xRead a value from the user into xINPUT name
OUTPUT value, …Print value(s) to screenOUTPUT "Hi ", name
+ − * /Addition, subtraction, multiply, divide (real)total ← a + b
DIVInteger division — drops remainder17 DIV 5 → 3
MODRemainder after integer division17 MOD 5 → 2
= <> < > <= >=Comparison operators — return BOOLEANscore >= 50
AND OR NOTLogical operators — combine conditionsa > 0 AND b > 0
LENGTH(s)Number of characters in string sLENGTH("hello") → 5
LCASE(s)Convert string to lowercaseLCASE("Hi") → "hi"
UCASE(s)Convert string to uppercaseUCASE("Hi") → "HI"
SUBSTRING(s, start, len)Extract len characters from s starting at start (1-based)SUBSTRING("Hello", 2, 3) → "ell"
IF … THEN … ELSE … ENDIFConditional — execute different blocks based on conditionIF x > 0 THEN … ENDIF
CASE OF … ENDCASEMulti-way selection on a single variable's valueCASE OF grade …
OTHERWISEDefault clause in CASE — runs if no case matchedOTHERWISE OUTPUT "?"
FOR i ← a TO bCount-controlled loop from a to b inclusiveFOR i ← 1 TO 10
STEP nOptional: sets loop increment (default 1, can be negative)FOR i ← 10 TO 1 STEP -1
NEXT iCloses a FOR loopNEXT i
WHILE cond DO … ENDWHILEPre-condition loop — may run zero timesWHILE x < 10 DO …
REPEAT … UNTIL condPost-condition loop — runs at least onceREPEAT … UNTIL ok
ARRAY[l:u] OF TYPEDeclare a 1D array with indices l to uARRAY[1:10] OF INTEGER
ARRAY[r1:r2, c1:c2] OF TYPEDeclare a 2D array (rows, columns)ARRAY[1:3, 1:4] OF CHAR
arr[i]Access element i of a 1D arrayscores[3] ← 90
arr[r, c]Access element at row r, column c of a 2D arraygrid[1, 2] ← 'X'
PROCEDURE Name(params)Define a procedure with optional parametersPROCEDURE Print(x : INTEGER)
ENDPROCEDUREClose a procedure definition
CALL Name(args)Execute a procedureCALL Print(42)
FUNCTION Name(params) RETURNS typeDefine a function that returns a valueFUNCTION Sq(n : INTEGER) RETURNS INTEGER
RETURN valueSend a value back from a functionRETURN n * n
ENDFUNCTIONClose a function definition