Difference between revisions of "Case"

From Lazarus wiki
Jump to navigationJump to search
Line 11: Line 11:
 
   3: ShowMessage('Bronze medal');  
 
   3: ShowMessage('Bronze medal');  
 
   else ShowMessage('Better luck next time');  
 
   else ShowMessage('Better luck next time');  
 +
end;
 +
 +
</delphi>
 +
 +
=== WhatIsChar ===
 +
 +
<delphi>
 +
 +
function WhatIsChar( c:char ):string;
 +
var
 +
  s : string;
 +
begin
 +
  s := '';
 +
  case c of
 +
    '0' .. '9' : s := 'digit (0-9)';
 +
    'a' .. 'z' : s := 'small letter (a-z)';
 +
    'A' .. 'Z' : s := 'big letter (A-Z)';
 +
    '+' , '-'  : s := 'sign (+ or -)';
 +
  end;
 +
  result := s;
 
  end;
 
  end;
  
Line 40: Line 60:
 
* [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
 
* [http://lazarus-ccr.sourceforge.net/pascal/pas3cb.html Tao Yue: Learn Pascal!] Case
 +
* [http://www.youtube.com/watch?v=pMr2xtUu3x0 Video: Free Pascal Tutorial 8 - Case Statements]
 
* [[Dialog_Examples#ShowMessage|ShowMessage]]
 
* [[Dialog_Examples#ShowMessage|ShowMessage]]
 
* [[Type]]
 
* [[Type]]

Revision as of 15:09, 14 October 2009

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>

WhatIsChar

<delphi>

function WhatIsChar( c:char ):string;
var
  s : string;
begin
  s := ;
  case c of
    '0' .. '9' : s := 'digit (0-9)';
    'a' .. 'z' : s := 'small letter (a-z)';
    'A' .. 'Z' : s := 'big letter (A-Z)';
    '+' , '-'  : s := 'sign (+ or -)';
  end;
  result := s;
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