Cambridge Pseudocode Reference
Complete language reference for Cambridge IGCSE (0478) and Cambridge International AS & A Level Computer Science (9618) pseudocode. IGCSE syntax is a subset — everything below works for both courses unless marked AS & A Level.
Getting Started
New to pseudocode? Start here. These four short examples walk you through the core ideas — no prior knowledge required.
Your First Program
The simplest pseudocode program just prints something to the screen. Use OUTPUT followed by a string in double quotes:
OUTPUT "Hello, World!"You can output multiple values in one line — separate them with commas:
DECLARE Score : INTEGER
Score <- 42
OUTPUT "Your score is ", Score<- assigns a value to a variable. DECLARE creates the variable first.
Asking for Input
Use INPUT to read a value the user types. The program pauses and waits:
DECLARE Name : STRING
INPUT Name, "What is your name? "
OUTPUT "Hello, ", Name, "!"The text after the comma is an optional prompt shown above the input field. Here's a slightly longer example that also does arithmetic:
DECLARE Length : REAL
DECLARE Width : REAL
INPUT Length, "Enter the length: "
INPUT Width, "Enter the width: "
OUTPUT "Area = ", Length * WidthMaking Decisions
Use IF … THEN … ELSE … ENDIF to choose between two paths:
DECLARE Mark : INTEGER
INPUT Mark, "Enter your mark: "
IF Mark >= 50 THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIFChain more conditions with ELSEIF:
DECLARE Mark : INTEGER
INPUT Mark, "Enter your mark: "
IF Mark >= 70 THEN
OUTPUT "Distinction"
ELSEIF Mark >= 50 THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIFYou can write it as ELSEIF (one word) or ELSE IF (two words) on the same line — both chain conditions the same way.
Repeating Things
Use a FOR loop when you know how many times to repeat:
FOR i <- 1 TO 5
OUTPUT i
NEXT iCombine everything: ask for five numbers and calculate their total:
DECLARE Num : INTEGER
DECLARE Total : INTEGER
Total <- 0
FOR i <- 1 TO 5
INPUT Num, "Enter number " & NUM_TO_STRING(i) & ": "
Total <- Total + Num
NEXT i
OUTPUT "Total = ", TotalReady to go deeper? Browse the full reference below — or jump straight to to learn about WHILE and REPEAT loops.
General Syntax Rules
- Comments use
//— everything after is ignored until end of line - Keywords are case-insensitive —
IF,if, andIfall work - Indentation is recommended but not enforced
- Assignment uses
<-(preferred) or= - Each statement goes on its own line
Variables, Constants & Data Types
Declaring Variables
Use DECLARE followed by the variable name, a colon, and the data type:
DECLARE Counter : INTEGER
DECLARE TotalToPay : REAL
DECLARE GameOver : BOOLEAN
DECLARE StudentName : STRING
DECLARE Initial : CHARConstants
Use CONSTANT for values that never change during execution:
CONSTANT HourlyRate <- 6.50
CONSTANT MaxSize <- 100
CONSTANT DefaultText <- "N/A"Data Types
| Type | Description | Example |
|---|---|---|
INTEGER | Whole numbers | 5, -3, 0 |
REAL | Decimal numbers | 4.7, 0.3 |
CHAR | Single character (single quotes) | 'x', 'A' |
STRING | Text (double quotes) | "Hello" |
BOOLEAN | Logical value | TRUE, FALSE |
Input and Output
OUTPUT
Use OUTPUT (or PRINT) to display values. Separate multiple values with commas:
OUTPUT "Hello, World!"
OUTPUT "You have ", Lives, " lives left"
OUTPUT "Sum = ", A + BINPUT
Use INPUT to read a value from the user. The program pauses until the user types a response:
DECLARE Name : STRING
INPUT Name
OUTPUT "Hello, ", NameYou can display a prompt message inline by adding a string after a comma — the text appears in the terminal before the input field:
DECLARE Name : STRING
INPUT Name, "Enter your name: "
OUTPUT "Hello, ", Name
// Same syntax works for array elements
DECLARE Scores : ARRAY[1:5] OF INTEGER
FOR i <- 1 TO 5
INPUT Scores[i], "Enter score " & NUM_TO_STRING(i) & ": "
NEXT iThe prompt string is optional — plain INPUT Name still works as before.
Arrays
1D Arrays
Declare with ARRAY[lower:upper] OF type. Indices are inclusive:
DECLARE Names : ARRAY[1:5] OF STRING
Names[1] <- "Alice"
Names[2] <- "Bob"
FOR i <- 1 TO 5
OUTPUT Names[i]
NEXT i2D Arrays
Declare with two ranges separated by a comma. Access with [row, col]:
DECLARE Grid : ARRAY[1:3, 1:3] OF CHAR
Grid[1,1] <- 'X'
Grid[2,2] <- 'O'
OUTPUT Grid[1,1]Operators
Arithmetic Operators
| Operator | Description | Example |
|---|---|---|
| + | Addition | 5 + 3 → 8 |
| - | Subtraction | 10 - 4 → 6 |
| * | Multiplication | 3 * 7 → 21 |
| / | Division | 10 / 3 → 3.33… |
| ^ | Power | 2 ^ 3 → 8 |
DIV | Integer division | 10 DIV 3 → 3 |
MOD | Remainder (modulo) | 10 MOD 3 → 1 |
DIV and MOD also work as functions: DIV(10, 3), MOD(10, 3)
OUTPUT DIV(10, 3) // 3
OUTPUT MOD(10, 3) // 1
OUTPUT 2 ^ 3 // 8Comparison Operators
| Operator | Meaning |
|---|---|
| = | Equal to |
| <> | Not equal to |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
Logical Operators
| Operator | Description |
|---|---|
AND | Both conditions must be true |
OR | At least one condition must be true |
NOT | Inverts a boolean value |
IF Age >= 18 AND HasID = TRUE THEN
OUTPUT "Entry allowed"
ENDIF
IF NOT Found THEN
OUTPUT "Item not found"
ENDIFString Concatenation
Use & to join strings together:
FullName <- FirstName & " " & LastName
OUTPUT "Hello " & Name & "!"Selection
IF / ELSE / ELSEIF
Basic if-then-else:
IF Score >= 50 THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIFMultiple conditions with ELSEIF:
IF Score >= 90 THEN
OUTPUT "A"
ELSEIF Score >= 80 THEN
OUTPUT "B"
ELSEIF Score >= 70 THEN
OUTPUT "C"
ELSE
OUTPUT "F"
ENDIFELSEIF may also be written as two words, ELSE IF, on the same line.
CASE / OTHERWISE
Multi-way selection on a single variable. Use OTHERWISE as a default:
CASE OF Grade
'A' :
OUTPUT "Excellent"
'B' :
OUTPUT "Good"
'C' :
OUTPUT "Average"
OTHERWISE:
OUTPUT "Below average"
ENDCASEIteration (Loops)
FOR Loop
Count-controlled loop. The counter variable is automatically incremented (or stepped):
FOR Counter <- 1 TO 10
OUTPUT Counter
NEXT CounterUse STEP to control the increment (including negative values for counting down):
FOR i <- 10 TO 1 STEP -1
OUTPUT i
NEXT i
FOR i <- 0 TO 20 STEP 2
OUTPUT i // 0, 2, 4, 6, ... 20
NEXT iWHILE Loop
Pre-condition loop — tests the condition before each iteration. May execute zero times:
DECLARE Number : INTEGER
Number <- 100
WHILE Number > 9 DO
Number <- Number - 9
ENDWHILE
OUTPUT NumberThe DO keyword after the condition is optional.
REPEAT Loop
Post-condition loop — always executes at least once, then checks the condition:
DECLARE Password : STRING
REPEAT
OUTPUT "Enter the password:"
INPUT Password
UNTIL Password = "Secret"Procedures and Functions
Procedures
Procedures perform actions but do not return a value. Invoke with CALL:
PROCEDURE Greet(Name : STRING)
OUTPUT "Hello, ", Name, "!"
ENDPROCEDURE
CALL Greet("Alice")
CALL Greet("Bob")Functions
Functions return a value with RETURN and can be used inside expressions:
FUNCTION Square(N : INTEGER) RETURNS INTEGER
RETURN N * N
ENDFUNCTION
OUTPUT "5 squared = ", Square(5)
// Functions can call other functions
FUNCTION Max(A : INTEGER, B : INTEGER) RETURNS INTEGER
IF A > B THEN
RETURN A
ELSE
RETURN B
ENDIF
ENDFUNCTION
OUTPUT "Larger: ", Max(10, 25)Built-in Functions
String Functions
| Function | Description |
|---|---|
LENGTH(s) | Number of characters in a string |
LCASE(s) | Convert to lowercase |
UCASE(s) | Convert to uppercase |
SUBSTRING(s, start, len) | Extract characters (1-based index) |
MID(s, start, len) | Same as SUBSTRING (alias) |
LEFT(s, len) | Leftmost characters |
RIGHT(s, len) | Rightmost characters |
OUTPUT LENGTH("Hello") // 5
OUTPUT UCASE("Hello") // HELLO
OUTPUT LCASE("Hello") // hello
OUTPUT SUBSTRING("Hello", 1, 3) // Hel
OUTPUT MID("Hello", 2, 3) // ell
OUTPUT LEFT("Hello", 2) // He
OUTPUT RIGHT("Hello", 3) // lloMath Functions
| Function | Description |
|---|---|
ROUND(n, places) | Round to decimal places |
RANDOM() | Random number between 0 and 1 |
INT(n) | Truncate to integer (towards zero) |
DIV(a, b) | Integer division |
MOD(a, b) | Remainder after division |
OUTPUT ROUND(3.14159, 2) // 3.14
OUTPUT INT(7.9) // 7
OUTPUT INT(-2.3) // -2
OUTPUT INT(RANDOM() * 6) // Random 0-5Type Conversion
| Function | Description |
|---|---|
ASC(c) | ASCII code of a character |
CHR(n) | Character from ASCII code |
NUM_TO_STRING(n) | Convert number to string |
STRING_TO_NUM(s) | Convert string to number |
IS_NUM(s) | Check if string is numeric (returns BOOLEAN) |
OUTPUT ASC('A') // 65
OUTPUT CHR(65) // A
OUTPUT NUM_TO_STRING(42) // "42"
OUTPUT STRING_TO_NUM("99") // 99
OUTPUT IS_NUM("123") // TRUE
OUTPUT IS_NUM("abc") // FALSEFile Handling
Files are simulated using browser localStorage. Data persists between sessions. You can view and manage files using the folder icon in the editor tab bar.
File Operations
| Statement | Description |
|---|---|
OPENFILE f FOR READ | Open file for reading |
OPENFILE f FOR WRITE | Open file for writing (overwrites) |
OPENFILE f FOR APPEND | Open file for appending |
READFILE f, var | Read one line into a variable |
WRITEFILE f, value | Write a line to the file |
CLOSEFILE f | Close the file |
EOF(f) | TRUE if end of file reached |
Writing to a File
OPENFILE "scores.txt" FOR WRITE
WRITEFILE "scores.txt", "Alice,95"
WRITEFILE "scores.txt", "Bob,87"
CLOSEFILE "scores.txt"Reading from a File
DECLARE Line : STRING
OPENFILE "scores.txt" FOR READ
WHILE NOT EOF("scores.txt") DO
READFILE "scores.txt", Line
OUTPUT Line
ENDWHILE
CLOSEFILE "scores.txt"Appending adds lines without overwriting existing content:
OPENFILE "log.txt" FOR APPEND
WRITEFILE "log.txt", "New entry"
CLOSEFILE "log.txt"Cambridge International AS & A Level Computer Science (9618)
User-Defined Types
Records group related values under one identifier. Access fields with dot notation; assigning a record copies it:
TYPE StudentRecord
DECLARE LastName : STRING
DECLARE YearGroup : INTEGER
ENDTYPE
DECLARE Pupil1 : StudentRecord
DECLARE Form : ARRAY[1:30] OF StudentRecord
Pupil1.LastName <- "Johnson"
Pupil1.YearGroup <- 6
Form[1] <- Pupil1
Form[1].YearGroup <- Form[1].YearGroup + 1Enumerated types list their possible values. Adding or subtracting an integer moves through the list:
TYPE Season = (Spring, Summer, Autumn, Winter)
DECLARE ThisSeason : Season
ThisSeason <- Spring
ThisSeason <- ThisSeason + 1 // SummerPointers hold the address of another variable.^x takes the address of x; ptr^ reads or writes the value at that address:
TYPE TIntPointer = ^INTEGER
DECLARE MyPointer : TIntPointer
DECLARE Num : INTEGER
Num <- 5
MyPointer <- ^Num
OUTPUT MyPointer^ // 5
MyPointer^ <- 10 // Num is now 10Note: ^ is also the power operator. x ^ 2 is still x squared — but to raise a variable to a negative exponent, write it in brackets: x ^ (-1).
Sets are declared with SET OF and given values with DEFINE:
TYPE LetterSet = SET OF CHAR
DEFINE Vowels ('A', 'E', 'I', 'O', 'U') : LetterSetDATE Type
DATE values are written as dd/mm/yyyy and compare chronologically:
DECLARE Birthday : DATE
Birthday <- 02/01/2005
IF Birthday < 01/01/2010 THEN
OUTPUT "Born before 2010"
ENDIFA run of digits shaped exactly like dd/mm/yyyy is read as a date — write division with spaces (10 / 02 / 2005) if you really mean arithmetic.
BYREF & BYVAL Parameters
By default parameters are passed by value (the procedure gets a copy). BYREFpasses a reference so the procedure changes the caller's variable. The keyword applies to the parameters after it too, until BYVAL switches back:
PROCEDURE SWAP(BYREF X : INTEGER, Y : INTEGER)
DECLARE Temp : INTEGER
Temp <- X
X <- Y // changes the caller's variable
Y <- Temp // Y is also BYREF (sticky)
ENDPROCEDURE
CALL SWAP(a, b)CASE Ranges
CASE labels can be ranges (value1 TO value2, inclusive) or several values separated by commas:
CASE OF Mark
80 TO 100 : OUTPUT "Grade A"
60 TO 79 : OUTPUT "Grade B"
'x', 'X' : OUTPUT "Absent"
OTHERWISE : OUTPUT "Ungraded"
ENDCASERandom-Access Files
Random files store records at numbered positions. SEEK moves the file pointer, PUTRECORD writes the record there and GETRECORD reads it back:
OPENFILE "StudentFile.dat" FOR RANDOM
SEEK "StudentFile.dat", 10
PUTRECORD "StudentFile.dat", Pupil
SEEK "StudentFile.dat", 10
GETRECORD "StudentFile.dat", Found
CLOSEFILE "StudentFile.dat"Classes & Objects
Classes group properties and methods. The constructor is a procedure named NEW; INHERITS creates a subclass and SUPERcalls the parent's methods. Members marked PRIVATE can only be used inside the class:
CLASS Pet
PRIVATE Name : STRING
PUBLIC PROCEDURE NEW(GivenName : STRING)
Name <- GivenName
ENDPROCEDURE
ENDCLASS
CLASS Cat INHERITS Pet
PRIVATE Breed : STRING
PUBLIC PROCEDURE NEW(GivenName : STRING, GivenBreed : STRING)
SUPER.NEW(GivenName)
Breed <- GivenBreed
ENDPROCEDURE
ENDCLASS
MyCat <- NEW Cat("Kitty", "Shorthaired")
Player.SetAttempts(5) // method call
OUTPUT Player.GetAttempts() // method in an expressionNote: the AS & A Level keywords (TYPE, SET, DATE, NEW, CLASS, RANDOM, …) still work as ordinary variable names — DECLARE Date : STRING or DECLARE Class : STRING are fine. Only SUPER, ENDTYPE, ENDCLASS and the original IGCSE keywords (such as IF, NEXT, READ) are fully reserved.
Common Patterns
These patterns appear frequently in IGCSE exam questions:
Totaling
DECLARE Numbers : ARRAY[1:5] OF INTEGER
DECLARE Total : INTEGER
Total <- 0
FOR i <- 1 TO 5
Total <- Total + Numbers[i]
NEXT i
OUTPUT "Total: ", TotalCounting
DECLARE Count : INTEGER
Count <- 0
FOR i <- 1 TO 100
IF Numbers[i] > 50 THEN
Count <- Count + 1
ENDIF
NEXT i
OUTPUT "Count above 50: ", CountFinding Maximum
DECLARE Max : INTEGER
Max <- Numbers[1]
FOR i <- 2 TO 10
IF Numbers[i] > Max THEN
Max <- Numbers[i]
ENDIF
NEXT i
OUTPUT "Maximum: ", MaxLinear Search
DECLARE Found : BOOLEAN
DECLARE SearchName : STRING
Found <- FALSE
OUTPUT "Enter name to search:"
INPUT SearchName
DECLARE i : INTEGER
i <- 1
WHILE i <= 10 AND NOT Found DO
IF Names[i] = SearchName THEN
Found <- TRUE
OUTPUT "Found at position ", i
ENDIF
i <- i + 1
ENDWHILE
IF NOT Found THEN
OUTPUT "Not found"
ENDIFBubble Sort
DECLARE Temp : INTEGER
DECLARE Swapped : BOOLEAN
REPEAT
Swapped <- FALSE
FOR i <- 1 TO 9
IF Numbers[i] > Numbers[i + 1] THEN
Temp <- Numbers[i]
Numbers[i] <- Numbers[i + 1]
Numbers[i + 1] <- Temp
Swapped <- TRUE
ENDIF
NEXT i
UNTIL NOT SwappedInput Validation
DECLARE Mark : INTEGER
REPEAT
INPUT Mark, "Enter a mark (0-100): "
UNTIL Mark >= 0 AND Mark <= 100
OUTPUT "Valid mark entered: ", Mark