case expression of
selector: statement;
...
selector: statement;
end;
or, with alternative statement sequence:
case expression of
selector: statement;
...
selector: statement;
otherwise { ``else'' instead of ``otherwise'' is allowed }
statement;
...
statement;
end;
or, as part of the invariant record type definition:
foo = record
field_declarations
case bar: variant_type of
selector: (field_declarations);
selector: (field_declarations);
...
end;
or, without a variant selector field,
foo = record
field_declarations
case variant_type of
selector: (field_declarations);
selector: (field_declarations);
...
end;
case opens a case statement. For further description see case Statement.
For case in a variant record type definition, see Record Types.
The case statement is defined in ISO 7185 Pascal and supported by all known Pascal variants.
According to ISO 7185 Pascal, the selector type must be a named type. UCSD Pascal and Borland Pascal allow any ordinal type here.
The alternative statement execution with otherwise it is an Extended Pascal extension; with else it is a Borland Pascal extension. In GNU Pascal, both are allowed.
program CaseDemo;
var
Foo: String (10);
Bar: Integer;
begin
WriteLn ('Enter up to ten arbitrary characters:');
ReadLn (Foo);
for Bar := 1 to Length (Foo) do
begin
Write (Foo[Bar], ' is ');
case Foo[Bar] of
'A' .. 'Z', 'a' .. 'z':
WriteLn ('an English letter');
'0' .. '9':
WriteLn ('a number');
otherwise
WriteLn ('an unrecognized character')
end
end
end.