Difference between revisions of "Case"

From Free Pascal wiki
Jump to navigationJump to search
(Example -> Examples)
m (reword sentence)
Line 3: Line 3:
 
The reserved word '''Case''' begins a ''case statement''. The case statement compares the value of either an enumerated type, an ordinal expression or a string, to each selector. The selector can be a [[Const|constant]], a subrange, or a list separated by [[Comma|commas]]. The selector field is separated from its related action(s) by a [[Colon]].
 
The reserved word '''Case''' begins a ''case statement''. The case statement compares the value of either an enumerated type, an ordinal expression or a string, to each selector. The selector can be a [[Const|constant]], a subrange, or a list separated by [[Comma|commas]]. The selector field is separated from its related action(s) by a [[Colon]].
  
The case statement also includes the [[Reserved word|reserved words]] [[Of]] and [[End]]. [[Else]] can be used if needed, when there is the possibility of no matches.
+
The case statement also includes the [[Reserved word|reserved words]] [[Of]] and [[End]]. [[Else]] can be used when there is the possibility of no matches.
  
  

Revision as of 02:10, 30 August 2017

Deutsch (de) English (en) español (es) suomi (fi) français (fr) русский (ru)

The reserved word Case begins a case statement. The case statement compares the value of either an enumerated type, an ordinal expression or a string, to each selector. The selector can be a constant, a subrange, or a list separated by commas. The selector field is separated from its related action(s) by a Colon.

The case statement also includes the reserved words Of and End. Else can be used when there is the possibility of no matches.



Examples

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

The Free 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 Enumerator

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

 type
    TMedal = (mGold, mSilver, mBronze);  // in case you want Gold to Start from 1, put this (Gold=1, ...but
 const
    MedalNames: array [TMedal] of string[6] = ('Gold', 'Silver', 'Bronze'); // in [TMedal], you have to change it to [Gold..Bronze] ;)  suggestions to edgarrod71@gmail.com
 {[ ... ]}
var
  place: integer;  /// place starts from 0;
  M: TMedal;
 {[ ... ]}
  M := TMedal(place);
  case M of
      mGold:   ShowMessage(Medalnames[M] + ' medal');
      mSilver: ShowMessage(Medalnames[M] + ' medal');
      mBronze: ShowMessage(Medalnames[M] + ' medal');
  else
      ShowMessage('Better luck next time! :)'); 
  end;

Another example without MedalNames array...

 type
    TMedal = (Gold, Silver, Bronze);  // avoid the "m"
 {[ ... ]}
var
  place: integer;  /// place starts from 0;
  M: TMedal;
  S: string;
 {[ ... ]}
  M := TMedal(place);
  case M of
      Gold:   writeStr(S, M);
      Silver: writeStr(S, M);
      Bronze: writeStr(S, M);
  else
      S := 'Better luck next time! ;)'; 
  end;
  ShowMessage(S);


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