Difference between revisions of "Case"

From Free Pascal wiki
Jump to navigationJump to search
Line 10: Line 10:
 
   2: ShowMessage('Silver medal');
 
   2: ShowMessage('Silver medal');
 
   3: ShowMessage('Bronze medal');  
 
   3: ShowMessage('Bronze medal');  
   else ShowMessage('Next time: Good luck');  
+
   else ShowMessage('Next time: Better luck');  
 
  end;
 
  end;
  
Line 39: Line 39:
 
* [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]
 +
* [http://lazarus-ccr.sourceforge.net/pascal/pas3cb.html Tao Yue: Learn Pascal!] Case
 +
* [[Dialog_Examples#ShowMessage|ShowMessage]]
 +
* [[Type]]

Revision as of 11:36, 17 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 to action field by Colon.

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


<delphi>

case place of
  1: ShowMessage('Cold medal');
  2: ShowMessage('Silver medal');
  3: ShowMessage('Bronze medal'); 
  else ShowMessage('Next time: Better luck'); 
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