Difference between revisions of "Case"
Line 44: | Line 44: | ||
end; | end; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
If a hexadecimal is a different kind of a variable, for example Shortint, values shall be cast: | If a hexadecimal is a different kind of a variable, for example Shortint, values shall be cast: | ||
Line 53: | Line 54: | ||
else ShowMessage('Better luck next time'); | else ShowMessage('Better luck next time'); | ||
end; | end; | ||
+ | </syntaxhighlight> | ||
+ | or another solution can be used: | ||
+ | <syntaxhighlight> | ||
+ | var | ||
+ | myplace : ShortInt; | ||
+ | place : Byte absolute myplace; | ||
+ | begin | ||
+ | myplace:=-61; | ||
+ | case place of | ||
+ | $C3: ShowMessage('Gold medal'); | ||
+ | $34: ShowMessage('Silver medal'); | ||
+ | $58: ShowMessage('Bronze medal'); | ||
+ | else ShowMessage('Better luck next time'); | ||
+ | end; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Revision as of 11:40, 3 February 2015
│
Deutsch (de) │
English (en) │
español (es) │
suomi (fi) │
français (fr) │
русский (ru) │
Case opens a case statement. The case statement compares the value of an ordinal expression to each selector, which can be a constant, a subrange, or a list of them separated by commas. The selector field is separated from action field by Colon.
The case statement includes the reserved words Of and End. Else can be used if needed, too.
case place of
1: ShowMessage('Gold medal');
2: ShowMessage('Silver medal');
3: ShowMessage('Bronze medal');
else ShowMessage('Better luck next time');
end;
WhatIsChar
function WhatIsChar( c:char ):string;
var
s : string;
begin
s := '';
case c of
'0' .. '9' : s := 'digit (0-9)';
'a' .. 'z' : s := 'lowercase letter (a-z)';
'A' .. 'Z' : s := 'uppercase letter (A-Z)';
'+' , '-' : s := 'sign (+ or -)';
end;
result := s;
end;
Hexadecimal input
If hexadecimal number are used, it is considered that they are unsigned:
case place of
$1: ShowMessage('Gold medal');
$2: ShowMessage('Silver medal');
$3: ShowMessage('Bronze medal');
else ShowMessage('Better luck next time');
end;
If a hexadecimal is a different kind of a variable, for example Shortint, values shall be cast:
case place of
Shortint($C3): ShowMessage('Gold medal'); //C3= -61;
Shortint($34): ShowMessage('Silver medal');
Shortint($58): ShowMessage('Bronze medal');
else ShowMessage('Better luck next time');
end;
or another solution can be used:
var
myplace : ShortInt;
place : Byte absolute myplace;
begin
myplace:=-61;
case place of
$C3: ShowMessage('Gold medal');
$34: ShowMessage('Silver medal');
$58: ShowMessage('Bronze medal');
else ShowMessage('Better luck next time');
end;
Variant Record
Case is used in Variant Record, too. A Variant Record is also called a tagged union.
type
ScaleKelvin = 223 .. 323;
ScaleCelsius = -50 .. 50;
TemperatureScale = ( celsius, kelvin ) ;
Temperature = record
case scale : TemperatureScale of
celcius : (celsius_value : ScaleCelsius);
kelvin : (kelvin_value : ScaleKelvin);
end;