Data types and records
Choosing the right data type
- Every variable has a data type — the kind of value it holds and the operations allowed.
- Picking well keeps programs correct and efficient.
- When several values describe one thing, a record groups them.
The basic data types
INTEGER— a whole number (counts, indexes, IDs).REAL— a number with a fractional part (money, measurements).STRING— text in quotes;CHAR— a single character.BOOLEAN—TRUE/FALSE(flags);DATE— a calendar date.- Pick the smallest precise type — a
BOOLEANflag, not the strings "yes"/"no".
Practice
Which data type best stores a yes/no flag like "in stock"?
A flag has two states, so BOOLEAN (TRUE/FALSE) is the precise choice — not the strings "yes"/"no".
Practice
Which type is best for a price like 3.99?
A value with a fractional part needs REAL; INTEGER holds only whole numbers.
Records
- A record holds several fields of different types under one name.
TYPE TStockItem
DECLARE ItemID : INTEGER
DECLARE Category : STRING
DECLARE ItemCost : REAL
ENDTYPE
DECLARE Item1 : TStockItem
Item1.Category ← "Fruit" // dot notation reaches a field
- Use a record when values always belong together (a customer, a stock item); use separate variables for unrelated values.
Practice
A record is used to:
A record bundles fields of (possibly) different types that describe one thing. An array holds many values of the same type.
Practice
To set the Category field of a record variable Item1, you write:
Dot notation record.field reaches a field of a record.
You've got it
Key idea
- types:
INTEGER,REAL,STRING,CHAR,BOOLEAN,DATE— pick the smallest precise fit - a record groups several fields of different types under one name
- reach a field with dot notation (
Item1.Category) - use a record when the values always describe one thing together