Case statement

Description

Case statements perform one action out of a choice of actions, and include the following:

Constant ranges can be used to specify a number of contiguous case-constants (for example 1..5 is the same as 1, 2, 3, 4, 5).

No two case-list-elements can contain the same constant, so

   case x of
    1, 2, 3 : write('1 to 3');
    2 :  write('2');
   end

is an error since both case-list-elements contain 2.

Example

For example, the program fragment below shows an ordinal type and a procedure with a case statement.

   type
      DayOfWeek =
    (monday, tuesday, wednesday, thursday, friday, saturday, sunday);

   procedure WriteDayOfWeek(day : DayOfWeek);
   begin
      case day of
         monday:    write('monday');
         tuesday:   write('tuesday');
         wednesday: write('wednesday');
         thursday:  write('thursday');
         friday:    write('friday');
         saturday:  write('saturday');
         sunday:    write('sunday');
      end
   end;

When the case statement is executed the case-index (i.e. day) is evaluated and if it is equal to tuesday, for example, then since the second case-list-element contains a constant equal to tuesday then the second case-list-element is choosen and its statement (i.e. write('tuesday')) is executed.

Below is a slightly more complex example.

   program example(input, output);
   var
      c : char;
   begin
      write('Enter a digit :');
      readln(c);
      case c of
      '0'..'9' : writeln('OK');
      'a'..'z', 'A'..'Z' : writeln('That is a letter');
      otherwise writeln('What is that?');
      end
   end.

When the case statement is executed the case-index (i.e. c) is evaluated and its value used as follows:

Irie Pascal implements case statements in two different ways depending on the type of the case-index.

  1. If the case-index in the case statement has a type with 1024 values or less then Irie Pascal implements the case statement using a jump table.
  2. If the case-index in the case statement has a type with more than 1024 values then Irie Pascal implements the case statement as a series of hidden if statements.
In earlier versions of Irie Pascal the second (if statement) implementation had serious problems, the rule that no two case-list-elements can have the same constant was not enforced, and the case-index was evaluated once for each case-list-element until a match was found. As a result of these problems a warning message was issued when the second implementation was used. These problems have now been removed from the second implementation, so that now no matter what implementation is used, the rule that no two case-list-elements can have the same constant is always enforced, and the case-index is never evaluated more than once. As a result the warning message is no longer issued when the second implementation is used.

Syntax

(NOTE: for clarity some parts of the syntax are omitted, see Irie Pascal Grammar for the full syntax):

   case-statement = 'case' case-index case-body [ ';' ] 'end'

   case-index = ordinal-expression

   case-body = case-list-elements [ [ ';' ] case-statement-completer ] |
           case-statement-completer

   case-list-elements = case-list-element { ';' case-list-element }

   case-list-element = case-constant-list ':' statement

   case-constant-list = case-specifier { ',' case-specifier }

   case-specifier = case-constant [ '..' case-constant ]

   case-constant = ordinal-constant

   case-statement-completer = ( 'otherwise' | 'else' ) statement-sequence