Pseudocode Compiler

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-insensitiveIF, if, and If all 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 : CHAR

Constants

Use CONSTANT for values that never change during execution:

CONSTANT HourlyRate <- 6.50
CONSTANT MaxSize <- 100
CONSTANT DefaultText <- "N/A"

Data Types

TypeDescriptionExample
INTEGERWhole numbers5, -3, 0
REALDecimal numbers4.7, 0.3
CHARSingle character (single quotes)'x', 'A'
STRINGText (double quotes)"Hello"
BOOLEANLogical valueTRUE, 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 + B

INPUT

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, ", Name

You 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 i

2D 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

OperatorDescriptionExample
+Addition5 + 3 → 8
-Subtraction10 - 4 → 6
*Multiplication3 * 7 → 21
/Division10 / 3 → 3.33…
^Power2 ^ 3 → 8
DIVInteger division10 DIV 3 → 3
MODRemainder (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        // 8

Comparison Operators

OperatorMeaning
=Equal to
<>Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

Logical Operators

OperatorDescription
ANDBoth conditions must be true
ORAt least one condition must be true
NOTInverts a boolean value
IF Age >= 18 AND HasID = TRUE THEN
    OUTPUT "Entry allowed"
ENDIF

IF NOT Found THEN
    OUTPUT "Item not found"
ENDIF

String 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"
ENDIF

Multiple conditions with ELSEIF:

IF Score >= 90 THEN
    OUTPUT "A"
ELSEIF Score >= 80 THEN
    OUTPUT "B"
ELSEIF Score >= 70 THEN
    OUTPUT "C"
ELSE
    OUTPUT "F"
ENDIF

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"
ENDCASE

Iteration (Loops)

FOR Loop

Count-controlled loop. The counter variable is automatically incremented (or stepped):

FOR Counter <- 1 TO 10
    OUTPUT Counter
NEXT Counter

Use 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 i

WHILE 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 Number

The 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

FunctionDescription
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)        // llo

Math Functions

FunctionDescription
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-5

Type Conversion

FunctionDescription
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")       // FALSE

File 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

StatementDescription
OPENFILE f FOR READOpen file for reading
OPENFILE f FOR WRITEOpen file for writing (overwrites)
OPENFILE f FOR APPENDOpen file for appending
READFILE f, varRead one line into a variable
WRITEFILE f, valueWrite a line to the file
CLOSEFILE fClose 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: ", Total

Counting

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: ", Count

Finding 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: ", Max

Linear 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"
ENDIF

Bubble 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 Swapped

Input Validation

DECLARE Mark : INTEGER
REPEAT
    OUTPUT "Enter a mark (0-100):"
    INPUT Mark
UNTIL Mark >= 0 AND Mark <= 100
OUTPUT "Valid mark entered: ", Mark