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 variables (which can change) and constants (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.Cambridge IGCSE recognises 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 char 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 casting. 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.
Example — Choose the most appropriate data type for each piece of data, using a different type for each.
| Data | Best data type | Why |
|---|---|---|
83 | Integer | A whole number |
myemail@example.com | String | A sequence of characters |
TRUE | Boolean | A logical value |
'M' | Char | A single character (e.g. gender code) |
3.14 | Real | A number with a fractional part |