O Level 2210 | AS & A Level 9618 — syntax reference & comparison
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 (2210) or consistent spaces (9618). Each nesting level adds another 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 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 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"
Extract part of a string. SUBSTRING(string, start, length) — positions start at 1.
SUBSTRING("Cambridge", 1, 3) // "Cam" SUBSTRING("Cambridge", 4, 6) // "bridge"
Extract characters from the start or end of a string.
LEFT("Cambridge", 3) // "Cam" RIGHT("Cambridge", 6) // "bridge"
Convert between numbers and their string representations.
numStr ← NUM_TO_STRING(42) // "42" num ← STRING_TO_NUM("3.14") // 3.14
Returns TRUE if a string can be converted to a number.
IS_NUM("42") // TRUE IS_NUM("abc") // FALSE
Convert between a character and its ASCII code.
ASC('A') // 65 ASC('a') // 97 CHR(65) // 'A'
Rounds a REAL down to the nearest INTEGER (floor).
INT(3.9) // 3 INT(-2.1) // -3 (rounds towards negative infinity)
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
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")
In 9618, you can specify how parameters are passed. In 2210, this distinction is not tested.
PROCEDURE Double(n : INTEGER) n ← n * 2 OUTPUT n ENDPROCEDURE
PROCEDURE Double(BYREF n : INTEGER) n ← n * 2 ENDPROCEDURE // changes n in the calling code
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
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
OPENFILE "scores.txt" FOR READ WHILE NOT EOF("scores.txt") DO READFILE "scores.txt", line OUTPUT line ENDWHILE CLOSEFILE "scores.txt"
OPENFILE "output.txt" FOR WRITE WRITEFILE "output.txt", "Line one" WRITEFILE "output.txt", score CLOSEFILE "output.txt"
OPENFILE "log.txt" FOR APPEND WRITEFILE "log.txt", "New entry" CLOSEFILE "log.txt"
Always: OPEN → do operations → CLOSE. You cannot READFILE and WRITEFILE from the same OPENFILE call — use separate open/close pairs.
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
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
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)
| PRIVATE | Accessible only inside the class itself |
| PUBLIC | Accessible from anywhere (calling code, subclasses) |
| Syntax / Construct | What it does | 2210 | 9618 |
|---|---|---|---|
| DECLARE x : TYPE | Declare a variable | ✓ | ✓ |
| CONSTANT k ← value | Declare a constant | ✓ | ✓ |
| x ← value | Assignment | ✓ | ✓ |
| INPUT / OUTPUT | Read input / print output | ✓ | ✓ |
| + − * / | Basic arithmetic | ✓ | ✓ |
| DIV / MOD | Integer division / remainder | ✓ | ✓ |
| = <> < > <= >= | Comparison operators | ✓ | ✓ |
| AND OR NOT | Logical 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 … ENDIF | Conditional selection | ✓ | ✓ |
| CASE OF … ENDCASE | Multi-way selection on one variable | ✓ | ✓ |
| FOR … TO … STEP … NEXT | Count-controlled loop | ✓ | ✓ |
| WHILE … DO … ENDWHILE | Pre-condition loop | ✓ | ✓ |
| REPEAT … UNTIL | Post-condition loop | ✓ | ✓ |
| ARRAY[low:high] OF TYPE | 1D array declaration | ✓ | ✓ |
| ARRAY[r1:r2, c1:c2] OF TYPE | 2D array declaration | ✓ | ✓ |
| PROCEDURE / ENDPROCEDURE | Define a procedure | ✓ | ✓ |
| CALL ProcName() | Call a procedure | ✓ | ✓ |
| FUNCTION … RETURNS … ENDFUNCTION | Define a function | ✓ | ✓ |
| RETURN value | Return value from function | ✓ | ✓ |
| BYVALUE / BYREF | Parameter passing mode | ✗ | ✓ |
| OPENFILE … FOR READ/WRITE/APPEND | Open a text file | ✗ | ✓ |
| READFILE / WRITEFILE / CLOSEFILE | File I/O operations | ✗ | ✓ |
| EOF(filename) | Check for end of file | ✗ | ✓ |
| CLASS … ENDCLASS | Define a class | ✗ | ✓ |
| PUBLIC / PRIVATE | Access modifiers | ✗ | ✓ |
| NEW ClassName(…) | Create an object (instantiate) | ✗ | ✓ |
| INHERITS / SUPER | Inheritance from parent class | ✗ | ✓ |