Difference between revisions of "Case"

From Lazarus wiki
Jump to navigationJump to search
m (Reverted edits by Reverse22 (Talk); changed back to last version by DLag)
m (Text replace - "delphi>" to "syntaxhighlight>")
Line 6: Line 6:
  
  
<delphi>
+
<syntaxhighlight>
  
 
  case place of
 
  case place of
Line 15: Line 15:
 
  end;
 
  end;
  
</delphi>
+
</syntaxhighlight>
  
 
=== WhatIsChar ===
 
=== WhatIsChar ===
  
<delphi>
+
<syntaxhighlight>
  
 
  function WhatIsChar( c:char ):string;
 
  function WhatIsChar( c:char ):string;
Line 35: Line 35:
 
  end;
 
  end;
  
</delphi>
+
</syntaxhighlight>
  
 
== Variant Record ==
 
== Variant Record ==
Line 41: Line 41:
 
Case-word is used Variant [[Record]], too. Variant Record also called a tagged union.
 
Case-word is used Variant [[Record]], too. Variant Record also called a tagged union.
  
<delphi>
+
<syntaxhighlight>
  
 
   type
 
   type
Line 55: Line 55:
 
   end;
 
   end;
  
</delphi>
+
</syntaxhighlight>
  
 
== Read more ==
 
== Read more ==

Revision as of 14:57, 24 March 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 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.


 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 := 'small letter (a-z)';
     'A' .. 'Z' : s := 'big letter (A-Z)';
     '+' , '-'  : s := 'sign (+ or -)';
   end;
   result := s;
 end;

Variant Record

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

  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;

Read more