Quick start manual

4-26
Delphi Language Guide
Declarations and statements
Each value represented by a caseList must be unique in the case statement; subranges
and lists cannot overlap. A case statement can have a final else clause:
case selectorExpression of
caseList
1
: statement
1
;
ƒ
caseList
n
: statement
n
;
else
statements;
end
where statements is a semicolon-delimited sequence of statements. When a case
statement is executed, at most one of statement
1
... statement
n
is executed. Whichever
caseList has a value equal to that of selectorExpression determines the statement to be
used. If none of the caseLists has the same value as selectorExpression, then the
statements in the else clause (if there is one) are executed.
The case statement
case I of
1..5: Caption := 'Low';
6..9: Caption := 'High';
0, 10..99: Caption := 'Out of range';
else
Caption := '';
end;
is equivalent to the nested conditional
if I in [1..5] then
Caption := 'Low'
else if I in [6..10] then
Caption := 'High'
else if (I = 0) or (I in [10..99]) then
Caption := 'Out of range'
else
Caption := '';
Other examples of case statements:
case MyColor of
Red: X := 1;
Green: X := 2;
Blue: X := 3;
Yellow, Orange, Black: X := 0;
end;
case Selection of
Done: Form1.Close;
Compute: CalculateTotal(UnitCost, Quantity);
else
Beep;
end;