Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Learn Pascal in a Three Days (2nd Ed.)
(Publisher: Wordware Publishing, Inc.)
Author(s):
ISBN: 1556225679
Publication Date: 07/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


Chapter 5
Data Architecture

5-1 Ordinal Data Types

The data types explained so far are all predefined in the language and are called simple data types, as opposed to structured data types. Each datum of a simple data type is one single element, while in structured types (such as arrays) a datum may contain a collection of items.

Simple types fall into two main categories:

  The ordinal type
  The real type

The ordinal types include the INTEGER, CHAR, and BOOLEAN types. An ordinal type is distinguished by data values that form a series of discrete elements such that every element has a discrete predecessor (except the first element) and successor (except the last element). Integers are like that, as they form a set of distinct numbers ranging from -(MAXINT+1) to +MAXINT. The element “4,” for example, is preceded by “3” and followed by “5.” The type CHAR includes a set of characters ordered sequentially according to their ordinal numbers. The type BOOLEAN contains the set “TRUE and FALSE.” The value FALSE has the ordinal number “0” while TRUE has the ordinal number “1.”

Real numbers, on the other hand, are not discrete. For example, between the number “0” and “1” there exists an infinite number of fractions. Between any two real numbers, then, there is another real number.

Enumerations

It is sometimes useful in a program to define days of the week as integers in order to make the program code more readable. In this case, you need to either assign each day a number or to declare each a named constant as in:

    CONST
     Monday = 0;
     Tuesday = 1;
     Wednesday = 2;
     Thursday = 3;
     Friday = 4;
     Saturday = 5;
     Sunday = 6;

After these declarations you can refer to any of these days by its name:

    IF Today = Sunday THEN
       WRITELN('Sorry, we are closed on Sundays..');

In this statement an integer variable “Today” is tested to check if it is “Sunday”; in other words, if it contains the value “6.” Using such declarations will take a lot of programming effort, though, especially when you have a large number of constants (such as the names of the months).

The enumerated type gives you a shortcut to doing the same thing. Look at the following declaration:

VAR
   Day:(Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday);

In this declaration, the identifiers representing the months are listed in an ordered series and separated by commas. Thus, Monday is internally coded as “0” and “Sunday” is coded as “6.” Other days are represented by numbers between “0” and “6” according to their sequence in the enumeration. It is, however, illegal to read or write these values directly as you do with simple types (using WRITELN and READLN statements). With enumerations you may use any of the following operations:

1.  You may assign any one of the enumeration elements to the variable “Day” like this:
Day:= Friday;

but it is illegal to assign an explicit number to the variable “Day,” such as “Day:= 1.” This feature assures that the enumeration will be only assigned valid data.
2.  You can obtain and use the values associated with the enumeration elements using the ORD function. For example:
WRITELN(ORD(Monday));             gives the value "0"
WRITELN(ORD(Tuesday));    gives the value "1"
3.  You may also use the functions PRED and SUCC to obtain the predecessor and the successor of a specified element:
WRITELN(PRED(Friday));    gives the value "3"
WRITELN(SUCC(Monday));            gives the value "1"
4.  You can compare values of the enumerated type using the boolean operators (simple or compound), like this:
IF (Day = Saturday) OR (Day = Sunday) THEN
WRITELN('This is a weekend day.');

Again, you cannot use the explicit values in comparisons such as “IF Day = 2.” This results in an error.

In the following program a FOR loop uses the enumeration “Month” to display the corresponding integer values from “0” to “11.”

{ -------------------- figure 5-1 -------------------- }
PROGRAM Enumeration1(OUTPUT);
VAR
 Month:(Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec);
BEGIN
 WRITELN;
 FOR Month:= Jan TO Dec DO
  WRITE(ORD(Month),' ')
END.

The output of this program is:

0 1 2 3 4 5 6 7 8 9 10 11

As you can see in the output, the values that correspond to the twelve months range from “0” to “11.” If you would like to see the values range from “1” to “12” as the months in the calendar do, you can use the expression “ORD(month)+1” instead of the expression “ORD(month).” The enumerated type is an ordinal data type and is classified as a user-defined type.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.