Difference between revisions of "Case"
From Free Pascal wiki
Jump to navigationJump to searchm (Text replace - "delphi>" to "syntaxhighlight>") |
(Grammar, fixed code inconsistencies) |
||
Line 1: | Line 1: | ||
{{Case}} | {{Case}} | ||
− | Case opens a case statement. The case statement compares the value of ordinal expression to each selector, which can be a [[Const|constant]], a subrange, or a list of them separated by [[Comma|commas]]. | + | Case opens a case statement. The case statement compares the value of an ordinal expression to each selector, which can be a [[Const|constant]], a subrange, or a list of them separated by [[Comma|commas]]. The selector field is separated from action field by [[Colon]]. |
− | The case statement includes [[Reserved word|reserved words]] [[Of]] and [[End]] . | + | The case statement includes the [[Reserved word|reserved words]] [[Of]] and [[End]]. [[Else]] can be used if needed, too. |
<syntaxhighlight> | <syntaxhighlight> | ||
− | |||
case place of | case place of | ||
1: ShowMessage('Gold medal'); | 1: ShowMessage('Gold medal'); | ||
Line 14: | Line 13: | ||
else ShowMessage('Better luck next time'); | else ShowMessage('Better luck next time'); | ||
end; | end; | ||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 20: | Line 18: | ||
<syntaxhighlight> | <syntaxhighlight> | ||
− | |||
function WhatIsChar( c:char ):string; | function WhatIsChar( c:char ):string; | ||
var | var | ||
Line 28: | Line 25: | ||
case c of | case c of | ||
'0' .. '9' : s := 'digit (0-9)'; | '0' .. '9' : s := 'digit (0-9)'; | ||
− | 'a' .. 'z' : s := ' | + | 'a' .. 'z' : s := 'lowercase letter (a-z)'; |
− | 'A' .. 'Z' : s := ' | + | 'A' .. 'Z' : s := 'uppercase letter (A-Z)'; |
'+' , '-' : s := 'sign (+ or -)'; | '+' , '-' : s := 'sign (+ or -)'; | ||
end; | end; | ||
result := s; | result := s; | ||
end; | end; | ||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
== Variant Record == | == Variant Record == | ||
− | Case | + | Case is used in Variant [[Record]], too. A Variant Record is also called a tagged union. |
<syntaxhighlight> | <syntaxhighlight> | ||
− | |||
type | type | ||
Line 48: | Line 43: | ||
ScaleCelsius = -50 .. 50; | ScaleCelsius = -50 .. 50; | ||
− | TemperatureScale | + | TemperatureScale = ( celsius, kelvin ) ; |
− | Temperature | + | Temperature = record |
− | case | + | case scale : TemperatureScale of |
− | celcius : ( | + | celcius : (celsius_value : ScaleCelsius); |
kelvin : (kelvin_value : ScaleKelvin); | kelvin : (kelvin_value : ScaleKelvin); | ||
end; | end; | ||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
− | == | + | == See also == |
− | |||
* [http://www.freepascal.org/docs-html/ref/refsu40.html#x110-11700010.2.2 The Case statement] | * [http://www.freepascal.org/docs-html/ref/refsu40.html#x110-11700010.2.2 The Case statement] | ||
* [http://www.freepascal.org/docs-html/ref/refsu15.html#x38-450003.3.2 Record types] | * [http://www.freepascal.org/docs-html/ref/refsu15.html#x38-450003.3.2 Record types] |
Revision as of 11:34, 3 December 2012
│
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;
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;