[<<Previous Entry]
[^^Up^^]
[Next Entry>>]
[Menu]
[About The Guide]
enumerated User-defined Data Type Ordinal Type
Standard Pascal allows you to define your own enumerated data types.
An enumerated data type (EDT) consists of a list of unique
identifiers, separated by commas and enclosed by parentheses. Each
identifier has an ordinal value one greater than its predecessor and
one less than its successor; the first identifier has an ordinal value
of 0. Having defined an EDT, you can then declare variables,
parameters, and functions to be of that type. Consider the following
simple example:
type
Months = (January,February,March,April,May,June,July,August,
September,October,November,December);
var
Current : Months;
begin
for Current := January to December do
Writeln(Ord(Current))
end.
This will write out the ordinal values of January through December,
which are 0..11.
Assume that E is a variable of some EDT. The function Ord(E) returns
the ordinal value of E; the function Succ(E) returns the successor of
E; and the function Pred(E) returns the predecessor of E. If E is the
first value in an EDT, then Pred(E) is undefined; likewise, if E is
the last value, then Succ(E) is undefined.
You cannot write out or read in EDT values or variables in interactive
or text I/O. You can, however, declare a file of a given EDT (such as
"file of Months") or have an EDT as an element of an array, record, or
set that is a data file component.
EDT variables in Turbo Pascal occupy one byte; you can have a maximum
of 256 identifiers in an EDT list. Turbo Pascal allows type casting
from any ordinal type (Integer, Char, Boolean, or another EDT) to a
given EDT; use the name of the EDT as the "function." For example, the
statement
Current := Months(3);
will assign the value April to the variable Current.
-------------------------------- Example ---------------------------------
type
Days = (Sun,Mon,Tues,Wed,Thur,Fri,Sat);
var
Iota,Able,Fred : Days;
begin
Iota := Wed; { Iota gets Wed }
Able := Succ(Iota); { Able gets Thur }
Fred := Days(2); { Fred gets Tues }
end.
See Also:
Integer
Char
This page created by ng2html v1.05, the Norton guide to HTML conversion utility.
Written by Dave Pearson