IGCSE Pseudocode Reference
Complete language reference for the Cambridge IGCSE (0478) pseudocode syntax supported by this compiler.
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
OUTPUT "Enter your name:"
INPUT Name
OUTPUT "Hello, ", NameYou can also input directly into an array element: INPUT Scores[i]
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"
ENDIFCASE / 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"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
OUTPUT "Enter a mark (0-100):"
INPUT Mark
UNTIL Mark >= 0 AND Mark <= 100
OUTPUT "Valid mark entered: ", Mark