A-Level Computer Science / Unit 10: Organising Data in Programs

10.1.1 Choosing the Right Data Type

πŸ”’ Lesson slides are available to signed-in users. Sign in

10.1.1 Choosing the Right Data Type

A program must store each value in a form that matches its meaning. Choosing a suitable data type makes the design clearer and helps prevent invalid operations, such as trying to calculate with an identification code.

This lesson focuses on the six main single-value types used in pseudocode: INTEGER, REAL, CHAR, STRING, BOOLEAN, and DATE. ARRAY and FILE are introduced briefly as signposts because they are developed in later Unit 10 lessons.

By the end of this section, you should be able to:

  • select an appropriate data type from the purpose of a value;
  • distinguish whole-number, fractional, textual, logical, and date values;
  • explain why a numeric-looking code may need to be stored as a STRING;
  • write simple pseudocode declarations using suitable type names;
  • justify a choice by referring to the value and the operations required.

Start with the Purpose, Not the Appearance

The same symbols can represent different kinds of information. For example, 2407 could be a quantity used in arithmetic, or it could be a room-entry code that must keep all four digits. The correct type depends on what the program needs to do with the value.

Data type: a category that describes the form of a value and the operations that are appropriate for it.

A practical decision route

  1. Identify what the value represents.
  2. Decide whether arithmetic will be performed on it.
  3. If it is numeric, ask whether a fractional part is possible.
  4. If it is text, decide whether exactly one character or several characters are needed.
  5. If it records a two-state condition, consider BOOLEAN.
  6. If it represents a calendar date that may be compared or calculated with, consider DATE.
  7. If it stores many related values or persistent data, ARRAY or FILE may be needed later.
Exam tip: A strong justification links the type to both the meaning of the value and the operations required. Do not write only β€œbecause it is a number”.

Numeric Types: INTEGER and REAL

Use a numeric type only when the value represents a number that may be used in calculations or numeric comparisons.

Type What it stores Original example Suitable reason
INTEGER A signed whole number DroneCount = 18 The quantity cannot contain a fractional part.
REAL A signed numeric value that may contain a fractional part WindSpeed = 12.75 The measurement may need digits after the decimal point.
Signed: able to represent positive values, zero, and negative values.

A REAL may also hold a value that currently appears whole, such as 20.0, if future results may contain a fractional part. Choose the type from the full range of possible values, not from one sample value.

Common mistake: Do not choose REAL for every value involving digits. A count, index, or number of attempts normally needs INTEGER because fractions are not meaningful.

Text and Codes: CHAR and STRING

Textual values are stored as characters rather than treated as quantities. This includes names, messages, labels, reference numbers, and codes.

Type What it stores Original example Typical use
CHAR Exactly one character 'N' A compass direction, single menu choice, or one symbol
STRING Zero or more characters in sequence "QF-0082" A name, sentence, label, or identification code

Why a code is often a STRING

A parcel reference such as "004817" contains digits, but it is not a quantity. Adding or multiplying it would not be meaningful, and the leading zeros must be preserved. STRING is therefore more suitable than INTEGER.

The empty string "" is also a valid STRING. It contains no characters, so its length is zero, but it still has a data type.

Common mistake: '8', "8", and 8 are not interchangeable. They represent a CHAR, a STRING, and an INTEGER respectively.

Logical and Calendar Values: BOOLEAN and DATE

Type Possible content Original example Why it fits
BOOLEAN TRUE or FALSE SafetyCheckPassed = TRUE The value records one of two logical states.
DATE A calendar date; some systems may also include a time InspectionDate = 21/11/2027 The program may compare dates or calculate intervals.

A BOOLEAN is especially useful as a flag: a named value that records whether a condition has been met. Examples include Found, InputValid, and GameFinished.

A date could be displayed as text, but DATE is the better design choice when the program needs date-specific operations such as ordering events or finding the number of days between two dates.

Exam tip: For BOOLEAN, describe the two states clearly. For DATE, mention a date-specific operation when explaining why ordinary STRING storage would be less suitable.

Where ARRAY and FILE Fit

The pseudocode vocabulary for this unit also includes ARRAY and FILE. They solve different storage problems from the six single-value types above.

Type Use it when... Covered in detail
ARRAY several related values need to be stored and processed under one identifier; Sections 10.2.1 and 10.2.2
FILE data must remain available after the program has stopped; Sections 10.3.1 and 10.3.2
Important distinction: choosing INTEGER, REAL, CHAR, STRING, BOOLEAN, or DATE identifies the kind of a single value. Choosing ARRAY or FILE also describes how one or more values are organised or retained.

Using Data Types in Pseudocode

In pseudocode, a declaration connects an identifier to the kind of value it is intended to store.

DECLARE DeliveryCount : INTEGER
DECLARE MeanJourneyTime : REAL
DECLARE AccessLevel : CHAR
DECLARE LockerReference : STRING
DECLARE DoorIsLocked : BOOLEAN
DECLARE CollectionDate : DATE
Identifier: a meaningful name used to refer to a variable, constant, data structure, procedure, or function.

The identifier name should describe the value, while the type should match the value's purpose. For example, LockerReference is a STRING because it is a label such as "L-041", not a quantity.

Exam tip: Use the exact uppercase pseudocode type names. When a declaration is requested, include the identifier, colon, and type.

Interactive: Data Type Selector

Select a value and trace how we decide its most suitable pseudocode data type. Use this tool to practise explaining the evidence for the choice, rather than only naming the type.

Selected value 27

It is a signed whole number, so INTEGER is the best match.

Best data type INTEGER

Primitive / atomic data type

Step 1 of 4

Look at the value

First decide whether the value is numeric, textual, logical, or date-based.

Stores A signed whole number.
Good for Counting, indexing, or values that must not have a decimal part.
Watch out Do not use REAL if the value should always be whole.

Worked Example: Designing Data for a Smart Greenhouse

A greenhouse controller must store information about a growing zone. The program needs the zone label, the number of active lamps, the latest humidity reading, the current operating mode, whether ventilation is running, and the next inspection date.

Identifier Example value Chosen type Reason
ZoneLabel "GH-04" STRING It is a multi-character code and is not used in arithmetic.
ActiveLampCount 16 INTEGER A count must be a whole number.
HumidityPercent 67.4 REAL The sensor reading may contain a fractional part.
OperatingMode 'A' CHAR Exactly one character represents the mode.
VentilationRunning FALSE BOOLEAN Only the on/off logical state is required.
NextInspection 03/02/2028 DATE The controller may compare it with the current date.

Corresponding declarations

DECLARE ZoneLabel : STRING
DECLARE ActiveLampCount : INTEGER
DECLARE HumidityPercent : REAL
DECLARE OperatingMode : CHAR
DECLARE VentilationRunning : BOOLEAN
DECLARE NextInspection : DATE
Method: For each identifier, state what the value represents, identify the operations it needs, and then select the narrowest suitable type.

Common Mistakes and Misconceptions

  • Choosing a type from one example value instead of the full set of possible values.
  • Using REAL for a count, even though fractional counts are impossible.
  • Using INTEGER for a numeric-looking code and accidentally losing leading zeros.
  • Using CHAR for a value that may contain more than one character.
  • Using STRING values such as "yes" and "no" when a two-state BOOLEAN is sufficient.
  • Storing a date as ordinary text when the program must compare dates or calculate intervals.
  • Confusing the empty string "" with an undeclared or missing value.

Practice

Part A: Choose and justify

  1. PassengerCount stores the number of passengers currently on a ferry.
  2. WaterDepth stores a measurement such as 6.35 metres.
  3. RiskBand stores one of the characters 'L', 'M', or 'H'.
  4. TicketReference stores a value such as "00073-X".
  5. PaymentConfirmed records whether a payment check succeeded.
  6. DepartureDate is compared with today's date.

Part B: Think more carefully

  1. Explain why RoomCode = "021" should not normally use INTEGER.
  2. Could BatteryPercent be INTEGER or REAL? State the requirement that would decide.
  3. Explain one advantage of DATE over STRING for storing an appointment date.
  4. Name the later Unit 10 type that would suit a collection of 30 temperature readings.
  5. Name the later Unit 10 type that would allow readings to remain available after the program closes.
Check the suggested answers
  1. INTEGER β€” a passenger count is a whole-number quantity.
  2. REAL β€” the measurement may contain a fractional part.
  3. CHAR β€” exactly one character is stored.
  4. STRING β€” it is a code, may contain several characters, and leading zeros matter.
  5. BOOLEAN β€” the check has two logical outcomes.
  6. DATE β€” the value is used in date comparison.
  7. It is an identifier rather than a quantity; INTEGER could remove the leading zero.
  8. INTEGER if only whole percentages are recorded; REAL if fractional percentages are possible.
  9. DATE supports date ordering and date calculations more naturally.
  10. ARRAY.
  11. FILE.

Review

Question to ask Likely choice
Is it a whole-number quantity? INTEGER
Can a numeric result contain a fractional part? REAL
Is exactly one character required? CHAR
Is it text, a label, or a multi-character code? STRING
Does it record one of two logical states? BOOLEAN
Is it a calendar value used in date operations? DATE
Are many related values needed under one identifier? ARRAY β€” developed later
Must data persist after execution ends? FILE β€” developed later
Final exam tip: Give the exact uppercase type name and a concise justification. The best answers explain why the type supports the required values and operations.