Difference between revisions of "Case"

From Free Pascal wiki
Jump to navigationJump to search
Line 1: Line 1:
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]]. Selector field separated to action field by [[Colon]].
+
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]]. Selector field separated from action field by [[Colon]].
  
The case statement include [[Reserved word|reserved words]] [[Of]] and [[End]] . Sometimes [[Else]]. too.
+
The case statement includes [[Reserved word|reserved words]] [[Of]] and [[End]] . Sometimes [[Else]], too.
  
  
Line 7: Line 7:
  
 
  case place of
 
  case place of
   1: ShowMessage('Cold medal');
+
   1: ShowMessage('Gold medal');
 
   2: ShowMessage('Silver medal');
 
   2: ShowMessage('Silver medal');
 
   3: ShowMessage('Bronze medal');  
 
   3: ShowMessage('Bronze medal');  
   else ShowMessage('Next time: Better luck');  
+
   else ShowMessage('Better luck next time');  
 
  end;
 
  end;
  

Revision as of 15:03, 20 August 2008

Case opens a case statement. The case statement compares the value of ordinal expression to each selector, which can be a constant, a subrange, or a list of them separated by commas. Selector field separated from action field by Colon.

The case statement includes reserved words Of and End . Sometimes Else, too.


<delphi>

case place of
  1: ShowMessage('Gold medal');
  2: ShowMessage('Silver medal');
  3: ShowMessage('Bronze medal'); 
  else ShowMessage('Better luck next time'); 
end;

</delphi>

Variant Record

Case-word is used Variant Record, too. Variant Record also called a tagged union.

<delphi>

 type
     
  ScaleKelvin = 223 .. 323;
  ScaleCelsius = -50 .. 50;
   
  TemperatureScale   =  ( celcius, kelvin ) ;
  Temperature   =   record
   case  scale   :   TemperatureScale   of
    celcius : (celcius_value : ScaleCelsius);
    kelvin : (kelvin_value : ScaleKelvin);
  end;

</delphi>

Read more