CAIE Pseudocode
How-To Guide
A complete reference for writing Cambridge A-Level pseudocode — from first principles to object-oriented programming. Use the sidebar to jump to any topic.
Introduction
Pseudocode is a plain-language description of an algorithm. CAIE (Cambridge Assessment International Education) defines a specific pseudocode style used in A-Level Computer Science exams (syllabus codes 9618 and 9608). It is not a real programming language — it cannot be directly compiled — but it follows strict conventions so that any examiner can read and mark it consistently.
Key things to know before you start:
// on a line is a comment and is ignored logically. Use them to explain your code.// A simple first program DECLARE Name : STRING OUTPUT "What is your name? " INPUT Name OUTPUT "Hello, " & Name & "!"
Data Types
Every value in CAIE pseudocode has a data type. You must declare the type of every variable before using it.
| Type | Description | Example Values |
|---|---|---|
| INTEGER | Whole numbers (positive, negative, or zero) | 0, 42, -7 |
| REAL | Numbers with a decimal point | 3.14, -0.5, 2.0 |
| CHAR | A single character (enclosed in single quotes) | 'A', '9', ' ' |
| STRING | A sequence of zero or more characters (enclosed in double quotes) | "Hello", "42", "" |
| BOOLEAN | A logical value — either TRUE or FALSE | TRUE, FALSE |
| DATE | Stores a calendar date | 17/04/2026 |
CHAR (single quotes, one character) and STRING (double quotes, any length). Mixing them up is a common exam mistake.
Enumerated Types
You can define your own type with a fixed set of named values:
TYPE Season = (Spring, Summer, Autumn, Winter) DECLARE CurrentSeason : Season CurrentSeason <- Summer
Pointer Types
Pointer types store the memory address of another variable:
TYPE IntPointer = ^INTEGER DECLARE Ptr : IntPointer
Variables & Constants
All variables must be declared before they are used. Constants are declared with a fixed value that cannot change during program execution.
Declaring Variables
DECLARE Age : INTEGER DECLARE Price : REAL DECLARE Initial : CHAR DECLARE FullName : STRING DECLARE IsValid : BOOLEAN
Declaring Constants
CONSTANT PI <- 3.14159 CONSTANT MAX_SIZE <- 100 CONSTANT GREETING <- "Hello"
<- at declaration time and must never be reassigned later in the program.
Input / Output
Use INPUT to read a value from the user and OUTPUT (or PRINT) to display a value.
DECLARE Score : INTEGER DECLARE PlayerName : STRING OUTPUT "Enter your name: " INPUT PlayerName OUTPUT "Enter your score: " INPUT Score OUTPUT PlayerName & " scored: " & Score
The & operator concatenates (joins) strings and values together in an OUTPUT statement.
OUTPUT and PRINT are accepted in CAIE exams. OUTPUT is the preferred, official keyword.
Operators
Arithmetic Operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
| + | Addition | 7 + 3 | 10 |
| - | Subtraction | 7 - 3 | 4 |
| * | Multiplication | 7 * 3 | 21 |
| / | Division (real result) | 7 / 2 | 3.5 |
| DIV | Integer division (floor) | 7 DIV 2 | 3 |
| MOD | Remainder after integer division | 7 MOD 2 | 1 |
| ^ | Exponentiation (power) | 2 ^ 8 | 256 |
Comparison Operators
These return TRUE or FALSE and are used in conditions:
| Operator | Meaning |
|---|---|
| = | Equal to |
| <> | Not equal to |
| < | Less than |
| > | Greater than |
| <= | Less than or equal to |
| >= | Greater than or equal to |
Logical (Boolean) Operators
| Operator | Meaning | Example |
|---|---|---|
| AND | Both conditions must be TRUE | Age >= 18 AND HasID = TRUE |
| OR | At least one condition must be TRUE | Score > 90 OR Award = TRUE |
| NOT | Inverts the Boolean value | NOT IsFinished |
String Concatenation
DECLARE FullName : STRING FullName <- "Alice" & " " & "Smith" // "Alice Smith"
Assignment
The assignment operator <- stores a value into a variable. The variable must already have been declared with a matching type.
DECLARE Counter : INTEGER DECLARE Total : REAL DECLARE Message : STRING Counter <- 0 Total <- 0.0 Message <- "Starting..." // Increment a counter Counter <- Counter + 1 // Compute a value Total <- Counter * 3.5
<- for assignment, not =. The equals sign (=) is only used for comparison.
Selection — IF and CASE
Selection allows the program to choose different paths based on a condition.
IF … THEN … ENDIF
IF Score >= 50 THEN OUTPUT "Pass" ENDIF
IF … THEN … ELSE … ENDIF
IF Score >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF
Nested IF (ELSE IF)
IF Score >= 90 THEN OUTPUT "Grade A" ELSE IF Score >= 70 THEN OUTPUT "Grade B" ELSE IF Score >= 50 THEN OUTPUT "Grade C" ELSE OUTPUT "Fail" ENDIF ENDIF ENDIF
CASE OF … ENDCASE
Use CASE OF when selecting from several possible values of a single variable (similar to switch/match in other languages):
DECLARE Day : INTEGER INPUT Day CASE OF Day 1 : OUTPUT "Monday" 2 : OUTPUT "Tuesday" 3 : OUTPUT "Wednesday" 4 : OUTPUT "Thursday" 5 : OUTPUT "Friday" 6, 7 : OUTPUT "Weekend" OTHERWISE : OUTPUT "Invalid day" ENDCASE
OTHERWISE clause in CASE OF is the default — it runs when no other value matches. It is equivalent to ELSE in an IF statement.
Iteration — Loops
CAIE pseudocode has three loop structures. Choose the right one based on whether the repetition count is known in advance.
FOR … TO … NEXT (Count-Controlled)
Use when the number of iterations is known before the loop starts:
// Print 1 to 10 FOR i <- 1 TO 10 OUTPUT i NEXT i // Count down from 10 to 1 using STEP FOR i <- 10 TO 1 STEP -1 OUTPUT i NEXT i
WHILE … DO … ENDWHILE (Pre-condition)
The condition is checked before each iteration. If it is FALSE from the start, the loop body never executes:
DECLARE Number : INTEGER INPUT Number WHILE Number <> 0 DO OUTPUT "Number is: " & Number INPUT Number ENDWHILE
REPEAT … UNTIL (Post-condition)
The body runs at least once; the condition is checked after each iteration. The loop stops when the condition becomes TRUE:
DECLARE Password : STRING REPEAT OUTPUT "Enter password: " INPUT Password UNTIL Password = "secret123"
- Known number of iterations →
FOR … NEXT - Unknown, check first →
WHILE … ENDWHILE - Unknown, run at least once →
REPEAT … UNTIL
Arrays
An array is a fixed-size collection of elements of the same data type. CAIE arrays are 1-indexed by default (the first element is at index 1), but any lower bound can be used.
Declaring a 1D Array
// ARRAY [lower:upper] OF type DECLARE Scores : ARRAY[1:10] OF INTEGER DECLARE Names : ARRAY[1:5] OF STRING // Assign values Scores[1] <- 85 Scores[2] <- 72 // Read values OUTPUT Scores[1] // outputs 85
Iterating Through an Array
DECLARE Scores : ARRAY[1:5] OF INTEGER DECLARE i : INTEGER DECLARE Total : INTEGER Total <- 0 FOR i <- 1 TO 5 INPUT Scores[i] Total <- Total + Scores[i] NEXT i OUTPUT "Average: " & Total / 5
Declaring a 2D Array
// A 3-row × 4-column grid of integers DECLARE Grid : ARRAY[1:3, 1:4] OF INTEGER DECLARE Row, Col : INTEGER // Fill grid with zeros FOR Row <- 1 TO 3 FOR Col <- 1 TO 4 Grid[Row, Col] <- 0 NEXT Col NEXT Row
Records (Composite / User-Defined Types)
A record (also called a composite type or struct) groups together fields of different types under a single name. Useful for storing related data — e.g. all details about a student.
Defining and Using a Record Type
// Define the type TYPE Student DECLARE Name : STRING DECLARE Age : INTEGER DECLARE Grade : CHAR ENDTYPE // Declare a variable of that type DECLARE Pupil1 : Student // Assign field values using dot notation Pupil1.Name <- "Alice" Pupil1.Age <- 17 Pupil1.Grade <- 'A' // Output a field OUTPUT Pupil1.Name & " is in grade " & Pupil1.Grade
Array of Records
DECLARE ClassList : ARRAY[1:30] OF Student ClassList[1].Name <- "Bob" ClassList[1].Age <- 16
Procedures
A procedure is a named block of code that performs a task. It does not return a value. Call it by name.
Defining a Procedure
PROCEDURE Greet(Name : STRING) OUTPUT "Hello, " & Name & "!" ENDPROCEDURE
Calling a Procedure
CALL Greet("Alice") // outputs: Hello, Alice! CALL Greet("Bob") // outputs: Hello, Bob!
Procedure with No Parameters
PROCEDURE PrintDivider() OUTPUT "----------------------------" ENDPROCEDURE CALL PrintDivider()
Functions
A function is like a procedure but it returns a value. Declare its return type with RETURNS and use RETURN inside to send the value back.
Defining a Function
FUNCTION Square(n : INTEGER) RETURNS INTEGER RETURN n * n ENDFUNCTION
Calling a Function
DECLARE Result : INTEGER Result <- Square(5) // Result = 25 OUTPUT Square(7) // outputs 49
A More Complex Function
FUNCTION IsEven(n : INTEGER) RETURNS BOOLEAN IF n MOD 2 = 0 THEN RETURN TRUE ELSE RETURN FALSE ENDIF ENDFUNCTION
Parameters — BYVALUE and BYREF
Parameters can be passed to procedures and functions in two ways:
| Mode | Keyword | Behaviour |
|---|---|---|
| By Value | BYVALUE |
A copy of the argument is passed. Changes inside the subprogram do not affect the original variable. (Default if not specified.) |
| By Reference | BYREF |
The actual variable is passed. Changes inside the subprogram do affect the original variable. |
// BYVALUE — original is not changed PROCEDURE DoubleIt(BYVALUE x : INTEGER) x <- x * 2 OUTPUT x ENDPROCEDURE // BYREF — original IS changed PROCEDURE AddTen(BYREF value : INTEGER) value <- value + 10 ENDPROCEDURE DECLARE Num : INTEGER Num <- 5 CALL AddTen(Num) OUTPUT Num // outputs 15
BYREF when a procedure needs to modify the caller's variable (e.g. a sorting procedure). Use BYVALUE (or omit it) when you only need to read the argument.
File Handling
CAIE pseudocode supports reading from and writing to text files using a small set of keywords.
| Keyword | Purpose |
|---|---|
| OPENFILE | Opens a file for reading or writing |
| READFILE | Reads the next line from an open file into a variable |
| WRITEFILE | Writes a value/string to an open file |
| CLOSEFILE | Closes a file (always do this when finished) |
| EOF() | Returns TRUE when the end of file has been reached |
File Modes
| Mode | Meaning |
|---|---|
| READ | Open for reading only |
| WRITE | Open for writing (creates file or overwrites) |
| APPEND | Open for appending (adds to end of existing file) |
Reading from a File
DECLARE Line : STRING OPENFILE "data.txt" FOR READ WHILE NOT EOF("data.txt") DO READFILE "data.txt", Line OUTPUT Line ENDWHILE CLOSEFILE "data.txt"
Writing to a File
OPENFILE "output.txt" FOR WRITE WRITEFILE "output.txt", "Hello, world!" WRITEFILE "output.txt", "Second line" CLOSEFILE "output.txt"
Appending to a File
OPENFILE "log.txt" FOR APPEND WRITEFILE "log.txt", "New log entry" CLOSEFILE "log.txt"
Object-Oriented Programming (OOP)
CAIE A-Level pseudocode supports full object-oriented programming, including classes, inheritance, and polymorphism.
Defining a Class
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
Creating and Using Objects
DECLARE MyDog : Animal MyDog <- NEW Animal("Rex", "Woof") CALL MyDog.Speak() // Rex says Woof OUTPUT MyDog.GetName() // Rex
Inheritance
Use INHERITS to create a subclass. The subclass gains all the public methods and attributes of the parent:
CLASS Dog INHERITS Animal PRIVATE Breed : STRING PUBLIC PROCEDURE NEW(n : STRING, b : STRING) CALL SUPER.NEW(n, "Woof") // call parent constructor Breed <- b ENDPROCEDURE PUBLIC FUNCTION GetBreed() RETURNS STRING RETURN Breed ENDFUNCTION ENDCLASS DECLARE Fido : Dog Fido <- NEW Dog("Fido", "Labrador") CALL Fido.Speak() // Fido says Woof (inherited) OUTPUT Fido.GetBreed() // Labrador
PUBLIC— accessible from outside the classPRIVATE— accessible only within the class (encapsulation)
Built-in Functions
CAIE pseudocode provides a set of standard library functions you can use without defining them yourself.
String Functions
| Function | Description | Example |
|---|---|---|
| LEFT(s, n) | First n characters of string s | LEFT("Hello", 3) → "Hel" |
| RIGHT(s, n) | Last n characters of string s | RIGHT("Hello", 3) → "llo" |
| MID(s, start, n) | n characters from position start | MID("Hello", 2, 3) → "ell" |
| LENGTH(s) | Number of characters in string s | LENGTH("Hi") → 2 |
| LCASE(s) | Converts string to lowercase | LCASE("Hello") → "hello" |
| UCASE(s) | Converts string to uppercase | UCASE("hello") → "HELLO" |
| TO_UPPER(c) | Converts a CHAR to uppercase | TO_UPPER('a') → 'A' |
| TO_LOWER(c) | Converts a CHAR to lowercase | TO_LOWER('A') → 'a' |
Numeric Functions
| Function | Description | Example |
|---|---|---|
| INT(x) | Converts a REAL to INTEGER (truncates) | INT(3.9) → 3 |
| ROUND(x, n) | Rounds x to n decimal places | ROUND(3.567, 2) → 3.57 |
| RAND(n) | Returns a random REAL in range 0 ≤ r < n | RAND(10) → e.g. 7.3 |
Type Conversion
| Function | Description | Example |
|---|---|---|
| NUM_TO_STR(n) | Converts a number to a STRING | NUM_TO_STR(42) → "42" |
| STR_TO_NUM(s) | Converts a STRING to a number | STR_TO_NUM("42") → 42 |
| ASC(c) | ASCII code of a character | ASC('A') → 65 |
| CHR(n) | Character from ASCII code | CHR(65) → 'A' |
| IS_NUM(s) | Returns TRUE if string is a valid number | IS_NUM("42") → TRUE |
Style Conventions & Exam Tips
Follow these conventions to write clear, mark-worthy pseudocode in exams.
IF, WHILE, DECLARE, etc. — must be fully capitalised.StudentScore, not x. PascalCase for variables and procedures is standard.DECLARE variables with their type before use — marks may be lost otherwise.// comments to explain complex logic. Examiners appreciate clarity.IF needs ENDIF, every FOR needs NEXT, every WHILE needs ENDWHILE, etc.Common Mistakes to Avoid
- Using
=for assignment instead of<- - Forgetting to close blocks (
ENDIF,ENDWHILE,ENDFUNCTION) - Confusing
CHAR(single quotes) withSTRING(double quotes) - Using 0-based array indexing — CAIE arrays start at index 1 by default
- Forgetting
CALLwhen invoking a procedure - Using
RETURNin aPROCEDURE— onlyFUNCTIONs return values - Not closing files after use with
CLOSEFILE
Quick Syntax Reference
// ── Declarations ────────────────────────────────────── DECLARE MyVar : INTEGER CONSTANT MAX <- 100 // ── Assignment ───────────────────────────────────────── MyVar <- 42 // ── Input / Output ───────────────────────────────────── INPUT MyVar OUTPUT "Value is: " & MyVar // ── Selection ────────────────────────────────────────── IF MyVar > 0 THEN OUTPUT "Positive" ELSE OUTPUT "Non-positive" ENDIF // ── Loops ────────────────────────────────────────────── FOR i <- 1 TO MAX OUTPUT i NEXT i WHILE MyVar <> 0 DO INPUT MyVar ENDWHILE REPEAT INPUT MyVar UNTIL MyVar = 0 // ── Subprograms ──────────────────────────────────────── PROCEDURE DoSomething(BYREF x : INTEGER) x <- x + 1 ENDPROCEDURE FUNCTION GetDouble(x : INTEGER) RETURNS INTEGER RETURN x * 2 ENDFUNCTION