Skip to content
IGCSE·Tuition
Computer Science · Lessons

Choose an appropriate data type

You know the value you want to store, but the question asks which type fits and every option looks possible.

On this page
  1. How do I decide which type fits?
  2. Worked example
  3. Which mistake catches students most often?
  4. Self-check
  5. Where does this lead next?

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?

TypeHoldsExample value
INTEGERwhole numbers24
REALnumbers with a fractional part12.50
STRINGzero or more characters“Maya”
CHARexactly one character‘B’
BOOLEANTRUE or FALSETRUE

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.

Questions people ask

Why is a phone number or postcode stored as STRING and not INTEGER?

You never do arithmetic on it, and an INTEGER drops zeros at the start. The postcode 08000 would be stored as 8000, which is a different value. A STRING keeps every character exactly as typed. A good test is to ask whether adding or averaging the values would mean anything.

What is the difference between CHAR and STRING?

A CHAR holds exactly one character, such as 'Y' or 'A'. A STRING holds zero or more characters, such as "Y" or "Maya". A one-letter STRING is still a STRING. Use CHAR when the value can only ever be a single character, such as a grade letter.

When do I use REAL instead of INTEGER?

Use REAL when the value can have a fractional part, such as a price of 12.50 or an average of 72.5. Use INTEGER for whole-number counts, such as a number of seats. Check your exam year's syllabus for the exact list of data types it names.

Updated:

Your next step

If you keep choosing a type by habit rather than by what the program does with the value, a one-to-one teacher can question your choices live and show you the test that settles each one.

Paid one-hour trial at your assigned teacher’s confirmed rate, starting from RM80. Other fees, schedules and ongoing arrangements are confirmed directly with your teacher after the trial class.

Tuition is arranged with a parent or guardian. Send them this page on WhatsApp and they can enquire for you.

Parent or guardian? Enquire here

9,000+ students helped through our service