AS Level · Topic 10.1
A-Level Computer Science 9618 / Topic 10 / AS

The right type for the job.

Every item of data in a program has a data type that fixes what values it can hold and what you can do with it. When several items of different types describe one thing, a record keeps them together under a single identifier.

The key idea

A data type decides which values a variable can store and which operations are allowed on it. 9618 pseudocode uses INTEGER, REAL, CHAR, STRING, BOOLEAN and DATE. A record is a composite (user-defined) data type that holds a set of related data items, called fields, which can be of different data types, all under one identifier. It is defined with TYPE ... ENDTYPE and each field is accessed with dot notation, such as Pupil1.YearGroup.

one identifier, five fields, four different data types Pupil1 : StudentRecord .LastName.YearGroup.FormGroup .DateOfBirth.Boarder "Khan"12'B' 14/03/2009FALSE STRINGINTEGERCHAR DATEBOOLEAN
Fig. 1: a record variable groups fields of different data types under one identifier
Section 01

Choosing a data type.

Pick the type from what the data is and what the program must do with it. A value that is never used in arithmetic is often better stored as a string, even if it is made of digits.

TypeHoldsLiteralTypical use
INTEGERa whole number5, −3number of tickets, age in years
REALa number with a fractional part4.7, 0.0 (a digit on each side of the point)price, temperature, average
CHARexactly one character'B' (single quotes)grade, menu choice, one-letter code
STRINGzero or more characters"Khan", "" (double quotes)name, postcode, phone number
BOOLEANone of two logical valuesTRUE, FALSEflags such as Found, IsMember
DATEa valid calendar date14/03/2009 (state the format: dd/mm/yyyy)date of birth, due date
DECLARE TicketsSold : INTEGER
DECLARE Price : REAL
DECLARE Grade : CHAR
DECLARE Phone : STRING
DECLARE IsMember : BOOLEAN
DECLARE DueDate : DATE
Worked example Justify each choice
A library system stores, for each book: a book ID such as B0457, the number of copies, the replacement cost in dollars, the date it is next due back, a category code of one letter and whether it is on loan. Choose a data type for each item.
  1. Book ID: it mixes a letter and digits, so STRING.
  2. Number of copies: always a whole number, so INTEGER.
  3. Replacement cost, such as 12.50: needs a fractional part, so REAL.
  4. Due date: DATE. Category code: one letter, so CHAR. On loan: yes or no, so BOOLEAN.
Answer: STRING, INTEGER, REAL, DATE, CHAR, BOOLEAN
Your turn
A telephone number such as 07700900123 is to be stored. Should it be INTEGER or STRING? Give a reason.
Examiner trap

"Phone number: INTEGER because it is a number" loses the mark. Digits that are an identifier, not a quantity, belong in a STRING.

Section 02

Defining a record.

A record type is a template. Defining it creates no data; it tells the program which fields every variable of that type will have and the data type of each one.

TYPE BookRecord
    DECLARE BookID : STRING
    DECLARE Title : STRING
    DECLARE Copies : INTEGER
    DECLARE Cost : REAL
    DECLARE DueDate : DATE
    DECLARE OnLoan : BOOLEAN
ENDTYPE

DECLARE ThisBook : BookRecord
Examiner trap

TYPE ... ENDTYPE only defines the type. You still need DECLARE ThisBook : BookRecord before any data can be stored. Writing BookRecord.Title ← "Dune" uses the type name, not a variable, and earns no mark.

Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

Which data type should store the average of a set of test marks?

Quick check+10 XP

What is the purpose of a record structure?

Quick check+10 XP

StudentRecord has a field Surname. Pupil1 is declared as StudentRecord. Which statement is correct?

Quick check+10 XP

Which literal is a valid REAL in 9618 pseudocode?

Section 03

Reading and writing records.

Use dot notation to reach a field: the variable name, a full stop, then the field name. A whole record can be copied in one assignment if both variables have the same type, and an array of records stores many of them.

ThisBook.BookID ← "B0457"
ThisBook.Title ← "Dune"
ThisBook.Copies ← 3
ThisBook.Cost ← 12.50
ThisBook.DueDate ← 21/11/2026
ThisBook.OnLoan ← TRUE
OUTPUT ThisBook.Title, " has ", ThisBook.Copies, " copies"

DECLARE Library : ARRAY[1:500] OF BookRecord
Library[1] ← ThisBook
Worked example Process an array of records
Library is an ARRAY[1:500] OF BookRecord. Write pseudocode to output the title of every book that is on loan, followed by the number of books on loan.
  1. Declare and initialise a counter: Count ← 0.
  2. Loop through every element: FOR Index ← 1 TO 500.
  3. Test one field of the record in that element: IF Library[Index].OnLoan = TRUE THEN.
  4. Inside the IF: OUTPUT Library[Index].Title and Count ← Count + 1; after NEXT Index, output Count.
Answer:
DECLARE Count : INTEGER
DECLARE Index : INTEGER
Count ← 0
FOR Index ← 1 TO 500
    IF Library[Index].OnLoan = TRUE THEN
        OUTPUT Library[Index].Title
        Count ← Count + 1
    ENDIF
NEXT Index
OUTPUT Count, " books on loan"
Your turn
Write one statement that adds 1 to the number of copies of the book stored in element 12 of Library.
Examiner trap

The index belongs to the array, not the field: Library[12].Copies, never Library.Copies[12].

Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to AS Paper 1 and Paper 2 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

A program stores a member's postcode, such as CB2 1EW. Which data type is most appropriate?

Exam style+20 XP

Which line correctly declares an array that can hold 40 records of type PlayerRecord?

Exam style+20 XP

Which statement about a record type is true?

Exam style+20 XP

Readings is an ARRAY[1:24] OF SensorRecord with a REAL field Temp. Which expression gives the temperature of the 5th reading?

Skill unlocked

Data types and records, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
Choosing data types for a problem, defining record types with TYPE ... ENDTYPE, and reading and writing fields of records and arrays of records.