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:
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:
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
Order Information
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
1. What is the main purpose of data types in programming languages? [2 marks]
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
2. Write the pseudocode to declare a variable called "StudentAge" as an integer. [1 mark]
Answer
Note: DECLARE keyword, identifier (StudentAge), colon, then data type (INTEGER).
3. Why is string considered a "structured type"? [2 marks]
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'
4. What is an "empty string" and why is it a special case? [2 marks]
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)
5. Give an example of when you would use REAL instead of INTEGER data type. [2 marks]
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!
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:
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.
- 17
- "Computer Science"
- FALSE
- 9.81
- 'Z'
- 2025-12-25
- 0
- ""
- -100
- TRUE
Solution:
- 17: INTEGER (could also be REAL as 17.0 or STRING as "17")
- "Computer Science": STRING
- FALSE: BOOLEAN
- 9.81: REAL (could be STRING as "9.81" but that's less common)
- 'Z': CHAR (could be STRING as "Z")
- 2025-12-25: DATE
- 0: INTEGER (in some systems could represent BOOLEAN FALSE)
- "": STRING (empty string)
- -100: INTEGER (negative whole number)
- 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
1. What is the difference between CHAR and STRING data types? [2 marks]
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)
2. Why would you choose REAL over INTEGER for a variable? [2 marks]
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%)
3. How are Boolean values sometimes represented numerically? [1 mark]
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
4. Write pseudocode to declare a variable called "Temperature" as a real number. [1 mark]
Answer
This tells the computer that Temperature will store decimal numbers like 23.5 or 36.6.
5. What is a primitive (atomic) data type? [2 marks]
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
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
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
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:
DECLARE Person : PersonType
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:
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 Person : ARRAY[1:100] OF PersonType
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:
Accessing Fields (Dot Notation)
Key Points:
- Use dot notation (RecordName.FieldName) to access fields
- Each field maintains its own data type (STRING, INTEGER, etc.)
- All fields are grouped under one identifier (the record variable name)
- For arrays of records: ArrayName[index].FieldName
Real-Life Example: Hospital Patient Records
A hospital system uses records to store patient information efficiently:
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 PatientNames : ARRAY[1:1000] OF STRING
DECLARE PatientDOBs : ARRAY[1:1000] OF DATE
// 7 separate arrays = hard to manage!
With records:
// 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:
DECLARE StudentID : INTEGER
DECLARE Name : STRING
DECLARE YearGroup : INTEGER
DECLARE AverageGrade : REAL
DECLARE IsBoarder : BOOLEAN
ENDTYPE
Answer the following questions:
- Write pseudocode to declare a variable called "Student1" of type StudentRecord.
- Write pseudocode to assign the following values to Student1:
- StudentID = 10472
- Name = "Alex Chen"
- YearGroup = 12
- AverageGrade = 87.5
- IsBoarder = TRUE
- Write pseudocode to output the student's name and average grade.
- Write pseudocode to declare an array called "ClassList" that can store 30 StudentRecord items.
- Write pseudocode to set the name of the first student in ClassList to "Maria Garcia".
Solution:
-
Declare Student1:
DECLARE Student1 : StudentRecord -
Assign values to Student1:
Student1.StudentID <- 10472
Student1.Name <- "Alex Chen"
Student1.YearGroup <- 12
Student1.AverageGrade <- 87.5
Student1.IsBoarder <- TRUE -
Output name and grade:
OUTPUT Student1.Name
OUTPUT Student1.AverageGrade -
Declare ClassList array:
DECLARE ClassList : ARRAY[1:30] OF StudentRecord -
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
1. What is a record data type and what are its advantages? [3 marks]
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
2. Write pseudocode to define a Book record type with title (STRING), author (STRING), pages (INTEGER), and available (BOOLEAN) fields. [3 marks]
Answer
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.
3. How do you access the "author" field of a Book record variable called "MyBook"? [1 mark]
Answer
This uses dot notation: record variable name, dot, field name.
4. What is the advantage of using an array of records instead of multiple parallel arrays? [2 marks]
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
5. Write pseudocode to declare an array of 50 Book records and set the title of the 10th book to "Computer Science". [3 marks]
Answer
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
1. Explain the importance of data types in programming languages. Include examples of at least three different data types and their typical uses. [6 marks]
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?)
2. Compare and contrast the CHAR and STRING data types. Give examples of when each would be used. [4 marks]
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.
3. Define a record data type called "CarRecord" with appropriate fields for storing car information. Then write pseudocode to declare a variable of this type and assign values to its fields. [6 marks]
Marking Scheme & Answer
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
4. Explain why an array of records is often better than using multiple parallel arrays. Use an example to illustrate your answer. [5 marks]
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.
5. For each of the following, choose the most appropriate data type and explain why: [6 marks]
a) Storing a person's age
b) Storing a product price
c) Storing whether a student has passed an exam
d) Storing a book title
e) Storing a single letter grade (A, B, C, etc.)
f) Storing a date of birth
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.
6. Write pseudocode to declare an array of 100 StudentRecord items (assuming StudentRecord is already defined) and then find and output the name of the student with the highest average grade. [8 marks]
Marking Scheme & Answer
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.
7. What is an "empty string" and why is it considered a special case of the STRING data type? [3 marks]
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 (" ").
8. Describe a real-world situation where using a record data type would be clearly beneficial over using separate variables. [4 marks]
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.
9. Why is STRING considered a "structured type" while CHAR is a primitive type? [2 marks]
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.
10. Explain the difference between primitive data types and composite data types. Give one example of each. [4 marks]
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.