Difference between revisions of "Case"

From Lazarus wiki
Jump to navigationJump to search
(rename Using -> Case)
(Rearrange order)
Line 141: Line 141:
 
end;  
 
end;  
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
== Case Set ==
 +
 +
If you want to use Sets, this example is very clear:
 +
<syntaxhighlight>
 +
{ COMMENTS TO edgarrod71@gmail.com }
 +
type
 +
    medals = (Gold, Silver, Bronze);  // in case you want Gold to Start from 1, put this (Gold=1, ...but
 +
    SMedals = array [medals] of string[6] = ('Gold', 'Silver', 'Bronze'); // in [medals], you have to change it to [Gold..Bronze] ;)
 +
{[ ... ]}
 +
var
 +
  place: integer;  /// place starts from 0;
 +
{[ ... ]}
 +
 +
  case medals(place) of
 +
      Gold:  ShowMessage(SMedals[medals(place)] + ' medal');
 +
      Silver: ShowMessage(SMedals[medals(place)] + ' medal');
 +
      Bronze: ShowMessage(SMedals[medals(place)] + ' medal');
 +
  else
 +
      ShowMessage('Better luck next time! :)');
 +
  end;
 +
</syntaxhighlight>
 +
  
 
== Hexadecimal input ==
 
== Hexadecimal input ==
Line 195: Line 218:
 
     kelvin : (kelvin_value : ScaleKelvin);
 
     kelvin : (kelvin_value : ScaleKelvin);
 
   end;
 
   end;
</syntaxhighlight>
 
 
== Case Set ==
 
 
If you want to use Sets, this example is very clear:
 
<syntaxhighlight>
 
{ COMMENTS TO edgarrod71@gmail.com }
 
type
 
    medals = (Gold, Silver, Bronze);  // in case you want Gold to Start from 1, put this (Gold=1, ...but
 
    SMedals = array [medals] of string[6] = ('Gold', 'Silver', 'Bronze'); // in [medals], you have to change it to [Gold..Bronze] ;)
 
{[ ... ]}
 
var
 
  place: integer;  /// place starts from 0;
 
{[ ... ]}
 
 
  case medals(place) of
 
      Gold:  ShowMessage(SMedals[medals(place)] + ' medal');
 
      Silver: ShowMessage(SMedals[medals(place)] + ' medal');
 
      Bronze: ShowMessage(SMedals[medals(place)] + ' medal');
 
  else
 
      ShowMessage('Better luck next time! :)');
 
  end;
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  

Revision as of 04:54, 29 August 2017

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;

Case string

Case of works on strings. The Pascal language allows you to "Case of" on a string variable.

const
  ISO_3166_1_ALPHA_3_ANDORRA  = 'AND';
  ISO_3166_1_ALPHA_3_AUSTRALIA = 'AUS';
  ISO_3166_1_ALPHA_3_AUSTRIA = 'AUT';
  ISO_3166_1_ALPHA_3_BELGIUM = 'BEL';
  ISO_3166_1_ALPHA_3_BRAZIL = 'BRA';
  ISO_3166_1_ALPHA_3_CANADA = 'CAN';
  ISO_3166_1_ALPHA_3_CHINA = 'CHN';
  ISO_3166_1_ALPHA_3_CZECH_REPUBLIC = 'CZE';
  ISO_3166_1_ALPHA_3_CYPRUS = 'CYP';
  ISO_3166_1_ALPHA_3_GERMANY = 'DEU';
  ISO_3166_1_ALPHA_3_DENMARK = 'DNK';
  ISO_3166_1_ALPHA_3_SPAIN = 'ESP';
  ISO_3166_1_ALPHA_3_ESTONIA = 'EST';
  ISO_3166_1_ALPHA_3_FINLAND = 'FIN';
  ISO_3166_1_ALPHA_3_FRANCE = 'FRA';
  ISO_3166_1_ALPHA_3_GREECE = 'GRC';
  ISO_3166_1_ALPHA_3_INDIA = 'IND';
  ISO_3166_1_ALPHA_3_IRELAND = 'IRL';
  ISO_3166_1_ALPHA_3_ITALY = 'ITA';
  ISO_3166_1_ALPHA_3_JAPAN = 'JPN';
  ISO_3166_1_ALPHA_3_LITHUANIA = 'LTU';
  ISO_3166_1_ALPHA_3_LATVIA = 'LVA';
  ISO_3166_1_ALPHA_3_LUXEMBOURG = 'LUX';
  ISO_3166_1_ALPHA_3_MALTA = 'MLT';
  ISO_3166_1_ALPHA_3_MEXICO = 'MEX';
  ISO_3166_1_ALPHA_3_MONACO = 'MCO';
  ISO_3166_1_ALPHA_3_MONTENEGRO = 'MNE';
  ISO_3166_1_ALPHA_3_NAURU = 'NRU';
  ISO_3166_1_ALPHA_3_NETHERLANDS = 'NLD';
  ISO_3166_1_ALPHA_3_NORWAY = 'NOR';
  ISO_3166_1_ALPHA_3_POLAND = 'POL';
  ISO_3166_1_ALPHA_3_PORTUGAL = 'PRT';
  ISO_3166_1_ALPHA_3_REPUBLIC_OF_KOREA = 'KOR';
  ISO_3166_1_ALPHA_3_RUSSIAN_FEDERATION = 'RUS';
  ISO_3166_1_ALPHA_3_SAN_MARINO = 'SMR';
  ISO_3166_1_ALPHA_3_SLOVAKIA = 'SVK';
  ISO_3166_1_ALPHA_3_SLOVENIA = 'SVN';
  ISO_3166_1_ALPHA_3_SWEDEN = 'SWE' ;
  ISO_3166_1_ALPHA_3_SWITZERLAND = 'CHE';
  ISO_3166_1_ALPHA_3_UNITED_KINGDOM_OF_GREAT_BRITAIN_AND_NORTHERN_IRELAND = 'GBR';
  ISO_3166_1_ALPHA_3_UNITED_STATES_OF_AMERICA = 'USA';


  ISO_4217_EURO = 'EUR';
  ISO_4217_AUSTRALIAN_DOLLAR = 'AUD';
  ISO_4217_BRAZILIAN_REAL = 'BRL';
  ISO_4217_CANADIAN_DOLLAR = 'CAD';
  ISO_4217_CHINESE_YUAN = 'CNY';
  ISO_4217_CZECH_KORUNA = 'CZK' ;
  ISO_4217_DANISH_KRONE = 'DKK' ;
  ISO_4217_INDIAN_RUPEE = 'INR';
  ISO_4217_POUND_STERLING = 'GBP' ;
  ISO_4217_JAPANESE_YEN  = 'JPY';
  ISO_4217_MEXICAN_PESO = 'MXN';
  ISO_4217_NORWEGIAN_KRONE = 'NOK' ;
  ISO_4217_POLISH_ZLOTY = 'PLN' ;
  ISO_4217_RUSSIAN_RUBLE = 'RUB' ;
  ISO_4217_SOUTH_KOREAN_WON = 'KRW';
  ISO_4217_SWEDISH_KRONA = 'SEK' ;
  ISO_4217_SWISS_FRANC = 'CHF' ;
  ISO_4217_UNITED_STATES_DOLLAR = 'USD' ;

function ISO_4217_currency_name ( ISO_3166_1_alpha_3_code: string): string;
begin
  Case ISO_3166_1_alpha_3_code  of
    // Eurozone
    ISO_3166_1_ALPHA_3_ANDORRA, ISO_3166_1_ALPHA_3_AUSTRIA,
    ISO_3166_1_ALPHA_3_BELGIUM,ISO_3166_1_ALPHA_3_CYPRUS,
    ISO_3166_1_ALPHA_3_GERMANY, ISO_3166_1_ALPHA_3_SPAIN,
    ISO_3166_1_ALPHA_3_ESTONIA, ISO_3166_1_ALPHA_3_FINLAND,
    ISO_3166_1_ALPHA_3_FRANCE, ISO_3166_1_ALPHA_3_GREECE,
    ISO_3166_1_ALPHA_3_IRELAND, ISO_3166_1_ALPHA_3_ITALY,
    ISO_3166_1_ALPHA_3_LITHUANIA, ISO_3166_1_ALPHA_3_LATVIA,
    ISO_3166_1_ALPHA_3_LUXEMBOURG, ISO_3166_1_ALPHA_3_MONACO,
    ISO_3166_1_ALPHA_3_MALTA, ISO_3166_1_ALPHA_3_MONTENEGRO,
    ISO_3166_1_ALPHA_3_NETHERLANDS, ISO_3166_1_ALPHA_3_PORTUGAL,
    ISO_3166_1_ALPHA_3_SAN_MARINO, ISO_3166_1_ALPHA_3_SLOVAKIA,
    ISO_3166_1_ALPHA_3_SLOVENIA : result := ISO_4217_EURO ;

    ISO_3166_1_ALPHA_3_AUSTRALIA, ISO_3166_1_ALPHA_3_NAURU :
          result := ISO_4217_AUSTRALIAN_DOLLAR;
    ISO_3166_1_ALPHA_3_BRAZIL : result := ISO_4217_BRAZILIAN_REAL;
    ISO_3166_1_ALPHA_3_CANADA : result := ISO_4217_CANADIAN_DOLLAR;
    ISO_3166_1_ALPHA_3_CHINA : result := ISO_4217_CHINESE_YUAN;
    ISO_3166_1_ALPHA_3_CZECH_REPUBLIC : result := ISO_4217_CZECH_KORUNA ;
    ISO_3166_1_ALPHA_3_DENMARK : result := ISO_4217_DANISH_KRONE ;
    ISO_3166_1_ALPHA_3_INDIA : result := ISO_4217_INDIAN_RUPEE;
    ISO_3166_1_ALPHA_3_JAPAN : result := ISO_4217_JAPANESE_YEN;
    ISO_3166_1_ALPHA_3_MEXICO : result := ISO_4217_MEXICAN_PESO;
    ISO_3166_1_ALPHA_3_NORWAY : result := ISO_4217_NORWEGIAN_KRONE ;
    ISO_3166_1_ALPHA_3_POLAND : result := ISO_4217_POLISH_ZLOTY ;
    ISO_3166_1_ALPHA_3_REPUBLIC_OF_KOREA : result := ISO_4217_SOUTH_KOREAN_WON ;
    ISO_3166_1_ALPHA_3_RUSSIAN_FEDERATION : result := ISO_4217_RUSSIAN_RUBLE ;
    ISO_3166_1_ALPHA_3_SWEDEN : result := ISO_4217_SWEDISH_KRONA ;
    ISO_3166_1_ALPHA_3_SWITZERLAND : result := ISO_4217_SWISS_FRANC ;
    ISO_3166_1_ALPHA_3_UNITED_KINGDOM_OF_GREAT_BRITAIN_AND_NORTHERN_IRELAND :
          result := ISO_4217_POUND_STERLING ;
    ISO_3166_1_ALPHA_3_UNITED_STATES_OF_AMERICA :
          result := ISO_4217_UNITED_STATES_DOLLAR ;
  end;
end;

Case Set

If you want to use Sets, this example is very clear:

{ COMMENTS TO edgarrod71@gmail.com }
 type
    medals = (Gold, Silver, Bronze);  // in case you want Gold to Start from 1, put this (Gold=1, ...but
    SMedals = array [medals] of string[6] = ('Gold', 'Silver', 'Bronze'); // in [medals], you have to change it to [Gold..Bronze] ;)
 {[ ... ]}
var
  place: integer;  /// place starts from 0;
 {[ ... ]}

  case medals(place) of
      Gold:   ShowMessage(SMedals[medals(place)] + ' medal');
      Silver: ShowMessage(SMedals[medals(place)] + ' medal');
      Bronze: ShowMessage(SMedals[medals(place)] + ' medal');
  else
      ShowMessage('Better luck next time! :)'); 
  end;


Hexadecimal input

If hexadecimal number are used, it is considered that they are unsigned:

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


If a hexadecimal is a different kind of a variable, for example Shortint, values shall be cast:

 case place of
   Shortint($C3): ShowMessage('Gold medal');  //C3= -61;
   Shortint($34): ShowMessage('Silver medal');
   Shortint($58): ShowMessage('Bronze medal'); 
   else ShowMessage('Better luck next time'); 
 end;

or another solution can be used:

var
  myplace : ShortInt;
  place   : Byte absolute myplace;
begin
   myplace:=-61;
   case place of
     $C3: ShowMessage('Gold medal');
     $34: ShowMessage('Silver medal');
     $58: ShowMessage('Bronze medal');
   else ShowMessage('Better luck next time');
   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
     celsius : (celsius_value : ScaleCelsius);
     kelvin : (kelvin_value : ScaleKelvin);
   end;

See also