Programming Concepts
Programming · 4 question types
Exam Frequency Analysis
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).
Variables
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
Constants
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 MaxLives ← 3
Constants are named in the same PascalCase style as variables, and they make programs easier to maintain: if the VAT rate changes, only one line of the program has to be edited.
Naming conventions
Identifiers (the names of variables and constants) should:
- Use letters and digits only, no spaces.
- Start with a capital letter, not a digit.
- Be in PascalCase: each word capitalised, e.g.
TotalScore. - Be descriptive:
StudentScoreis far better thansorx. - Never contain an underscore, an accented letter or any other symbol — and this applies to constant names too.
Data types
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:
- A
CHARis exactly one character, written in single quotes ('A'). - A
STRINGis zero or more characters, written in double quotes ("Apple").
Casting
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.
Picking the right data type
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.