DATA TYPES IN PASCAL
Pascal supports two major types of data: simple vs. structured.
Within these types there are scalar and ordinal kinds of data.
Essentially, simple data types cannot be further broken down into
anything finer. The data of this type which we have used are
INTEGER, REAL, BOOLEAN and CHAR. These four types of data are
also called the Pascal standard data types. These four types are
also scalar data, since the relational characters < > = <> <= and
>= are all valid between values of any data of these types. In
addition, data of types INTEGER, BOOLEAN and CHAR are also
ordinal data. This means that every value of a given type
(except the last) has a unique value which comes after it (called
its successor) and every value (except the first) has a unique
value which comes before it (called its predecessor). Data of
type REAL however, is not ordinal, since finding the "next"
value after a real number value is determined by the number of
decimal digits the computer can store.
INTEGER: -32768, ..., -1, 0, 1, 2, 3, ... 32767 (= MAXINT)
LONGINT: -2147483648, ... -1, 0, 1, 2, ... 2147483647
BOOLEAN: FALSE, TRUE
CHAR: space < '!' < ... < '0' < '1' < ... < '9' < ':' <
';' < '<' < '=' < '>' < '?' < '@' < 'A' < 'B' <
... < 'Z' < ... < 'a' < 'b' < ... < 'z'
Ordinal data types support the PRED and SUCC functions
(Predecessor and Successor). The ORD function returns the
(integer) number of the position of the data in the list of
values of its type. It is especially useful with CHAR data. If
n is an integer (usually in the range 32..122), the function
CHR(n) returns the character with ASCII value n (its position in
the character sequence). Examples:
ORD('A') = 65
ORD('a') = 97
ORD('0') = 48
ORD('9') = 57
CHR(65) = 'A'
CHR(122) = 'z'
CHR(63) = '?'
CHR(32) = ' '
PRED('B') = 'A'
PRED(TRUE) = FALSE
PRED(8) = 7
PRED('!') = ' '
SUCC('C') = 'D'
SUCC('w') = 'x'
SUCC(12) = 13
SUCC(FALSE) = TRUE
ORD('9') - ORD('0') = 9
Summary:
BOOLEAN Values: FALSE < TRUE
Operations: NOT, AND, OR, assignment
Predefined functions: PRED, SUCC, ORD
Predefined procedures: WRITE, WRITELN
CHAR Values: as above
Operations: assignment, comparison with relational characters
Predefined functions: PRED, SUCC, ORD
Predefined procedures: READ, READLN, WRITE, WRITELN
INTEGER Values: as above
Operations: +, -, *, DIV, MOD, assignment, comparison
with relationals
Predefined functions: PRED, SUCC, ORD, ABS, SQR, SQRT,
ODD, CHR
Predefined procedures: READ, READLN, WRITE, WRITELN
REAL Values: ratios of integers (essentially numbers in
range ñ1 E ñ38)
Operations: +, -, *, /, assignment, comparison with relationals
Predefined functions: ABS, SQR, SQRT, ROUND, TRUNC
Predefined procedures: READ, READLN, WRITE, WRITELN
SUBRANGE TYPES
Identifiers can be defined so that they have a restricted range
of values of a given ordinal type. Using subranges can help
avoid "out-of-range" errors, especially in controlling loops.
The syntax is: Identifier: First value..Last value; These
constructs are called subrange types. Examples:
Var
Num: -10..19 { Num has integer values -10 to 19 inclusive }
Alphabet: 'A'..'Z' { Alphabet is char with values 'A' to 'Z' }
USER DEFINED TYPES
In Pascal, a programmer can define his/her own Constants (in a
Const section) and similarly his/her own Variables (in a Var
section). A programmer can also define an identifier to be a
data type -- thus a programmer has some freedom to define new
data types. These data types are called user defined types.
Such definitions are placed in a new section of the program, the
TYPE section. This section is found between CONST and VAR
sections using the reserved word TYPE.
One way to declare a type is to use a subrange. For example:
Const Last = 50;
Type Valid_numbers = 1..Last;
Var X, Y: Valid_numbers;
Another way to create a data type is to list the values of the
type in parentheses using programmer-created identifiers (the use
of previously defined identifiers is illegal because of conflicts
with the ORD function, among others). Such data types are called
enumerated types. For example,
Type
Work_days = (Monday, Tuesday, Wednesday, Thursday, Friday);
Week_end = (Saturday, Sunday);
Var
School_days: Work_days;
Free_days: Week_end;
Worst_days: Monday..Thursday; { user defined subrange type }
Note: ORD(Monday) = 0; SUCC(Saturday) = Sunday; SUCC(Friday) is
an error; PRED(Monday) is an error; School_days := Thursday is
valid, but Read(School_days) is invalid as well as
Writeln(School_days). Values in a user-defined enumerated type
are not compatible with the READ and WRITE procedures in Pascal.
School_days := 'Monday' will yield a compiler error (Data type
mismatch). Use the following as an example of how to integrate
the use of enumerated types:
For School_days := Monday to Wednesday Do
Case School_days of
Monday: Writeln('Monday');
Tuesday: Writeln('Tuesday');
Wednesday; Writeln('Wednesday')
End; { Case }
The major reason for having a user-defined TYPE section involves
passing information of various kinds to procedures and functions.
A subrange listing or an enumerated type cannot be used in a
procedure or function heading (but more importantly, as we will
see, neither can strings, an important class of structured data
types). Thus
Procedure Test(X: 1..20); is illegal, but
Type
Legal_range: 1..20;
Procedure Test(X: Legal_range); is legal.
Note: the following constructions are illegal:
Type Small_nums = (1, 2, 3); { integers are predefined values }
Vowels = ('a', 'e', 'i', 'o', 'u');
{ same reason as above, plus values in a subrange cannot
"jump around" -- they must be contiguous in their range }