Choosing a data type means deciding what kind of value a variable holds and what the program may do with it. Exam questions ask for the type of a value, or give a short scenario and ask you to declare variables for it.
This lesson is part of the programming structure module. The five types used in Cambridge-style pseudocode are INTEGER, REAL, STRING, CHAR and BOOLEAN. Confirm the names for your exam year on the Cambridge subject page.
How do I decide which type fits?
Ask two questions about the value. First, what does it look like: a whole number, a number with a fractional part, text, one character, or true/false? Second, what will the program do with it: calculate, compare, join together, or test?
| Type | Holds | Example value |
|---|---|---|
| INTEGER | whole numbers | 24 |
| REAL | numbers with a fractional part | 12.50 |
| STRING | zero or more characters | “Maya” |
| CHAR | exactly one character | ‘B’ |
| BOOLEAN | TRUE or FALSE | TRUE |
The second question breaks ties. A value made of digits is not automatically a number: if you will never calculate with it, it is text.
Worked example
A fictional school canteen stores one record per item. Choose a type for each field and declare it.
- Item code, such as “0457”
- Quantity in stock, such as 36
- Price in RM, such as 3.50
- Size letter S, M or L
- Is the item halal-certified, TRUE or FALSE
Item code. It is made of digits, but nobody adds two item codes. It starts with a zero that must be kept. So it is a STRING.
Quantity. Stock is counted in whole items, so INTEGER.
Price. It has a fractional part, so REAL.
Size letter. Exactly one character from a fixed set, so CHAR.
Certified. Only two states, so BOOLEAN.
DECLARE ItemCode : STRING
DECLARE Quantity : INTEGER
DECLARE Price : REAL
DECLARE Size : CHAR
DECLARE IsCertified : BOOLEAN
A quick check of a result: total value of stock is Quantity × Price = 36 × 3.50 = 126.00, which is a REAL. The product of an INTEGER and a REAL is a REAL.
Which mistake catches students most often?
Mistaken answer: “Item code 0457 is made of digits, so INTEGER.”
If you store 0457 in an INTEGER, the zero at the start is lost and the value becomes 457. Another item might really be coded 457, so two different items now look identical.
The correction is the test above: does the program calculate with it? A code, phone number or postcode identifies something and is never added, so it is a STRING.
A related slip is using INTEGER for an average. An average of 72.5 needs REAL, because the fractional part would be lost.
Self-check
1. Choose a type for the number of pupils in a class.
Show answer
INTEGER. Pupils are counted in whole numbers.
2. Choose a type for the postcode 08000.
Show answer
STRING. It is never used in a calculation and the zero at the start must be kept.
3. Choose types for (a) the grade letter ‘A’ and (b) the result of 15 / 4.
Show answer
(a) CHAR, a single character. (b) REAL, because 15 / 4 = 3.75 has a fractional part.
Where does this lead next?
Once variables have the right type, the next question is where they can be used. That is covered in local and global scope. The restricted pseudocode trace trainer lets you step through short examples, and the safe Python reasoning sandbox shows the same ideas in Python.
If you can name types but lose marks on the reason, our online one-to-one Computer Science tuition can drill the wording with you.