O Level Computer Science · Complete pseudocode syntax for Paper 2
All reserved words are written in UPPERCASE.
IF, WHILE, FOR, PROCEDURE, FUNCTION, OUTPUT
Variable and procedure names use camelCase — new words start with a capital letter.
numberOfStudents totalScore StudentName
Use the ← arrow (not = which is only for comparison).
score ← 0 name ← "Alice" total ← total + 1
Comments start with // and run to the end of the line.
// This is a comment score ← 0 // initialise score
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 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
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 a variable before using it with DECLARE.
DECLARE score : INTEGER DECLARE average : REAL DECLARE initial : CHAR DECLARE name : STRING DECLARE passed : BOOLEAN
Constants are declared with a value that never changes.
CONSTANT Pi ← 3.14159 CONSTANT MaxSize ← 100 CONSTANT Grade ← 'A'
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
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)
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
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
Returns the number of characters in a string.
OUTPUT LENGTH("hello") // outputs 5 DECLARE len : INTEGER len ← LENGTH(name)
Convert a string to all lowercase or all uppercase.
LCASE("Hello") // returns "hello" UCASE("hello") // returns "HELLO"
IF LCASE(answer) = "yes" THEN OUTPUT "Confirmed" ENDIF
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"
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
IF score >= 50 THEN OUTPUT "Pass" ENDIF
IF score >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF
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
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
CASE OF dayNumber 1 : OUTPUT "Monday" 2 : OUTPUT "Tuesday" 3 : OUTPUT "Wednesday" OTHERWISE OUTPUT "Invalid day" ENDCASE
| CASE | One variable tested against exact discrete values |
| IF | Ranges, inequalities, or multiple variables in conditions |
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
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
Checks condition after each iteration. Always executes at least once.
REPEAT INPUT age UNTIL age >= 0
| FOR | Number of repetitions known |
| WHILE | Condition checked first; might not run at all |
| REPEAT | Must run at least once; condition checked after |
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
[0:9]) or one-indexed ([1:10]) — the declaration sets it.scores[1] ← 85 // assign OUTPUT scores[3] // read scores[i] ← scores[i] + 1 // update in loop
FOR i ← 1 TO 10 OUTPUT scores[i] NEXT i
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
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
PROCEDURE ShowMenu() OUTPUT "1. Start" OUTPUT "2. Quit" ENDPROCEDURE CALL ShowMenu() // call it
PROCEDURE Greet(userName : STRING) OUTPUT "Hello, ", userName ENDPROCEDURE CALL Greet("Alice")
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
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
FUNCTION Add(a : INTEGER, b : INTEGER) RETURNS INTEGER RETURN a + b ENDFUNCTION OUTPUT Add(3, 7) // outputs 10
| Feature | Procedure | Function |
|---|---|---|
| Returns a value? | No | Yes — with RETURN |
| How to call | CALL Name() | x ← Name() |
| Header keyword | PROCEDURE | FUNCTION … RETURNS type |
| Syntax / Construct | What it does | Example |
|---|---|---|
| DECLARE x : TYPE | Declare a variable | DECLARE score : INTEGER |
| CONSTANT k ← value | Declare an unchanging constant | CONSTANT Pi ← 3.14 |
| x ← value | Assign a value to a variable | score ← 0 |
| INPUT x | Read a value from the user into x | INPUT name |
| OUTPUT value, … | Print value(s) to screen | OUTPUT "Hi ", name |
| + − * / | Addition, subtraction, multiply, divide (real) | total ← a + b |
| DIV | Integer division — drops remainder | 17 DIV 5 → 3 |
| MOD | Remainder after integer division | 17 MOD 5 → 2 |
| = <> < > <= >= | Comparison operators — return BOOLEAN | score >= 50 |
| AND OR NOT | Logical operators — combine conditions | a > 0 AND b > 0 |
| LENGTH(s) | Number of characters in string s | LENGTH("hello") → 5 |
| LCASE(s) | Convert string to lowercase | LCASE("Hi") → "hi" |
| UCASE(s) | Convert string to uppercase | UCASE("Hi") → "HI" |
| SUBSTRING(s, start, len) | Extract len characters from s starting at start (1-based) | SUBSTRING("Hello", 2, 3) → "ell" |
| IF … THEN … ELSE … ENDIF | Conditional — execute different blocks based on condition | IF x > 0 THEN … ENDIF |
| CASE OF … ENDCASE | Multi-way selection on a single variable's value | CASE OF grade … |
| OTHERWISE | Default clause in CASE — runs if no case matched | OTHERWISE OUTPUT "?" |
| FOR i ← a TO b | Count-controlled loop from a to b inclusive | FOR i ← 1 TO 10 |
| STEP n | Optional: sets loop increment (default 1, can be negative) | FOR i ← 10 TO 1 STEP -1 |
| NEXT i | Closes a FOR loop | NEXT i |
| WHILE cond DO … ENDWHILE | Pre-condition loop — may run zero times | WHILE x < 10 DO … |
| REPEAT … UNTIL cond | Post-condition loop — runs at least once | REPEAT … UNTIL ok |
| ARRAY[l:u] OF TYPE | Declare a 1D array with indices l to u | ARRAY[1:10] OF INTEGER |
| ARRAY[r1:r2, c1:c2] OF TYPE | Declare a 2D array (rows, columns) | ARRAY[1:3, 1:4] OF CHAR |
| arr[i] | Access element i of a 1D array | scores[3] ← 90 |
| arr[r, c] | Access element at row r, column c of a 2D array | grid[1, 2] ← 'X' |
| PROCEDURE Name(params) | Define a procedure with optional parameters | PROCEDURE Print(x : INTEGER) |
| ENDPROCEDURE | Close a procedure definition | |
| CALL Name(args) | Execute a procedure | CALL Print(42) |
| FUNCTION Name(params) RETURNS type | Define a function that returns a value | FUNCTION Sq(n : INTEGER) RETURNS INTEGER |
| RETURN value | Send a value back from a function | RETURN n * n |
| ENDFUNCTION | Close a function definition |