Pseudocode reference
Pseudocode syntax reference
Statements, names, data types, constants, operators, library routines, and block endings supported by the browser compiler.
Core statements
Use DECLARE for variables, <- for assignment, INPUT for user input, and OUTPUT for displayed values.
Pick Cambridge IGCSE, Cambridge A Level, IB, OCR, or AQA in Settings so the compiler follows that board's notation.
DECLARE Total : INTEGER
Total <- 0
OUTPUT TotalNames, comments, and layout
Consistent names and layout make a program easier to mark and easier to debug.
- Write keywords in upper case:
IF,FOR,WHILE,PROCEDURE. - Write identifiers in PascalCase, starting with a capital letter, such as
RowTotal. - Don't use underscores in identifiers.
- Start a comment with
//. - Indent the body of every block by four spaces.
Data types and constants
Declare a variable with DECLARE Name : TYPE before you use it. Declare a constant with CONSTANT Name <- value. A constant's value must be a literal, not an expression.
| Type | Holds | Example |
|---|---|---|
INTEGER | A whole number | 42 |
REAL | A number with a fractional part | 3.142 |
CHAR | A single character | 'M' |
STRING | Text of any length | "Mai" |
BOOLEAN | A truth value | TRUE or FALSE |
CONSTANT Pi <- 3.142
DECLARE Radius : REAL
DECLARE Initial : CHAR
DECLARE Name : STRING
DECLARE Passed : BOOLEAN
Radius <- 2.5
Initial <- 'M'
Name <- "Mai"
Passed <- TRUE
OUTPUT Name, " ", Initial, " ", Pi * Radius ^ 2, " ", PassedOperators
Comparisons and logical operators produce a BOOLEAN, so you can use them directly in IF, WHILE, and UNTIL conditions.
| Kind | Operators |
|---|---|
| Arithmetic | + - * / ^ |
| Integer division | DIV(a, b) gives the whole-number quotient; MOD(a, b) gives the remainder |
| Comparison | = < <= > >= <> |
| Logical | AND OR NOT |
Library routines
These routines are built in and appear in autocomplete. ROUND returns a REAL, so store its result in a REAL variable.
| Routine | Returns |
|---|---|
LENGTH(Text) | The number of characters in a string |
LCASE(Text), UCASE(Text) | The string in lower or upper case |
SUBSTRING(Text, Start, Length) | Part of a string; the first character is position 1 |
ROUND(Value, Places) | A real number rounded to that many decimal places |
RANDOM() | A random real number from 0 to 1 inclusive |
DECLARE Phrase : STRING
DECLARE Roll : REAL
Phrase <- "Happy Days"
OUTPUT LENGTH(Phrase)
OUTPUT LCASE(Phrase), " ", UCASE(Phrase)
OUTPUT SUBSTRING(Phrase, 1, 5)
OUTPUT ROUND(15.6789, 2)
OUTPUT DIV(17, 5), " ", MOD(17, 5)
Roll <- ROUND(RANDOM() * 5, 0) + 1
OUTPUT RollCheat sheet
Every block has a matching closing keyword. Matching block endings makes programs easier to trace and debug.
| Structure | Opens with | Closes with |
|---|---|---|
| Selection | IF ... THEN | ENDIF |
| Multi-way selection | CASE OF ... | ENDCASE |
| Count-controlled loop | FOR ... TO ... | NEXT ... |
| Pre-condition loop | WHILE ... DO | ENDWHILE |
| Post-condition loop | REPEAT | UNTIL ... |
| Procedure | PROCEDURE ... | ENDPROCEDURE |
| Function | FUNCTION ... RETURNS ... | ENDFUNCTION |