Chapter 2: Fundamental Data Types
Chapter 2: Fundamental Data Types
4
Whereas the slash denotes ordinary division resulting in a value of type REAL, the operator
DIV denotes integer division resulting in a value of type INTEGER. If we define the quotient
q = m DIV n, and the remainder r = m MOD n, the following relations hold, assuming n > 0:
q * n + r = m and 0 ≤ r < n.
Examples:
31 DIV 10 = 3 31 MOD 10 = 1
−31 DIV 10 = −4 −31 MOD 10 = 9
P Q P&Q P OR Q ¬P
TRUE TRUE TRUE TRUE FALSE
TRUE FALSE FALSE TRUE FALSE
FALSE TRUE FALSE TRUE TRUE
FALSE FALSE FALSE FALSE TRUE
The definition of such types introduces not only a new type identifier, but at the same time the
set of identifiers denoting the values of the new type. These identifiers may then be used as
constants throughout the program, and they enhance its understandability considerably. If, as
an example, we introduce variables s, d, r, and b.
VAR s: sex
VAR d: weekday
VAR r: rank
then the following assignment statements are possible:
s:= male
d:= Sunday
r:= major
b:= TRUE
Evidently, they are considerably more informative than their counterparts
s:= 1 d:= 7 r:= 6 b:= 2
Which are based on the assumption that c, d, r, and b are defined as integers and that the
constants are mapped onto the natural numbers in the order of their enumeration. Furthermore,
a compiler can check against the inconsistent use of operators. For example, given the
declaration of s above, the statement s:= s+1 would be meaningless.
If, however, we recall that enumerations are ordered, then it is sensible to introduce operators
that generate the successor and predecessor of their argument. We therefore postulate the
following standard operators, which assign to their argument its successor and predecessor
respectively: INC(x) DEC(x)
8
as shown in the figure below, the identifiers s1, s2, ..., sn introduced by a record type definition
are the names given to the individual components of variables of that type. As components of
records are called fields, the names are field identifiers. They are used in record selectors applied
to record structured variables. Given a variable x: T, its i-th field is denoted by x.si. Selective
updating of x is achieved by using the same selector denotation on the left side in an assignment
statement:
x.si:= e
where e is a value (expression) of type Ti. Given, for example, the record variables z, d, and p
declared above, the following are selectors of components:
z.im (of type REAL)
d.month (of type INTEGER)
p.name (of type Name)
p.birthdate (of type Date)
p.birthdate.day (of type INTEGER)
The example of the type Person shows that a constituent of a record type may itself be
structured. Thus, selectors may be concatenated. Naturally, different structuring types may also
be used in a nested fashion. For example, the i-th component of an array being a component of
a record variable r is denoted by r.a[i], and the component with the selector name s of the i-th
record structured component of the array a is denoted by a[i].s.
It is a characteristic of the Cartesian product that it contains all combinations of elements
of the constituent types. But it must be noted that in practical applications not all of them may
be meaningful. For instance, the type Date as defined above includes the 31st April as well as
the 29th February 1985, which are both dates that never occurred. Thus, the definition of this
type does not mirror the actual situation entirely correctly; but it is close enough for practical
purposes, and it is the responsibility of the programmer to ensure that meaningless values never
occur during the execution of a program.
The following short excerpt from a program shows the use of record variables. Its
purpose is to count the number of persons represented by the array variable family that are both
female and single:
VAR count: INTEGER;
family: ARRAY N OF Person;
count := 0;
FOR i := 0 TO N-1 DO
9
IF (family[i].sex = female) & (family[i].marstatus= single) THEN
INC(count) END
END
The record structure and the array structure have the common property that both are
random access structures. The record is more general in the sense that there is no requirement
that all constituent types must be identical. In turn, the array offers greater flexibility by
allowing its component selectors to be computable values (expressions), whereas the selectors
of record components are field identifiers declared in the record type definition.
10