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.

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.

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