Home Up Contents Search

CASE statement

CASE statement

The CASE statement allows a program to test the value of an ordinal variable against a number of possible values, and to execute
statements appropriate for each possible value.

Syntax:

CASE <ordinal-expression> OF
<value1> : <action1>;
<value2> : <action2>;
...
<valueN> : <actionN>;
[
ELSE <actionN+1>; ]
END; { case }


Note that the ELSE clause is optional; if omitted, and if <ordinal-expression> matches none of the values in the case-tests, then
execution simply continues with the statement after the CASE statement.

CASE statements usually result in more readable programs than the equivalent operation performed by multiple IF statements.

program CaseDemo;

uses
Crt32;

var

command: char;
foreColour,
backColour: integer;


begin

foreColour := 0;
backColour := 0;
while true do

begin

Write('T(one, F(orecolour, B(ackcolour, Q(uit: ');
command := ReadKey;
Writeln;
command := UpCase(command);
case command of

'T': Write(^G);
'F':
begin

foreColour := foreColour + 1;
TextColor(foreColour);
end; { 'F' }

'B': begin

backColour := backColour + 1;
TextBackground(backColour);
end; { 'B' }

'Q': Exit;
else Writeln('Illegal command')

end; { case }

end;

end. { CaseDemo }

 
Previous Next
Send mail to ljschwerin@hotmail.com with questions or comments about this web site.

Copyright © 1999-2002 Leon Schwerin
Last modified: 26 March 2000
1