📘 Cambridge Pseudocode — Student Notes

O Level 2210  |  AS & A Level 9618 — syntax reference & comparison

🔵 In both 2210 & 9618 🟢 9618 A Level only Based on official Cambridge pseudocode guides
O Level 2210 covers this
A Level 9618 only — not in 2210
✏️
Writing Conventions
Rules that apply to all Cambridge pseudocode
Both 2210 & 9618
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 (2210) or consistent spaces (9618). Each nesting level adds another 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
Both 2210 & 9618
Core Types

Five primitive data types are used across both syllabuses:

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
Both 2210 & 9618
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 — In Both Guides
Available in 2210 and 9618
Both 2210 & 9618
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"
In 9618, these may also appear as TO_LOWER() and TO_UPPER() — same behaviour, different name.
SUBSTRING()

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

SUBSTRING("Cambridge", 1, 3)  // "Cam"
SUBSTRING("Cambridge", 4, 6)  // "bridge"
In 9618, this is also available as MID(string, start, length) — identical behaviour.
🟢
Extra String & Conversion Functions
These are NOT in 2210 — A Level only
9618 A Level only
LEFT() / RIGHT()

Extract characters from the start or end of a string.

LEFT("Cambridge", 3)   // "Cam"
RIGHT("Cambridge", 6)  // "bridge"
NUM_TO_STRING() / STRING_TO_NUM()

Convert between numbers and their string representations.

numStr NUM_TO_STRING(42)     // "42"
num STRING_TO_NUM("3.14")   // 3.14
IS_NUM()

Returns TRUE if a string can be converted to a number.

IS_NUM("42")    // TRUE
IS_NUM("abc")   // FALSE
ASC() / CHR()

Convert between a character and its ASCII code.

ASC('A')   // 65
ASC('a')   // 97
CHR(65)    // 'A'
Useful for: checking if a character is a letter (ASC('A')=65 … ASC('Z')=90, ASC('a')=97 … ASC('z')=122), shifting cipher characters, etc.
INT()

Rounds a REAL down to the nearest INTEGER (floor).

INT(3.9)    // 3
INT(-2.1)   // -3  (rounds towards negative infinity)
🔀
Selection — IF Statement
Both 2210 & 9618
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
Both 2210 & 9618
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
🔁
Iteration — All Three Loop Types
Both 2210 & 9618
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 — same in both syllabuses
Both 2210 & 9618
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
Both 2210 & 9618
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")
BYREF (9618 only)

In 9618, you can specify how parameters are passed. In 2210, this distinction is not tested.

2210 — no BYREF/BYVAL keyword
PROCEDURE Double(n : INTEGER)
  n ← n * 2
  OUTPUT n
ENDPROCEDURE
9618 — BYREF / BYVALUE
PROCEDURE Double(BYREF n : INTEGER)
  n ← n * 2
ENDPROCEDURE
// changes n in the calling code
BYVALUE — a copy is passed; the original is unchanged.
BYREF — the actual variable is passed; changes inside the procedure affect the original.
🔧
Functions
Named blocks that RETURN a value
Both 2210 & 9618
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
📁
File Handling
Reading and writing text files
9618 A Level only
Open modes

A file must be opened in the correct mode before any operation.

OPENFILE "data.txt" FOR READ     // read existing data
OPENFILE "data.txt" FOR WRITE    // overwrite / create new
OPENFILE "data.txt" FOR APPEND   // add to end of file
Read from file
OPENFILE "scores.txt" FOR READ
WHILE NOT EOF("scores.txt") DO
  READFILE "scores.txt", line
  OUTPUT line
ENDWHILE
CLOSEFILE "scores.txt"
EOF(filename) returns TRUE when there are no more lines to read. Always check it before READFILE to avoid an error.
Write to file
OPENFILE "output.txt" FOR WRITE
WRITEFILE "output.txt", "Line one"
WRITEFILE "output.txt", score
CLOSEFILE "output.txt"
FOR WRITE overwrites the entire file. Use FOR APPEND to add without losing existing data.
Append to file
OPENFILE "log.txt" FOR APPEND
WRITEFILE "log.txt", "New entry"
CLOSEFILE "log.txt"
Rules

Always: OPEN → do operations → CLOSE. You cannot READFILE and WRITEFILE from the same OPENFILE call — use separate open/close pairs.

🏗️
Object-Oriented Programming
Classes, objects, inheritance
9618 A Level only
Class definition

A class groups attributes (data) and methods (behaviour). Use PUBLIC and PRIVATE to control access.

CLASS Animal
  PRIVATE name : STRING
  PRIVATE sound : STRING

  PUBLIC PROCEDURE NEW(n : STRING, s : STRING)
    name ← n
    sound ← s
  ENDPROCEDURE

  PUBLIC PROCEDURE Speak()
    OUTPUT name, " says ", sound
  ENDPROCEDURE

  PUBLIC FUNCTION GetName() RETURNS STRING
    RETURN name
  ENDFUNCTION
ENDCLASS
Create an object

Use NEW followed by the class name and constructor arguments.

myPet NEW Animal("Cat", "Meow")
CALL myPet.Speak()            // Cat says Meow
OUTPUT myPet.GetName()        // Cat
Inheritance

A child class inherits from a parent using INHERITS. Call the parent's constructor with SUPER.

CLASS Dog INHERITS Animal

  PUBLIC PROCEDURE NEW(n : STRING)
    SUPER(n, "Woof")    // call Animal's NEW
  ENDPROCEDURE

  PUBLIC PROCEDURE Fetch()
    OUTPUT "Fetching!"
  ENDPROCEDURE

ENDCLASS

rex NEW Dog("Rex")
CALL rex.Speak()     // Rex says Woof (inherited)
CALL rex.Fetch()     // Fetching! (new method)
Key OOP concepts tested in 9618: encapsulation (PUBLIC/PRIVATE), inheritance (INHERITS), overriding methods, calling SUPER.
Access rules
PRIVATE Accessible only inside the class itself
PUBLIC Accessible from anywhere (calling code, subclasses)
Best practice: Attributes (data) are usually PRIVATE. Methods (procedures/functions) that need to be called from outside are PUBLIC. This is encapsulation.
📋
Full Quick Reference Table
Every construct at a glance — tick shows which syllabus covers it
Syntax / Construct What it does 2210 9618
DECLARE x : TYPEDeclare a variable
CONSTANT k ← valueDeclare a constant
x ← valueAssignment
INPUT / OUTPUTRead input / print output
+ − * /Basic arithmetic
DIV / MODInteger division / remainder
= <> < > <= >=Comparison operators
AND OR NOTLogical operators
LENGTH(s)Length of string
LCASE(s) / UCASE(s)Convert case (2210 names)
TO_LOWER(s) / TO_UPPER(s)Convert case (9618 alias names)
SUBSTRING(s, start, len)Extract part of string (2210)
MID(s, start, len)Extract part of string (9618 alias)
LEFT(s, n) / RIGHT(s, n)First / last n characters
NUM_TO_STRING(n)Number → string
STRING_TO_NUM(s)String → number
IS_NUM(s)Can string be a number?
ASC(c) / CHR(n)Char ↔ ASCII code
INT(x)Round down to integer
IF … THEN … ELSE … ENDIFConditional selection
CASE OF … ENDCASEMulti-way selection on one variable
FOR … TO … STEP … NEXTCount-controlled loop
WHILE … DO … ENDWHILEPre-condition loop
REPEAT … UNTILPost-condition loop
ARRAY[low:high] OF TYPE1D array declaration
ARRAY[r1:r2, c1:c2] OF TYPE2D array declaration
PROCEDURE / ENDPROCEDUREDefine a procedure
CALL ProcName()Call a procedure
FUNCTION … RETURNS … ENDFUNCTIONDefine a function
RETURN valueReturn value from function
BYVALUE / BYREFParameter passing mode
OPENFILE … FOR READ/WRITE/APPENDOpen a text file
READFILE / WRITEFILE / CLOSEFILEFile I/O operations
EOF(filename)Check for end of file
CLASS … ENDCLASSDefine a class
PUBLIC / PRIVATEAccess modifiers
NEW ClassName(…)Create an object (instantiate)
INHERITS / SUPERInheritance from parent class