D

10.1 Data Types & Records

Understanding primitive and composite data types, and how they are used in programming

Learning Objectives

By the end of this lesson, you will be able to:

  • Define what data types are and explain why they are important in programming
  • Identify and describe different primitive data types (Boolean, char, date, integer, real, string)
  • Explain what a structured type is and identify string as a special case
  • Declare variables with appropriate data types using pseudocode syntax
  • Define and use composite user-defined data types (records)
  • Create and manipulate record data types with multiple fields of different types
  • Declare arrays of records and access individual fields using dot notation
  • Apply data types appropriately in real-world programming scenarios

Key Terms

Data Type

A classification that specifies which type of value a variable can hold and what operations can be performed on it

Primitive Data Type

A fundamental data type built into a programming language (also called atomic data type)

Boolean

A data type that can have only two values: TRUE or FALSE (represented as 1 and 2 in some systems)

Char

A data type that stores a single alphanumerical character

Date

A data type that represents calendar dates

Integer

A data type for whole numbers (positive, negative, or zero) without decimal points

Real

A data type for numbers with decimal points (also called float or floating-point)

String

A structured data type that stores a sequence of alphanumerical characters

Empty String

A string value that contains no characters (a special case of string data type)

Structured Type

A data type composed of other data types (e.g., string is a sequence of characters)

Composite Data Type

A user-defined data type that combines multiple data types (e.g., records)

Record Data Type

A composite data type structure containing a fixed number of components of different types

Declaration

The process of specifying a variable's data type before it can be used in a program

Identifier

A name given to a variable, constant, or other programming element

Dot Notation

The syntax used to access fields within a record (e.g., Person.Name)

Introduction to Data Types

Data types allow programming languages to provide different classifications for items of data, so they can be used for different purposes. For example, integers are discrete whole numbers used for counting and indexing, whereas real numbers can be used to provide accurate measurements.

Why Data Types Matter

Think of data types like different containers in your kitchen:

Wrong Container = Problems!
  • You wouldn't store milk in a salt shaker
  • You wouldn't put cereal in a teacup
  • Each container is designed for specific contents
Right Container = Works Perfectly!
  • Milk goes in a jug or bottle
  • Cereal goes in a bowl
  • Each data type is designed for specific values
Key Insight: Declaration is Like Labeling Containers

When you declare a variable with a data type, you're telling the computer: "This container will hold THIS type of data." This helps prevent errors and makes your code more efficient.

Data Types Visualizer

Click on each data type card to see examples and understand what kind of values it can store:

Boolean
True/False values
Char
Single character
Date
Calendar dates
Integer
Whole numbers
Real
Decimal numbers
String
Text sequences

Click on any data type card above to see examples and explanations.

Variable Declaration in Pseudocode

Before data can be used, its data type needs to be decided. This is done by declaring the data type for each item to be used. In pseudocode, a variable can be declared as:

DECLARE <identifier> : <Data Type>

The <identifier> is the name you give to the variable, and <Data Type> specifies what kind of data it will hold.

Real-Life Example: Online Shopping System

Consider an online shopping system. Different types of data need different data types:

Product Information
Product Name: STRING (e.g., "Wireless Mouse")
Price: REAL (e.g., 24.99)
Quantity in Stock: INTEGER (e.g., 150)
In Stock: BOOLEAN (TRUE/FALSE)
Order Information
Order Date: DATE (e.g., 2024-03-15)
Customer Initial: CHAR (e.g., 'J')
Order Number: INTEGER (e.g., 10472)
Shipping Address: STRING (e.g., "123 Main St")

Using the wrong data type would cause errors. For example, trying to multiply a STRING ("Wireless Mouse") by 2 makes no sense, but multiplying a REAL (24.99) by 2 for a discount does!

Check Your Understanding: Data Types Basics

Answer
  • [1 mark] To provide different classifications for items of data
  • [1 mark] So data can be used for different purposes and operations
  • [Additional] Example: Integers for counting, real numbers for measurements, strings for text
Answer
DECLARE StudentAge : INTEGER

Note: DECLARE keyword, identifier (StudentAge), colon, then data type (INTEGER).

Answer
  • [1 mark] Because it is composed of (structured from) multiple characters
  • [1 mark] It is a sequence of alphanumerical characters rather than a single value
  • [Additional] Example: "Hello" is structured from characters 'H', 'e', 'l', 'l', 'o'
Answer
  • [1 mark] An empty string is a string value with no characters stored in it
  • [1 mark] It is a special case because it has the string data type but contains no data
  • [Additional] Represented as "" (two quotation marks with nothing between them)
Answer
  • [1 mark] When you need to represent measurements with decimal precision
  • [1 mark] Example: Temperature (36.6°C), currency amounts (£19.99), scientific measurements (3.14159)
  • [Additional] INTEGER would round these values, losing important precision

Primitive Data Types

Primitive data types (also called atomic data types) refer to fundamental data types provided by a programming language. These data types are built into the language and are used to represent simple values.

Primitive Data Types Table

Data type Description Pseudocode Python Java VB.NET
Boolean Logical values, True (1) and False (2) BOOLEAN bool boolean Boolean
char Single alphanumerical character CHAR Not used char Char
date Value to represent a date DATE class datetime class Date Date
integer Whole number, positive or negative INTEGER int byte short int long Integer
real Positive or negative number with a decimal point REAL float float double single
string Sequence of alphanumerical characters STRING str class String String

Important Notes About Primitive Data Types

Key Points to Remember
  • Boolean values are often represented as 1 (True) and 2 (False) in some systems
  • Char stores exactly ONE character (e.g., 'A', '5', '$')
  • String can store ZERO or MORE characters (including empty string "")
  • Real numbers are also called "floating-point" numbers
  • Integer has no decimal point - it's a whole number
Common Student Mistakes
  • Confusing CHAR and STRING (char = one character, string = multiple)
  • Using INTEGER for measurements that need decimals (use REAL instead)
  • Forgetting that Boolean can only be TRUE/FALSE, not other values
  • Trying to perform math operations on STRING data

Data Type Comparison Visualizer

Select values to see which data type they belong to. Some values can belong to multiple types!

Select a value to see which data types it could belong to.

Why some values can have multiple types:

  • 42 is INTEGER, but could also be stored as REAL (42.0) or even STRING ("42")
  • 0 is INTEGER, but could also be BOOLEAN (FALSE in some systems)
  • "A" could be CHAR (single character) or STRING (one-character string)
  • The context determines which data type is most appropriate!

Real-Life Example: Student Database System

A school's student information system uses different data types for different information:

StudentID INTEGER
FirstName STRING
LastName STRING
DateOfBirth DATE
GradeAverage REAL
YearGroup INTEGER
IsBoarder BOOLEAN
Initial CHAR

Each data type is chosen carefully: GradeAverage needs REAL for decimal precision (e.g., 87.5%), IsBoarder needs BOOLEAN (TRUE/FALSE), and Initial needs CHAR (just one letter like 'J').

Activity 1: Data Type Identification

For each value below, identify which primitive data type(s) it could be. Some values may have more than one possible type.

  1. 17
  2. "Computer Science"
  3. FALSE
  4. 9.81
  5. 'Z'
  6. 2025-12-25
  7. 0
  8. ""
  9. -100
  10. TRUE
Solution:
  1. 17: INTEGER (could also be REAL as 17.0 or STRING as "17")
  2. "Computer Science": STRING
  3. FALSE: BOOLEAN
  4. 9.81: REAL (could be STRING as "9.81" but that's less common)
  5. 'Z': CHAR (could be STRING as "Z")
  6. 2025-12-25: DATE
  7. 0: INTEGER (in some systems could represent BOOLEAN FALSE)
  8. "": STRING (empty string)
  9. -100: INTEGER (negative whole number)
  10. TRUE: BOOLEAN

Note: The most appropriate data type depends on context. For example, 0 is usually INTEGER, but in a true/false context could be BOOLEAN.

Check Your Understanding: Primitive Data Types

Answer
  • [1 mark] CHAR stores exactly one alphanumerical character
  • [1 mark] STRING stores a sequence of zero or more alphanumerical characters
  • [Additional] Example: CHAR = 'A', STRING = "Hello" or "" (empty string)
Answer
  • [1 mark] When the data requires decimal precision
  • [1 mark] For measurements, currency, scientific values, or any values that aren't whole numbers
  • [Additional] Example: Temperature (36.6°C), price (£19.99), average grade (87.5%)
Answer
  • [1 mark] TRUE as 1 and FALSE as 2 (in some systems)
  • [Additional] In other systems, TRUE might be 1 and FALSE might be 0, or TRUE might be -1
Answer
DECLARE Temperature : REAL

This tells the computer that Temperature will store decimal numbers like 23.5 or 36.6.

Answer
  • [1 mark] A fundamental data type built into a programming language
  • [1 mark] Used to represent simple values (not composed of other types)
  • [Additional] Examples: INTEGER, REAL, BOOLEAN, CHAR. Contrast with structured types like STRING.

Composite User-defined Data Types

A composite data type is a user-defined data type that combines multiple data types. The most common composite data type is the record.

Record Data Type

What is a Record?

A record is a composite data type structure that contains a fixed number of components, which can be of different types. It allows the programmer to collect together values with different data types under a single identifier.

Advantage of Record Data Type:

A set of data related to one thing of different type is held under a single identifier.

Real-World Analogy

Think of a record like a student file folder:

  • The folder (record) contains different types of documents (fields)
  • Each document has specific information (data of specific type)
  • All documents belong to one student (single identifier)
  • You can access any document individually when needed

Defining Record Types in Pseudocode

Example 1: Employee Record

TYPE EmployeeRecord
  DECLARE EmployeeFirstName : STRING
  DECLARE EmployeeFamilyName : STRING
  DECLARE DateEmployed : DATE
  DECLARE Salary : CURRENCY
ENDTYPE

This creates a new data type called EmployeeRecord with four fields of different types.

Example 2: Person Type

TYPE PersonType
  DECLARE Name : STRING
  DECLARE DateOfBirth : DATE
  DECLARE Height : REAL
  DECLARE NumberOfSiblings : INTEGER
  DECLARE IsFullTimeStudent : BOOLEAN
ENDTYPE

This record type mixes STRING, DATE, REAL, INTEGER, and BOOLEAN fields.

Example 3: Book Record

TYPE TbookRecord
  DECLARE title : STRING
  DECLARE author : STRING
  DECLARE publisher : STRING
  DECLARE noPages : INTEGER
  DECLARE fiction : BOOLEAN
ENDTYPE

After defining this type, we can declare variables of type TbookRecord.

Using Record Types

Declaring Record Variables

Once a record type is defined, you can declare variables of that type:

// Using PersonType defined earlier
DECLARE Person : PersonType
// Using TbookRecord defined earlier
DECLARE Book1 : TbookRecord

Now Person and Book1 are variables that can store all the fields defined in their record types.

Accessing Record Fields

Use dot notation to access individual fields of a record:

// Assign values to Person fields
Person.Name <- "Fred"
Person.NumberOfSiblings <- 3
Person.IsFullTimeStudent <- TRUE

// Output a field
OUTPUT Person.Name

The dot (.) separates the record variable name from the field name.

Arrays of Records

You can create arrays of records, which is extremely useful for storing multiple records of the same type (like a database of people or books).

// Declare an array of 100 Person records
DECLARE Person : ARRAY[1:100] OF PersonType
// Access individual records in the array
Person[1].Name <- "Fred"
OUTPUT Person[1].Name
Why Arrays of Records Are Powerful

Instead of having separate arrays for names, birthdates, heights, etc., you have ONE array where each element is a complete record. This keeps related data together and makes your code much more organized!

Record Structure Visualizer

Select a record type to see its structure and how fields are accessed:

Select a record type to view its structure.

Real-Life Example: Hospital Patient Records

A hospital system uses records to store patient information efficiently:

TYPE PatientRecord
  DECLARE PatientID : INTEGER
  DECLARE Name : STRING
  DECLARE DateOfBirth : DATE
  DECLARE BloodType : CHAR
  DECLARE Temperature : REAL
  DECLARE WardNumber : INTEGER
  DECLARE IsCritical : BOOLEAN
ENDTYPE

Without records:

DECLARE PatientIDs : ARRAY[1:1000] OF INTEGER
DECLARE PatientNames : ARRAY[1:1000] OF STRING
DECLARE PatientDOBs : ARRAY[1:1000] OF DATE
// 7 separate arrays = hard to manage!

With records:

DECLARE Patients : ARRAY[1:1000] OF PatientRecord
// 1 array = all related data together!

Patients[1].Name <- "John Smith"
Patients[1].IsCritical <- TRUE

Records keep all related patient data together, making the code cleaner and reducing errors (like mismatched array indices).

Activity 2: Working with Records

Using the following record definition:

TYPE StudentRecord
  DECLARE StudentID : INTEGER
  DECLARE Name : STRING
  DECLARE YearGroup : INTEGER
  DECLARE AverageGrade : REAL
  DECLARE IsBoarder : BOOLEAN
ENDTYPE

Answer the following questions:

  1. Write pseudocode to declare a variable called "Student1" of type StudentRecord.
  2. Write pseudocode to assign the following values to Student1:
    • StudentID = 10472
    • Name = "Alex Chen"
    • YearGroup = 12
    • AverageGrade = 87.5
    • IsBoarder = TRUE
  3. Write pseudocode to output the student's name and average grade.
  4. Write pseudocode to declare an array called "ClassList" that can store 30 StudentRecord items.
  5. Write pseudocode to set the name of the first student in ClassList to "Maria Garcia".
Solution:
  1. Declare Student1:
    DECLARE Student1 : StudentRecord
  2. Assign values to Student1:
    Student1.StudentID <- 10472
    Student1.Name <- "Alex Chen"
    Student1.YearGroup <- 12
    Student1.AverageGrade <- 87.5
    Student1.IsBoarder <- TRUE
  3. Output name and grade:
    OUTPUT Student1.Name
    OUTPUT Student1.AverageGrade
  4. Declare ClassList array:
    DECLARE ClassList : ARRAY[1:30] OF StudentRecord
  5. Set first student's name:
    ClassList[1].Name <- "Maria Garcia"

Note: Remember the dot notation for accessing fields and the array indexing for accessing specific records in an array.

Check Your Understanding: Composite Data Types

Answer
  • [1 mark] A composite data type structure containing fixed number of components of different types
  • [1 mark] Allows collecting related data of different types under single identifier
  • [1 mark] Advantage: Keeps related data together, more organized than separate variables/arrays
  • [Additional] Example: Student record with name (STRING), age (INTEGER), grade (REAL) all together
Answer
TYPE Book
  DECLARE title : STRING
  DECLARE author : STRING
  DECLARE pages : INTEGER   DECLARE available : BOOLEAN
ENDTYPE

Note: TYPE keyword to define, DECLARE for each field, ENDTYPE to end definition.

Answer
MyBook.author

This uses dot notation: record variable name, dot, field name.

Answer
  • [1 mark] Keeps all related data for one item together in one place
  • [1 mark] Reduces errors from mismatched array indices across parallel arrays
  • [Additional] More organized code: instead of StudentNames[i], StudentAges[i], StudentGrades[i], you have Students[i].Name, Students[i].Age, Students[i].Grade
Answer
// Assuming Book type is already defined
DECLARE Library : ARRAY[1:50] OF Book

// Set title of 10th book
Library[10].title <- "Computer Science"

Note: Array index in square brackets [10], then dot notation to access the title field.

Key Takeaways

  • Data types classify data so it can be used for different purposes (integers for counting, reals for measurements, etc.)
  • Primitive (atomic) data types are built into programming languages: BOOLEAN, CHAR, DATE, INTEGER, REAL, STRING
  • STRING is a structured type (sequence of characters) with a special case: empty string (no characters)
  • Variables must be declared with a data type before use: DECLARE identifier : DataType
  • Composite data types like records allow grouping different data types under one identifier
  • A record contains a fixed number of components (fields) that can be of different types
  • Define a record with TYPE ... ENDTYPE and declare fields with DECLARE field : DataType
  • Access record fields using dot notation: RecordName.FieldName
  • You can create arrays of records to store multiple records efficiently
  • Arrays of records are better than parallel arrays because they keep related data together
  • Choose data types carefully based on what the data represents and what operations will be performed on it
  • Common mistake: Using INTEGER when REAL is needed for decimal values
  • Common mistake: Confusing CHAR (single character) with STRING (sequence of characters)
  • Records improve code organization, especially when working with related data of different types

Question Bank

Marking Scheme & Answer
  • [2 marks] Importance: Data types classify data for different purposes, determine valid operations, prevent errors, optimize memory usage
  • [1 mark] INTEGER: Whole numbers for counting, indexing, quantities (e.g., student count, array indices)
  • [1 mark] REAL: Decimal numbers for measurements, calculations needing precision (e.g., temperature, price, average grade)
  • [1 mark] STRING: Text data for names, addresses, descriptions (e.g., student name, book title)
  • [1 mark] BOOLEAN: True/False values for flags, conditions (e.g., isPassed, isAvailable)
  • [Additional] Without data types, computers wouldn't know how to interpret or process data correctly (e.g., is "101" a number or text?)
Marking Scheme & Answer
  • [1 mark] CHAR: Stores exactly one alphanumerical character
  • [1 mark] STRING: Stores sequence of zero or more alphanumerical characters
  • [1 mark] CHAR use: Single character data like initials ('J'), yes/no responses ('Y'/'N'), menu choices ('A', 'B', 'C')
  • [1 mark] STRING use: Text data like names ("John"), addresses ("123 Main St"), sentences ("Hello world")
  • [Additional] Key difference: CHAR is fixed at one character, STRING can be empty ("") or many characters. CHAR is primitive, STRING is structured.
Marking Scheme & Answer
// Defining the record type
TYPE CarRecord
  DECLARE Make : STRING
  DECLARE Model : STRING
  DECLARE Year : INTEGER
  DECLARE Price : REAL
  DECLARE IsAutomatic : BOOLEAN
ENDTYPE

// Declaring and assigning values
DECLARE MyCar : CarRecord

MyCar.Make <- "Toyota"
MyCar.Model <- "Corolla"
MyCar.Year <- 2022
MyCar.Price <- 24500.00
MyCar.IsAutomatic <- TRUE

[3 marks] for correct record definition with appropriate fields and data types
[3 marks] for correct declaration and assignment using dot notation

Marking Scheme & Answer
  • [1 mark] Parallel arrays: Separate arrays for each field (e.g., names array, ages array, grades array)
  • [1 mark] Array of records: Single array where each element is a complete record
  • [1 mark] Advantage 1: Keeps all related data together - Student[i] contains name, age, grade together
  • [1 mark] Advantage 2: Reduces errors - no risk of mismatched indices across arrays
  • [1 mark] Example: With parallel arrays: if you delete StudentNames[5], you must remember to delete StudentAges[5] and StudentGrades[5]. With array of records: just delete Students[5].
  • [Additional] Code is cleaner and more maintainable with arrays of records, especially as more fields are added.
Marking Scheme & Answer
  • [1 mark] a) Age: INTEGER - Age is a whole number, no decimal points needed
  • [1 mark] b) Product price: REAL - Prices often have decimal points (e.g., £19.99)
  • [1 mark] c) Passed exam: BOOLEAN - True/False value (either passed or not)
  • [1 mark] d) Book title: STRING - Text data of varying length
  • [1 mark] e) Single letter grade: CHAR - Exactly one character (A, B, C, etc.)
  • [1 mark] f) Date of birth: DATE - Specifically designed for calendar dates
  • [Additional] Alternative for e) could be STRING, but CHAR is more precise since it's exactly one character.
Marking Scheme & Answer
// Declare array of student records
DECLARE Students : ARRAY[1:100] OF StudentRecord

// Variables to track highest grade and student
DECLARE HighestGrade : REAL
DECLARE TopStudentName : STRING

// Initialize
HighestGrade <- Students[1].AverageGrade
TopStudentName <- Students[1].Name

// Loop through all students
FOR i <- 2 TO 100
  IF Students[i].AverageGrade > HighestGrade THEN
    HighestGrade <- Students[i].AverageGrade
    TopStudentName <- Students[i].Name
  ENDIF
NEXT i

// Output result
OUTPUT "Top student: ", TopStudentName
OUTPUT "Grade: ", HighestGrade

[2 marks] for correct array declaration
[2 marks] for initializing variables
[3 marks] for correct loop and comparison logic
[1 mark] for correct output
[Additional] Note the use of dot notation to access record fields within the array.

Marking Scheme & Answer
  • [1 mark] An empty string is a string value with no characters stored in it
  • [1 mark] It is represented as two quotation marks with nothing between them: ""
  • [1 mark] It is a special case because it has the STRING data type but contains no actual character data
  • [Additional] Useful for initializing string variables or representing "no text" conditions. Different from a string containing a space character (" ").
Marking Scheme & Answer
  • [1 mark] Situation: A library management system tracking books
  • [1 mark] Without records: Separate variables for each book's title, author, ISBN, available status, etc. - very messy with many books
  • [1 mark] With records: Define Book record type with all fields, then create array of Book records
  • [1 mark] Benefit: All data for one book is together; easy to add new books; code is organized; functions can accept entire Book record as parameter
  • [Additional] Other examples: Student information system, hospital patient records, e-commerce product catalog, employee database.
Marking Scheme & Answer
  • [1 mark] CHAR is a primitive type because it represents a single, indivisible value (one character)
  • [1 mark] STRING is a structured type because it is composed of (structured from) multiple CHAR values in sequence
  • [Additional] Example: STRING "Hello" is structured from CHAR values 'H', 'e', 'l', 'l', 'o'. It has internal structure (sequence) while CHAR does not.
Marking Scheme & Answer
  • [1 mark] Primitive data types: Fundamental types built into language; represent simple, indivisible values
  • [1 mark] Example: INTEGER, REAL, BOOLEAN, CHAR
  • [1 mark] Composite data types: User-defined types composed of other types; combine multiple values
  • [1 mark] Example: Record (combines multiple fields of different types), array (collection of same type)
  • [Additional] Primitive types are like basic building blocks; composite types are structures built from those blocks.