📑 Contents

Chapter 10.1: Data Types & Records

9618 AS Computer Science

📚 Learning Objectives
🌟 Did You Know?

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!

📖 Prior Knowledge Required

1. What Are Data Types?

A data type is a classification of data into groups according to the kind of data they represent. Computers use different data types to represent different types of data in a program.

📖 Definition

Data types allow programming languages to provide different classifications for items of data, so they can be used for different purposes. The data type determines:

1.1 Why Are Data Types Important?

It is important to choose the correct data type for a given situation to ensure accuracy and efficiency in the program.

📝 Declaring Variables with Data Types

In pseudocode and some other programming languages, data types must be declared before they can be used. Each data item is given a unique name called an identifier.

DECLARE <identifier> : <data type>

Example: DECLARE Age : INTEGER

DATA TYPES Memory Size Valid Operations Allowed Values Interpretation

2. 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.

Data Type Description Examples
INTEGER Whole numbers (positive or negative) 10, -5, 0, 42, -100
REAL Numbers with fractional part (positive or negative) 3.14, -2.5, 0.0, 99.99
CHAR A single character (letter, digit, or symbol) 'a', 'B', '5', '$', '@'
STRING A sequence of alphanumeric characters "Hello World", "1234", "CS@9618"
BOOLEAN Logical values - true or false only TRUE, FALSE
DATE Stores calendar dates DD/MM/YY, "24/04/2025"
💡 Exam Tip

Remember: CHAR uses single quotes ('A') while STRING uses double quotes ("Hello"). A string is known as a structured type because it is a sequence of characters. A special case is the empty string: a value of data type string, but with no characters stored in it (written as "").

PRIMITIVE DATA TYPES INTEGER REAL CHAR STRING BOOLEAN DATE 42, -7 3.14 'A' "Hi" TRUE 25/12/25

3. Declaring Variables in Pseudocode

Before data can be used, the data type needs to be decided. This is done by declaring the data type for each item to be used.

📝 Declaration Syntax
DECLARE <identifier> : <data type>
Data Type Pseudocode Example
INTEGER
DECLARE Age : INTEGER Age ← 10
REAL
DECLARE Temp : REAL Temp ← 3.14
CHAR
DECLARE Grade : CHAR Grade ← 'A'
STRING
DECLARE Name : STRING Name ← "Hello"
BOOLEAN
DECLARE LoggedIn : BOOLEAN LoggedIn ← TRUE
DATE
DECLARE DOB : DATE DOB ← "24/04/2025"
⚠️ Important

When assigning values:

4. Record Data Types

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

📖 What is a Record?

4.1 Advantages of Record Data Types

🌟 Key Advantage

A set of data related to one thing of different types is held under a single identifier. This makes code more organized and easier to maintain!

RECORD: PersonType Name : STRING DateOfBirth : DATE Height : REAL NumberOfSiblings : INTEGER IsFullTimeStudent : BOOLEAN FIELDS

5. Defining Records in Pseudocode

📝 Record Definition Syntax
TYPE <TypeName> DECLARE <identifier> : <data type> DECLARE <identifier> : <data type> DECLARE <identifier> : <data type> ... ENDTYPE

5.1 Example: Employee Record

Record for employee data:
TYPE EmployeeRecord DECLARE EmployeeFirstName : STRING DECLARE EmployeeFamilyName : STRING DECLARE DateEmployed : DATE DECLARE Salary : CURRENCY ENDTYPE

5.2 Example: Person Record

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

Records in programming are a type of data structure used to group related data in your code. These are not the same as records in a database, which refer to a complete set of fields on a single entity in a table (row)!

6. Using Records - Declaring & Assigning

6.1 Declaring a Record Variable

After defining the record type, you can declare a variable of that type:

DECLARE Person : PersonType

6.2 Assigning Values to Fields

Use the dot notation to access individual fields:

Person.Name ← "Fred" Person.NumberOfSiblings ← 3 Person.IsFullTimeStudent ← TRUE Person.Height ← 1.75 Person.DateOfBirth ← "15/03/2005"

6.3 Reading from Records

To output a field of a record:

OUTPUT Person.Name
📝 Accessing Fields

The format is: RecordName.FieldName

Person . Name "Fred" Record Name Field Name Value

7. Arrays of Records

We can use arrays of records to store multiple records of the same type. This is useful when we have several entities' data to work with and do not want to use a separate 1D array for each field.

7.1 Declaring an Array of Records

Using the Person record type, we can declare an array for 100 person records:

DECLARE Person : ARRAY[1:100] OF PersonType

7.2 Accessing Individual Records

We can access an individual's data using the array index combined with dot notation:

Person[1].Name ← "Fred" Person[1].NumberOfSiblings ← 3 Person[1].IsFullTimeStudent ← TRUE OUTPUT Person[1].Name
⚠️ Important: Array Index + Field Access

The format is: RecordArray[Index].FieldName

Person Array: [1] Name: "Fred" Height: 1.75 DOB: "15/03/05" ... [2] Name: "Mary" Height: 1.62 DOB: "22/07/04" ... [3] Name: "Ahmed" Height: 1.80 DOB: "08/11/05" ... ... [100] Name: "Zara" Height: 1.68 DOB: "19/02/05" ... [ ]

8. Key Features of Records

Feature Explanation
Can store different data types Unlike arrays, records are not limited to a single type
Fields are named Each item in the record has a meaningful identifier
Structured storage Easier to manage complex data about real-world entities
Single identifier All related data is grouped under one name
Fixed structure The number and type of fields are defined once

8.1 Records vs Arrays

Aspect Array Record
Data Types Same type only Different types allowed
Access Method Index number Field name
Size Can be dynamic Fixed number of fields
Use Case Lists of same-type items Related data of different types
🧠 Memory Trick

RECORD = Related Elements Collected Organized under Record Definition

Think of a record like a form you fill out - each field has a name and can hold different types of information!

ARRAY (Same type) 42 17 89 33 [0] [1] [2] [3] RECORD (Mixed types) Name: "Ali" Age: 16 Height: 1.72 Active: TRUE Grade: 'A' DOB: "01/05/08"

9. More Record Examples

9.1 Car Record Example

TYPE Car DECLARE Make : STRING DECLARE Model : STRING DECLARE Colour : STRING DECLARE Price : REAL DECLARE DateOfRegistration : DATE ENDTYPE DECLARE MyCar : Car MyCar.Make ← "Toyota" MyCar.Model ← "Yaris" MyCar.Colour ← "Red" MyCar.Price ← 14995.99 MyCar.DateOfRegistration ← "12/03/2022" OUTPUT "Make: ", MyCar.Make OUTPUT "Model: ", MyCar.Model OUTPUT "Colour: ", MyCar.Colour OUTPUT "Price: £", MyCar.Price

9.2 Book Record Example

TYPE TBookRecord DECLARE Title : STRING DECLARE Author : STRING DECLARE ISBN : STRING DECLARE Price : REAL DECLARE InStock : BOOLEAN ENDTYPE DECLARE Book1 : TBookRecord Book1.Author ← "EMK" Book1.Title ← "CS Made Easy" Book1.Price ← 24.99 Book1.InStock ← TRUE
💡 Exam Tip

When asked to define a record type, always include:

  1. The TYPE ... ENDTYPE structure
  2. Appropriate field names (meaningful identifiers)
  3. Correct data types for each field

10. Exam-Style Questions (Part 1)

1. State the most appropriate data type for each of the following: (a) A person's age, (b) The price of an item, (c) Whether a user is logged in, (d) A customer's name. [4 marks]

Answer:

  • (a) INTEGER - Age is a whole number
  • (b) REAL - Prices can have decimal places (e.g., 19.99)
  • (c) BOOLEAN - Only two states: logged in or not (TRUE/FALSE)
  • (d) STRING - Names are sequences of characters

Additional points for deeper understanding:

  • CURRENCY could also be used for price in some languages
  • DATE would be used if storing date of birth instead of age
2. Explain the difference between a CHAR data type and a STRING data type. Give an example of each. [4 marks]

Answer:

  • CHAR stores a single character only, enclosed in single quotes
  • STRING stores a sequence of zero or more characters, enclosed in double quotes
  • CHAR example: 'A', '5', '@'
  • STRING example: "Hello", "CS9618", "" (empty string)

Additional points for deeper understanding:

  • A STRING is considered a structured type because it is a sequence of characters
  • CHAR takes less memory than a STRING containing one character
  • The empty string "" is a valid STRING with zero characters
3. Define a record type called StudentRecord that stores: student name, student ID, exam score, and whether the student has passed. [5 marks]

Answer:

TYPE StudentRecord DECLARE StudentName : STRING DECLARE StudentID : STRING DECLARE ExamScore : REAL DECLARE HasPassed : BOOLEAN ENDTYPE

Additional points for deeper understanding:

  • StudentID could be STRING (e.g., "S12345") rather than INTEGER if it contains letters
  • ExamScore could be INTEGER if only whole marks are used
  • Alternative field names: Name, ID, Score, Passed - as long as they're meaningful
4. Write pseudocode to declare a variable called MyClass of type StudentRecord (using the record type from Q3), then assign values to all fields and output the student name. [4 marks]

Answer:

DECLARE MyClass : StudentRecord MyClass.StudentName ← "John Smith" MyClass.StudentID ← "S12345" MyClass.ExamScore ← 78.5 MyClass.HasPassed ← TRUE OUTPUT MyClass.StudentName

Additional points for deeper understanding:

  • The DECLARE statement creates a variable of the record type
  • Dot notation (.) is used to access individual fields
  • The ← symbol (or =) is used for assignment
5. State two advantages of using a record data type instead of separate variables. [2 marks]

Answer:

  • All related data is stored under one identifier, making code more organized
  • Easier to manage complex data about real-world entities

Additional points for deeper understanding:

  • Fields have meaningful names, improving code readability
  • Can pass all related data as a single parameter to procedures/functions
  • Can create arrays of records to store multiple related items
  • Reduces the risk of mixing up data from different entities

10. Exam-Style Questions (Part 2)

6. A school stores information about books in a library. Write pseudocode to define a record type called BookType with fields for: title, author, ISBN, number of pages, and whether it is available. [5 marks]

Answer:

TYPE BookType DECLARE Title : STRING DECLARE Author : STRING DECLARE ISBN : STRING DECLARE NumberOfPages : INTEGER DECLARE IsAvailable : BOOLEAN ENDTYPE

Additional points for deeper understanding:

  • ISBN is STRING because it may contain hyphens (e.g., "978-0-123456-78-9")
  • NumberOfPages is INTEGER because pages are whole numbers
  • IsAvailable is BOOLEAN - either available (TRUE) or not (FALSE)
7. Using the BookType from Question 6, write pseudocode to: (a) Declare an array that can store 500 books, (b) Assign values to the first book in the array. [5 marks]

Answer:

DECLARE Library : ARRAY[1:500] OF BookType Library[1].Title ← "Computer Science" Library[1].Author ← "S. Smith" Library[1].ISBN ← "978-0-123456-78-9" Library[1].NumberOfPages ← 350 Library[1].IsAvailable ← TRUE

Additional points for deeper understanding:

  • The array index goes in square brackets [ ]
  • Use dot notation after the index to access fields
  • Array could also be declared as ARRAY[0:499] depending on language
8. Compare arrays and records. Give one similarity and two differences. [4 marks]

Answer:

Similarity:

  • Both are data structures that store multiple pieces of data under a single identifier

Differences:

  • Arrays store data of the same type; records can store different types
  • Array elements are accessed by index number; record fields are accessed by field name
  • Arrays can have dynamic size; records have a fixed number of fields
9. Explain why the correct choice of data type is important when writing a program. [4 marks]

Answer:

  • Ensures accuracy - correct data type prevents invalid operations (e.g., arithmetic on strings)
  • Ensures efficiency - appropriate types use memory efficiently
  • Determines what values can be stored (e.g., BOOLEAN can only be TRUE or FALSE)
  • Determines what operations can be performed (e.g., arithmetic on numbers, concatenation on strings)

Additional points for deeper understanding:

  • Prevents runtime errors from type mismatches
  • Makes code more readable and maintainable
  • Helps compiler/interpreter optimize memory usage
10. Write pseudocode that uses a loop to output the names of all students in an array of 30 StudentRecord variables. Assume the array is called Students. [4 marks]

Answer:

FOR Index ← 1 TO 30 OUTPUT Students[Index].StudentName NEXT Index

Additional points for deeper understanding:

  • Alternative: FOR Index ← 0 TO 29 (depends on array declaration)
  • The loop variable (Index) is used as the array index
  • Dot notation accesses the StudentName field of each record
  • WHILE loop could also be used with a counter variable

11. Glossary

Term Definition
Data Type A classification of data into groups according to the kind of data they represent
Primitive Data Type Fundamental data types built into a programming language (e.g., INTEGER, REAL, CHAR, STRING, BOOLEAN, DATE)
Atomic Data Type Another name for primitive data types - basic, indivisible data types
INTEGER A data type for whole numbers, positive or negative (e.g., 42, -7, 0)
REAL A data type for numbers with a fractional part (e.g., 3.14, -2.5)
CHAR A data type for a single character, enclosed in single quotes (e.g., 'A', '5')
STRING A structured data type for a sequence of characters, enclosed in double quotes (e.g., "Hello")
BOOLEAN A data type that can only have two values: TRUE or FALSE
DATE A data type for storing calendar dates (e.g., DD/MM/YYYY)
Identifier A unique name given to a variable, constant, or other programming element
Record A composite data structure containing a fixed number of fields of different data types
Field A single item within a record, with its own name and data type
Composite Data Type A data type composed of multiple primitive data types (e.g., records)
Dot Notation The use of a dot (.) to access fields within a record (e.g., Person.Name)
Array of Records An array where each element is a record of a defined type

12. Exam Success Tips (Part 1)

💡 Data Type Selection - Key Reminders
💡 Quote Rules - Don't Get Confused!
💡 Record Definition Checklist

When asked to define a record, make sure you include:

  1. TYPE ... ENDTYPE structure (must have both!)
  2. Meaningful field names (not just "field1", "field2")
  3. Correct data types for each field
  4. DECLARE keyword for each field
💡 Accessing Record Fields
🧠 Memory Trick: Record Definition Template
TYPE TypeName DECLARE Field1 : DataType1 DECLARE Field2 : DataType2 ... ENDTYPE

Remember: TYPE starts it, ENDTYPE ends it, DECLARE for each field!

12. Exam Success Tips (Part 2)

⚠️ Common Mistakes to Avoid
❌ Wrong vs ✓ Right
❌ Wrong ✓ Right
Name ← "Ali" DECLARE Name : STRING
Name ← "Ali"
Grade ← A Grade ← 'A'
Active ← "TRUE" Active ← TRUE
Person[Name] ← "Ali" Person.Name ← "Ali"
💡 Answer Strategy Tips

13. Key Takeaways

📌 Primitive Data Types
📌 Record Data Types
📌 Declaration Syntax
// Variable declaration DECLARE Identifier : DataType // Record definition TYPE TypeName DECLARE Field : DataType ENDTYPE // Array of records DECLARE ArrayName : ARRAY[1:n] OF TypeName
🌟 Quick Reference
Topic Key Point
Data Types Determine what values can be stored and what operations are allowed
CHAR vs STRING CHAR = single character ('A'), STRING = sequence ("Hello")
Record Access RecordName.FieldName for single record, ArrayName[Index].FieldName for arrays
Record Advantage Groups related data of different types under one identifier