Programming
Variables, data types, selection, iteration, operators, strings, procedures, functions, arrays, file handling and maintainable code.
1 Data Types & Variables
Click or hover over each card to see its description and examples.
e.g. 5, -3, 0, 100
e.g. 3.14, -2.5, 0.0
e.g. 'A', '5', '@', ' '
e.g. "Hello", "42", ""
TRUE or FALSE
"35" is a STRING, not an INTEGER โ the speech marks make it text. You cannot perform arithmetic on it directly.
// Variables โ value can change during execution DECLARE Score : INTEGER DECLARE Average : REAL DECLARE FirstName : STRING DECLARE Grade : CHAR DECLARE Passed : BOOLEAN // Constants โ value cannot change during execution CONSTANT MaxScore โ 100 CONSTANT PI โ 3.14159
2 Input & Output
INPUT Name INPUT Score OUTPUT "Your score is: ", Score OUTPUT "Hello, ", Name, "!"
3 Selection โ IF & CASE
IF Score >= 50 THEN OUTPUT "Pass" ENDIF
IF Score >= 70 THEN OUTPUT "Distinction" ELSE IF Score >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF ENDIF
Use CASE when testing one variable against multiple specific values. It is cleaner than a long chain of IF/ELSE IF statements.
CASE OF Grade 'A' : OUTPUT "Excellent" 'B' : OUTPUT "Good" 'C' : OUTPUT "Average" OTHERWISE : OUTPUT "Below Average" ENDCASE
4 Iteration
Used when the number of iterations is known in advance. The loop variable is automatically incremented.
// Basic FOR loop FOR i โ 1 TO 5 OUTPUT i NEXT i // Outputs: 1 2 3 4 5 // With STEP (counting backwards by 2) FOR i โ 10 TO 1 STEP -2 OUTPUT i NEXT i // Outputs: 10 8 6 4 2
The condition is checked before each iteration. If the condition is false at the start, the body never executes.
WHILE Number > 0 DO OUTPUT Number Number โ Number - 1 ENDWHILE
The body executes at least once โ the condition is checked after each iteration. Continues until the condition becomes TRUE.
REPEAT OUTPUT "Enter password: " INPUT Password UNTIL Password = "Secret123"
Key difference: WHILE checks condition first (may run 0 times). REPEAT UNTIL checks condition last (runs at least once).
5 Operators
+addition-subtraction*multiplication/division (real result)^power ofMODremainder after divisionDIVinteger quotient
=equal to<>not equal to<<=>>=ANDboth conditions trueORat least one trueNOTinverts a Boolean value
MOD(10, 3) // returns 1 (10 mod 3 = remainder) DIV(10, 3) // returns 3 (integer division) ROUND(3.567, 2) // returns 3.57 RANDOM() // returns a random number 0.0 to 1.0 inclusive
6 String Handling
| Function | Description | Example | Result | |
|---|---|---|---|---|
LENGTH(s) | Number of characters in string s | UCASE(s) | LCASE(s) | SUBSTRING(s, start, len)
|
The first character of a string is at position 1 in Cambridge pseudocode (not 0). Always check the question wording if it specifies otherwise.
7 Procedures & Functions
- A named, reusable block of code
- Does NOT return a value
- Called using
CALL - Can take 0 or more parameters
- Used for actions (display, save)
- A named, reusable block of code
- Returns a single value
- Called within an expression (no CALL)
- Uses
RETURNto send back a value - Used for calculations
PROCEDURE DisplayGreeting(Name : STRING) OUTPUT "Hello, ", Name ENDPROCEDURE CALL DisplayGreeting("Arj") // outputs: Hello, Arj
FUNCTION Square(n : INTEGER) RETURNS INTEGER RETURN n * n ENDFUNCTION Result โ Square(5) // Result = 25 OUTPUT Square(4) // outputs: 16
Local vs Global Variables
| Local Variable | Global Variable | |
|---|---|---|
| Declared inside} -- | ||
| Accessible from} -- | ||
| Lifetime} -- | ||
| Preferred?} -- |
8 Arrays โ 1D & 2D
An array is a fixed-length data structure that stores multiple elements of the same data type, accessed using an index number.
// Declare: 5 integers, index 1 to 5 DECLARE Scores : ARRAY[1:5] OF INTEGER Scores[1] โ 85 Scores[2] โ 72 // Read all values with a loop FOR i โ 1 TO 5 OUTPUT Scores[i] NEXT i
// Declare: 3ร3 grid of characters DECLARE Grid : ARRAY[1:3, 1:3] OF CHAR Grid[1,1] โ 'X' Grid[2,3] โ 'O' // Nested loop to process entire grid FOR row โ 1 TO 3 FOR col โ 1 TO 3 Grid[row, col] โ ' ' NEXT col NEXT row
9 File Handling
| Command | Purpose |
|---|---|
OPENFILE "file.txt" FOR READ} --
| |
OPENFILE "file.txt" FOR WRITE} --
| |
OPENFILE "file.txt" FOR APPEND} --
| |
READFILE "file.txt", Variable} --
| |
WRITEFILE "file.txt", Variable} --
| |
CLOSEFILE "file.txt"} --
| |
EOF("file.txt")} --
|
DECLARE Line : STRING OPENFILE "data.txt" FOR READ WHILE NOT EOF("data.txt") DO READFILE "data.txt", Line OUTPUT Line ENDWHILE CLOSEFILE "data.txt"
10 Maintainable Code
Maintainable code is easy to read, understand and modify by any programmer โ not just the original author.
| Technique | Description | Example |
|---|---|---|
| Meaningful identifiers} -- | ||
| Comments} -- | ||
| Procedures/Functions} -- | ||
| Constants} -- |
For full marks on maintainability questions, mention all four: meaningful identifiers, comments, procedures/functions, and constants.
