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.
A practical decision route
- Identify what the value represents.
- Decide whether arithmetic will be performed on it.
- If it is numeric, ask whether a fractional part is possible.
- If it is text, decide whether exactly one character or several characters are needed.
- If it records a two-state condition, consider BOOLEAN.
- If it represents a calendar date that may be compared or calculated with, consider DATE.
- If it stores many related values or persistent data, ARRAY or FILE may be needed later.
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. |
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.
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.
'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.
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 |
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
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.
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.
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
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
PassengerCountstores the number of passengers currently on a ferry.WaterDepthstores a measurement such as6.35metres.RiskBandstores one of the characters'L','M', or'H'.TicketReferencestores a value such as"00073-X".PaymentConfirmedrecords whether a payment check succeeded.DepartureDateis compared with today's date.
Part B: Think more carefully
- Explain why
RoomCode = "021"should not normally use INTEGER. - Could
BatteryPercentbe INTEGER or REAL? State the requirement that would decide. - Explain one advantage of DATE over STRING for storing an appointment date.
- Name the later Unit 10 type that would suit a collection of 30 temperature readings.
- Name the later Unit 10 type that would allow readings to remain available after the program closes.
Check the suggested answers
- INTEGER β a passenger count is a whole-number quantity.
- REAL β the measurement may contain a fractional part.
- CHAR β exactly one character is stored.
- STRING β it is a code, may contain several characters, and leading zeros matter.
- BOOLEAN β the check has two logical outcomes.
- DATE β the value is used in date comparison.
- It is an identifier rather than a quantity; INTEGER could remove the leading zero.
- INTEGER if only whole percentages are recorded; REAL if fractional percentages are possible.
- DATE supports date ordering and date calculations more naturally.
- ARRAY.
- 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 |