Back to DFS's Pascal Page


Enumerated Type

It is frequently desirable to have subscripts which are represented by labels instead of numeric values.

This can be done with various constants defined as the same value. However, this can lead to long tedious lists at the beginning of a program.

Pascal provides another method -- a programmer-defined type called an enumerated type. This is a set of integer constants that are represented by identifiers, the usual alphanumeric ones used for variables. The type is declared as an ordinal list in the type section such as the following one for the days of the week.

type
   days = ( SUN, MON, TUES, WED, THURS, FRI, SAT );

Internally, the values start with the zeroth item, so SUN is equivalent to 0 and SAT to 6 in the above example. Thus, for a variable day declared as

   day : days;

could be assigned a value as follows

   day := TUES

and the following writeln statement would print the character 2.

   writeln(day)

The use of the enumerated type permits loops to be defined such as the following.

   for day := SUN to SAT do

Turbo Pascal permits typecasting which uses the name of an enumerated type like a function call to convert an integer to the enumerated type. This technique is used in the second program below ane results in shorter code but it may not be available on all Pascal compilers/interpreters.

   day := days(Choice - 1)

The output below can be produced by either of the two programs which follow. The portions of code which differ are highlighted in red.

    MENU
1     Sunday
2     Monday
3    Tuesday
4  Wednesday
5   Thursday
6     Friday
7   Saturday
Select a day by typing its number: 3
You selected Tuesday.

Standard Pascal Version Using a case Statement

This program uses a case statement to convert the user's selection into enumerated type data.

Program Days_Enum(input, output);
type
  days = (SUN, MON, TUES, WED, THURS, FRI, SAT);
var
  names : array [days] of string[10];
  Today, day : days;
  Choice : integer;
begin
  {Initialize names array}
  names[SUN] := 'Sunday';
  names[MON] := 'Monday';
  names[TUES] := 'Tuesday';
  names[WED] := 'Wednesday';
  names[THURS] := 'Thursday';
  names[FRI] := 'Friday';
  names[SAT] := 'Saturday';
  {Print Menu}
  writeln('MENU':8);
  for day := SUN to SAT do
    writeln(ord(day)+ 1, names[day]:11);
  {Get User's Selection}
  repeat
    write('Select a day by typing its number: ');
    readln(Choice)
  until Choice in [1..7];
  {Convert Selection to Enumerated Type}
  case Choice of
    1: day := SUN;
    2: day := MON;
    3: day := TUES;
    4: day := WED;
    5: day := THURS;
    6: day := FRI;
    7: day := SAT
  end;
  {Report Selection}
  writeln('You selected ', names[day], '.')
end.

Turbo Pascal Version Using Typecasting

This program uses typecasting to convert the integer value of Choice to the enumerated type data of day. This technique results in shorter code but may not be available on all Pascal compilers/interpreters.

Program Days_Enum(input, output);
type
  days = (SUN, MON, TUES, WED, THURS, FRI, SAT);
var
  names : array [days] of string[10];
  Today, day : days;
  Choice : integer;
begin
  {Initialize names array}
  names[SUN] := 'Sunday';
  names[MON] := 'Monday';
  names[TUES] := 'Tuesday';
  names[WED] := 'Wednesday';
  names[THURS] := 'Thursday';
  names[FRI] := 'Friday';
  names[SAT] := 'Saturday';
  {Print Menu}
  writeln('MENU':8);
  for day := SUN to SAT do
    writeln(ord(day)+ 1, names[day]:11);
  {Get User's Selection}
  repeat
    write('Select a day by typing its number: ');
    readln(Choice)
  until Choice in [1..7];
  {Convert Selection to Enumerated Type by Typecasting}
  day := days(Choice - 1);
  {Report Selection}
  writeln('You selected ', names[day], '.')
end.

Back to Top


© 1999 DFStermole
Created 30 Dec 99