Data types and records

Computer Science AS · Data types and structures. The idea in plain words, the rules to remember, the mistake to avoid and a worked example, written and checked by Mega Lecture.

Test this topic freeAll Computer Science AS topics

The idea

Every item of data has a data type. The type decides which values the item can hold and what can be done with it. INTEGER holds whole numbers, REAL holds numbers with a fractional part, CHAR holds one character, STRING holds text, BOOLEAN holds TRUE or FALSE and DATE holds a date.

Choose the type by how the data is used. A telephone number is a STRING: it is never used in arithmetic and its leading zero must be kept.

A record groups several fields about one thing under one identifier. The fields can have different data types, which an array does not allow. A field is reached with dot notation, for example Student1.Age.

In simpler words

Every piece of data has a type. The type says what values it can hold. INTEGER is a whole number. REAL has a decimal part. CHAR is one letter. STRING is text. BOOLEAN is TRUE or FALSE. Pick the type by how the data is used. A phone number is a STRING. A record groups several fields about one thing, and they can be different types.

An everyday comparison

A record type is like a blank form with boxes for name, age and so on. Each filled-in form is a variable, holding real answers. You read an answer by naming the form and the box, such as Student1.Age. The comparison stops working because each box in a record has a fixed data type, so you cannot write text in the INTEGER box, while a paper form accepts anything.

Rules to remember

Common mistake

Writing the type name before the dot, as in StudentRecord.Age ← 17. The type is only a template; the data is held in the variable, so write Student1.Age ← 17.

In the exam

When choosing a data type, name it and give a reason tied to how the data is used, e.g. telephone number as STRING because it is not used in arithmetic and the leading zero must be kept. To define a record, write TYPE name, a DECLARE line for each field with its type, then ENDTYPE. Declare a variable of the type, then access fields with dot notation on the variable, not the type name. For an array of records, write Group[3].Name.

Worked example

A record type CarRecord has the fields Reg : STRING, Seats : INTEGER and Electric : BOOLEAN. An array is declared with DECLARE Garage : ARRAY[1:50] OF CarRecord. Which statement stores 7 seats for the second car?

  1. Garage is an array, so first choose the element with an index: Garage[2].
  2. That element is one whole record, so add a dot and the field name: Garage[2].Seats.
  3. Seats is an INTEGER, so assign the whole number 7 with no quotes.

Answer: Garage[2].Seats ← 7

Ask about this

Type what you are stuck on. The helper finds the explanation, worked examples and the notes to read, free and instant.

Keep going

← AlgorithmsArrays →Take a Computer Science AS test