Define and use non-composite types

Published by Patrick Mutisya · 14 days ago

Cambridge A‑Level Computer Science 9618 – 13.1 User‑defined Data Types

13.1 User‑defined Data Types

Learning Objective

Define and use non‑composite user‑defined types in a program.

What are Non‑composite Types?

Non‑composite types are data types that are not built from other types. In the Cambridge A‑Level syllabus they include:

  • Enumerated (enum) types
  • Subrange types
  • Type synonyms (type definitions)

1. Enumerated Types

An enumerated type is a set of named values. Each value is called an enumeration constant. The underlying representation is usually an integer, but the programmer works with the symbolic names.

Syntax (Pascal‑like)

type

Colour = (Red, Green, Blue);

In this example Colour can only take one of the three values Red, Green or Blue.

Common operations:

  • Assignment: c := Red;
  • Comparison: if c = Blue then …
  • Iteration (using succ / pred in Pascal): \$\text{next} = \text{succ}(\text{current})\$

2. Subrange Types

A subrange type restricts the values of an existing ordinal type (e.g., integer or char) to a contiguous subset.

Syntax

type

Score = 0..100; { integer subrange }

Grade = 'A'..'F'; { character subrange }

Variables of type Score can only hold values between 0 and 100 inclusive. Attempting to assign a value outside the range causes a run‑time error.

3. Type Synonyms (Type Definitions)

A type synonym creates a new name for an existing type. It does not create a new representation, but it improves code readability and can be used to enforce abstraction.

Syntax

type

StudentID = integer;

Temperature = real;

After the definition, StudentID and integer are interchangeable, but the name conveys intent.

Why Use Non‑composite User‑defined Types?

  • Clarity: Code reads like natural language (e.g., if status = Open then …).
  • Safety: The compiler can detect illegal values at compile‑time or run‑time.
  • Maintainability: Changing the underlying representation requires only the type definition.

Example Program

The following Pascal‑style program demonstrates all three non‑composite types.

program ExamResult;

type

Grade = (A, B, C, D, F); { enumerated }

Mark = 0..100; { subrange }

StudentID = integer; { synonym }

var

id : StudentID;

m : Mark;

g : Grade;

function CalculateGrade(m : Mark) : Grade;

begin

case m of

90..100: CalculateGrade := A;

80..89 : CalculateGrade := B;

70..79 : CalculateGrade := C;

60..69 : CalculateGrade := D;

else CalculateGrade := F;

end;

end;

begin

id := 12345;

m := 78;

g := CalculateGrade(m);

writeln('Student ', id, ' scored ', m, ' and received grade ', g);

end.

Summary Table

TypePurposeTypical SyntaxExample \cdot alues
EnumeratedDefine a fixed set of named constantstype Name = (Const1, Const2, …);Colour = (Red, Green, Blue)
SubrangeRestrict an ordinal type to a contiguous intervaltype Name = low..high;Score = 0..100
Type synonymGive a meaningful name to an existing typetype NewName = ExistingType;StudentID = integer

Common Pitfalls

  1. Assuming an enumerated type can be compared with integers directly – they must be compared using the enumeration constants.
  2. Assigning a value outside a subrange; this triggers a run‑time range‑check error if enabled.
  3. Confusing a type synonym with a distinct type – they are interchangeable, so type‑checking does not catch mismatches.

Practice Questions

  1. Define an enumerated type Day for the seven days of the week. Write a procedure that prints “Weekend” if the day is Saturday or Sunday, otherwise prints “Weekday”.
  2. Create a subrange type Age that allows values from 0 to 120. Write a function that returns true if a given Age is a legal voting age (≥18).
  3. Explain the difference between type Money = real; and type Money = 0..1000; in terms of allowed values and program safety.

Suggested diagram: A flowchart showing how a value moves from input → subrange check → processing → output.