Programming · 4 question types
Past paper frequency (2018 to 2024)
This topic accounts for approximately 13% of your exam marks.
Paper 2 is almost entirely programming. Variable types, assignments, loops and conditionals are tested every session.
Every program manipulates data: numbers, text, true/false values. To work with data, the program stores values in named (which can change) and (which cannot).
A variable is a named storage location whose value can change while the program is running.
In CIE pseudocode, a variable is declared with DECLARE and a data type:
DECLARE Age : INTEGER
DECLARE Price : REAL
DECLARE Name : STRING
DECLARE FirstInitial : CHAR
DECLARE GameOver : BOOLEAN
After declaration, a value is assigned with the arrow operator ←:
Age ← 17
Price ← 9.99
Name ← "Alice"
FirstInitial ← 'A'
GameOver ← FALSE
A constant is a named storage location whose value is set once and cannot change for the rest of the program.
CONSTANT PI ← 3.142
CONSTANT VAT ← 0.20
CONSTANT MAX_LIVES ← 3
Constants are usually written in UPPERCASE so they stand out from variables, and they make programs easier to maintain: if VAT changes, only one line of the program has to be edited.
Identifiers (the names of variables and constants) should:
TotalScore) or camelCase.StudentScore is far better than s or x.There are five basic data types. Choosing the right one for each variable makes the program more accurate and uses memory efficiently.
| Data type | Used for | Pseudocode keyword | Examples |
|---|---|---|---|
| Integer | Whole numbers (no fractional part) | INTEGER | 10, -5, 0, 1000 |
| Real | Numbers with a fractional part | REAL | 3.14, -2.5, 0.0 |
| Char | A single character | CHAR | 'a', 'B', '7', '£' |
| String | A sequence of characters | STRING | "Hello world", "ABC", "" |
| Boolean | A logical value | BOOLEAN | TRUE, FALSE |
Notes on the difference between and string:
CHAR is exactly one character, written in single quotes ('A').STRING is zero or more characters, written in double quotes ("Apple").Sometimes a value needs to be converted from one type to another. This is called . For example, a number typed by the user arrives as a STRING and may need to be converted to an INTEGER before arithmetic can be done on it.
Common slip: trying to add
"5"to"3"as strings gives"53"(concatenation), not8. Cast to integer first.
A short reference for matching everyday data to the most suitable CIE type:
| Kind of value | Best type | Quick test |
|---|---|---|
| Whole-number counts (age, score, year) | INTEGER | No decimal point ever needed |
| Measurements and money (1.75 m, £8.50) | REAL | A fractional part is possible |
| Names, addresses, sentences | STRING | More than one character, or any text |
A single flag character ('M', 'F', 'Y', 'N') | CHAR | Exactly one character |
| Yes/no, on/off, paid/unpaid | BOOLEAN | Two possible values: TRUE or FALSE |
When two types look possible, prefer the smaller one. A house number is an INTEGER, not a STRING. A pH reading is a REAL, not a STRING. Storing numbers as strings means losing the ability to compare or do arithmetic on them later.
Choosing the right data type for a given value
What comes up: a matching question gives four descriptions (or database fields) and asks for the most appropriate data type for each — appears across multiple papers.
Watch out: the traps that decide the marks — a number you never do arithmetic on (phone number, barcode, student ID) is best stored as STRING even though it is digits; money (£8.50) and measurements (1.75 m) are REAL, not INTEGER, because they can have a fractional part.