Contents | Prev | Next
9.8 Case statement
Description
Case statements perform one action out of a choice of actions, and include the
following:
- A case-index, which is specified by an ordinal expression (i.e an
expression of
ordinal type). The case-index is evaluated and it's
value determines which action is performed.
- Zero or more case-list-elements, each of which, represent a choice of action.
Case-list-elements contain one or more case-constants and a statement. The case-constants in
a case-list-element specify values which if equal to the value of the case-index will cause
that case-list-element to be choosen. The statement in a case-list-element specifies the action
performed when that case-list-element is choosen. This statement can be a
compound statement, containing a statement-sequence.
If this statement transfers program execution to a statement outside
of the case statement then execution of the case statement is
terminated when program execution is transfered.
- As an extension to Standard Pascal, Irie Pascal
allows case statements to include case-statement-completers, which if present,
represents a kind of default choice of action (i.e. the action choosen when none of the
other actions are choosen). Case-statement-completers contain a statement-sequence, which
specifies the action performed when the case-statement-completer is choosen. If one of the
statements in the statement-sequence transfers program execution to a statement outside
of the case statement then execution of the case statement is
terminated when program execution is transfered.
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 it's 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:
- If the value is a digit then the first case-list-element is choosen, and it's statement
(i.e. writeln('OK')) is executed.
- If the value is a letter then the second case-list-element is choosen, and it's statement
(i.e. writeln('That is a letter')) is executed.
- If the value is not a digit or a letter then the case-statement-completer is choosen,
and it's statement (i.e. writeln('What is that?')) is executed.
Irie Pascal implements case statements in two different ways depending on the type of the
case-index.
- 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.
- 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
Contents | Prev | Next