0% found this document useful (0 votes)
59 views7 pages

Chapter 2: Fundamental Data Types

This document discusses fundamental data types in programming. It defines several standard primitive data types including integers, reals, Booleans, characters, and sets. Each data type determines a set of possible values and has a fixed type for results of operations. New enumeration types can also be defined by listing their distinct values. Standard operators are available for each data type.

Uploaded by

Carlton Afuhnwi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views7 pages

Chapter 2: Fundamental Data Types

This document discusses fundamental data types in programming. It defines several standard primitive data types including integers, reals, Booleans, characters, and sets. Each data type determines a set of possible values and has a fixed type for results of operations. New enumeration types can also be defined by listing their distinct values. Standard operators are available for each data type.

Uploaded by

Carlton Afuhnwi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Chapter

2: Fundamental data types


In mathematics it is customary to classify variables according to certain important
characteristics. Clear distinctions are made between real, complex, and logical variables or
between variables representing individual values, or sets of values, or sets of sets, or between
functions, functionals, sets of functions, and so on. This notion of classification is equally if not
more important in data processing. We will adhere to the principle that every constant, variable,
expression, or function is of a certain type. This type essentially characterizes the set of values
to which a constant belongs, or which can be assumed by a variable or expression, or which can
be generated by a function.
The primary characteristics of the concept of data type are as follows:
 A data type determines the set of values to which a constant belongs, or which may be
assumed by a variable or an expression, or which may be generated by an operator or a
function.
 The type of a value denoted by a constant, variable, or expression may be derived from
its form or its declaration without the necessity of executing the computational process.
 Each operator or function expects arguments of a fixed type and yields a result of a fixed
type. If an operator admits arguments of several types (e.g., + is used for addition of
both integers and real numbers), then the type of the result can be determined from
specific language rules.
Variables and data types are introduced in a program in order to be used for computation.
To this end, a set of operators must be available. For each standard data type, a programming
language offers a certain set of primitive, standard operators, and likewise with each structuring
method a distinct operation and notation for selecting a component. The task of composition of
operations is often considered the heart of the art of programming. However, it will become
evident that the appropriate composition of data is equally fundamental and essential.

I. Primitive Data Types


1. Standard Primitive Data Types
Standard primitive types are those types that are available on most computers as built-in
features. They include the whole numbers, the logical truth values, and a set of printable
characters. On many computers fractional numbers are also incorporated, together with the
standard arithmetic operations. We denote these types by the identifiers: INTEGER, REAL,
BOOLEAN, CHAR, SET.
a) Integers
The type INTEGER comprises a subset of the whole numbers whose size may vary among
individual computer systems. If a computer uses n bits to represent an integer in two’s
complement notation, then the admissible values x must satisfy −2n−1 ≤ x < 2n−1. It is assumed
that all operations on data of this type are exact and correspond to the ordinary laws of
arithmetic, and that the computation will be interrupted in the case of a result lying outside the
representable subset. This event is called overflow. The standard operators are the four basic
arithmetic operations of addition (+), subtraction (-), multiplication (*), and division (/, DIV).

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

b) The type REAL


The type REAL denotes a subset of the real numbers. Whereas arithmetic with operands of the
types INTEGER is assumed to yield exact results, arithmetic on values of type REAL is
permitted to be inaccurate within the limits of round-off errors caused by computation on a
finite number of digits. This is the principal reason for the explicit distinction between the types
INTEGER and REAL, as it is made in most programming languages. The standard operators
are the four basic arithmetic operations of addition (+), subtraction (-), multiplication (*), and
division (/). Many programming languages do not include an exponentiation operator. The
following is an algorithm for the fast computation of y = xn, where n is a non-negative integer.

c) The type BOOLEAN


The two values of the standard type BOOLEAN are denoted by the identifiers TRUE and
FALSE. The Boolean operators are the logical conjunction, disjunction, and negation whose
values are defined in Table 1.1. The logical conjunction is denoted by the symbol &, the logical
disjunction by OR, and negation by “˜”. Note that comparisons are operations yielding a result
of type BOOLEAN. Thus, the result of a comparison may be assigned to a variable, or it may
be used as an operand of a logical operator in a Boolean expression. For instance, given Boolean
variables p and q and integer variables x = 5, y = 8, z = 10, the two assignments
p:= x = y
q:= (x <= y) & (y < z)
yield p=FALSE and q=TRUE.
The Boolean operators & (AND) and OR have an additional property in most programming
languages, which distinguishes them from other dyadic operators. Whereas, for example, the
sum x+y is not defined, if either x or y is undefined, the conjunction p&q is defined even if q is
undefined, provided that p is FALSE. This conditionality is an important and useful property.
The exact definition of & and OR is therefore given by the following equations:
p & q = if p then q else FALSE
p OR q = if p then TRUE else q

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

d) The type CHAR


The standard type CHAR comprises a set of printable characters. Unfortunately, there is no
generally accepted standard character set used on all computer systems. Therefore, the use of
5
the predicate “standard” may in this case be almost misleading; it is to be understood in the
sense of “standard on the computer system on which a certain program is to be executed.” The
character set defined by the International Standards Organization (ISO), and particularly its
American version ASCII (American Standard Code for Information Interchange) is the most
widely accepted set. The ASCII set is therefore tabulated in Appendix A. It consists of 95
printable (graphic) characters and 33 control characters, the latter mainly being used in data
transmission and for the control of printing equipment.
In order to be able to design algorithms involving characters (i.e., values of type CHAR)
that are system independent, we should like to be able to assume certain minimal properties of
character sets, namely:
 The type CHAR contains the 26 capital Latin letters, the 26 lower-case letters, the 10
decimal digits, and a number of other graphic characters, such as punctuation marks.
 The subsets of letters and digits are ordered and contiguous, i.e.,
(“A” _ x) & (x _ “Z”) implies that x is a capital letter
(“a” _ x) & (x _ “z”) implies that x is a lower-case letter
(“0” _ x) & (x _ “9”) implies that x is a decimal digit
 The type CHAR contains a non-printing, blank character and a line-end character that
may be used as separators.
The availability of two standard type transfer functions between the types CHAR and
INTEGER is particularly important in the quest to write programs in a machine independent
form. We will call them ORD(ch), denoting the ordinal number of chin the character set, and
CHR(i), denoting the character with ordinal number i. Thus, CHR is the inverse function of
ORD, and vice versa, that is,
ORD(CHR(i)) = i (if CHR(i) is defined)
CHR(ORD(c)) = c
Furthermore, we postulate a standard function CAP(ch). Its value is defined as the capital
letter corresponding to ch, provided ch is a letter.
ch is a lower-case letter implies that CAP(ch) = corresponding capital letter
ch is a capital letter implies that CAP(ch) = ch

2. New Primitive Data types


A new, primitive type is definable by enumerating the distinct values belonging to it. Such
a type is called an enumeration type. Its definition has the form
TYPE T = (c1, c2, ... , cn)
T is the new type identifier, and the ci are the new constant identifiers.
Examples:
TYPE shape = (rectangle, square, ellipse, circle)
TYPE color = (red, yellow, green)
TYPE sex = (male, female)
TYPE weekday = (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday)
TYPE currency = (franc, mark, pound, dollar, shilling, lira, guilder, krone, ruble, cruzeiro, yen)
TYPE destination = (hell, purgatory, heaven)
TYPE vehicle = (train, bus, automobile, boat, airplane)
TYPE rank = (private, corporal, sergeant, lieutenant, captain, major, colonel, general)
6
TYPE object = (constant, type, variable, procedure,module)
TYPE structure = (array, record, set, sequence)
TYPE condition = (manual, unloaded, parity, skew)

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)

II. The type SET


The type SET denotes sets whose elements are integers in the range 0 to a small number,
typically 31 or 63. Given, for example, variables
VAR r, s, t: SET
possible assignments are
r:= {5}; s:= {x, y .. z}; t:= {}
Here, the value assigned to r is the singleton set consisting of the single element 5; to t is
assigned the empty set, and to s the elements x, y, y+1, ... , z-1, z.
The following elementary operators are defined on variables of type SET:
* set intersection
+ set union
- set difference
/ symmetric set difference
IN set membership
Constructing the intersection or the union of two sets is often called set multiplication or
7
set addition, respectively; the priorities of the set operators are defined accordingly, with the
intersection operator having priority over the union and difference operators, which in turn have
priority over the membership operator, which is classified as a relational operator. Following
are examples of set expressions and their fully parenthesized equivalents:
r * s + t = (r*s) + t
r - s * t = r - (s*t)
r - s + t = (r-s) + t
r + s / t = r + (s/t)
x IN s + t = x IN (s+t)

III. The Record Structure


The most general method to obtain structured types is to join elements of arbitrary types,
that are possibly themselves structured types, into a compound. Examples from mathematics
are complex numbers, composed of two real numbers, and coordinates of points, composed of
two or more numbers according to the dimensionality of the space spanned by the coordinate
system. An example from data processing is describing people by a few relevant characteristics,
such as their first and last names, their date of birth, sex, and marital status.
In mathematics such a compound type is the Cartesian product of its constituent types.
This stems from the fact that the set of values defined by this compound type consists of all
possible combinations of values, taken one from each set defined by each constituent type.
Thus, the number of such combinations, also called n-tuples, is the product of the number of
elements in each constituent set, that is, the cardinality of the compound type is the product of
the cardinalities of the constituent types.
In data processing, composite types, such as descriptions of persons or objects, usually
occur in files or data banks and record the relevant characteristics of a person or object. The
word record has therefore become widely accepted to describe a compound of data of this
nature, and we adopt this nomenclature in preference to the term Cartesian product. In general,
a record type T with components of the types T1,T2, ... , Tn is defined as follows:
TYPE T = RECORD s1:T1; s2:T2; ... sn:Tn END
card(T) = card(T1) * card(T2) * ... * card(Tn)
Examples
TYPE Complex = RECORD re, im: REAL END
TYPE Date = RECORD day, month, year: INTEGER END
TYPE Person = RECORD name, firstname: Name;
birthdate: Date;
sex: (male, female);
marstatus: (single, married, widowed, divorced)
END
We may visualize particular, record-structured values of, for example, the variables
z: Complex
d: Date
p: Person

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)

Figure 1 Records of type Complex, Date, and Person.

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

You might also like