Difference between revisions of "Case"

From Lazarus wiki
Jump to navigationJump to search
(Grammar, fixed code inconsistencies)
(37 intermediate revisions by 11 users not shown)
Line 1: Line 1:
 
{{Case}}
 
{{Case}}
  
Case opens a case statement. The case statement compares the value of an ordinal expression to each selector, which can be a [[Const|constant]], a subrange, or a list of them separated by [[Comma|commas]]. The selector field is separated from action field by [[Colon]].
+
The [[Reserved word|reserved word]] <syntaxhighlight lang="pascal" inline>case</syntaxhighlight> starts a clause where alternatives are chosen.
  
The case statement includes the [[Reserved word|reserved words]] [[Of]] and [[End]]. [[Else]] can be used if needed, too.
+
== structure ==
 +
The general structure of <syntaxhighlight lang="pascal" inline>case</syntaxhighlight>-clauses looks like this:
 +
<syntaxhighlight lang="pascal">
 +
case selector of
 +
caseValue0: …;
 +
caseValue1: …;
 +
caseValue2, caseValue3: …;
 +
caseValue4..caseValue7: …;
 +
</syntaxhighlight>
 +
The following constraints have to be met:
 +
* The data type of <syntaxhighlight lang="pascal" inline>selector</syntaxhighlight> has to be an ordinal type. [[FPC|FreePascal]] additionally allows [[Character and string types|strings]].
 +
* All case values have to be the same data type as <syntaxhighlight lang="pascal" inline>selector</syntaxhighlight> is.
 +
* They have to be constant expressions, i.e. known at compile-time.
 +
* All <syntaxhighlight lang="pascal" inline>case</syntaxhighlight>-labels have to be mutually disjoint. For every discrete value there applies exactly one or no cases.
 +
* A <syntaxhighlight lang="pascal" inline>case</syntaxhighlight>-clause has to have at least one <syntaxhighlight lang="pascal" inline>case</syntaxhighlight>-label. Imperative <syntaxhighlight lang="pascal" inline>case</syntaxhighlight>-statements can have at most one anonymous cases (at most one <syntaxhighlight lang="pascal" inline>else</syntaxhighlight>/<syntaxhighlight lang="delphi" inline>otherwise</syntaxhighlight>-branches).
  
 +
The actual order of <syntaxhighlight lang="pascal" inline>case</syntaxhighlight>-labels is not relevant.
 +
They may be ascending, descending, or mixed up, it does not matter.
  
<syntaxhighlight>
+
Originally, standard Pascal also set the constraint, that ''all possible'' values of the selector have to have a corresponding match.
case place of
+
This is no longer the case with modern compilers.
  1: ShowMessage('Gold medal');
+
 
  2: ShowMessage('Silver medal');
+
<syntaxhighlight lang="pascal" inline>case</syntaxhighlight> can have both, imperative and declarative meanings, depending on where it is written.
  3: ShowMessage('Bronze medal');  
+
The former are “statements”, whereas the latter start a “variant part” of records.
  else ShowMessage('Better luck next time');  
+
 
end;
+
=== <syntaxhighlight lang="pascal" inline>case</syntaxhighlight>-statements ===
</syntaxhighlight>
+
<syntaxhighlight lang="pascal" inline>case</syntaxhighlight>-statements are a concise way of writing [[Branch|branches]].
 +
They suit best, where alternative paths are taken ''exclusively''.
 +
They may contain an [[Else|<syntaxhighlight lang="pascal" inline>else</syntaxhighlight>]]-branch catching all cases that are not listed.
 +
<syntaxhighlight lang="pascal" line highlight="8-20">program asciiTest(input, output, stderr);
 +
 
 +
var
 +
c: char;
 +
 
 +
begin
 +
read(c);
 +
case ord(c) of
 +
// empty statement, so the control characters are not
 +
// considered by the else-branch as non-ASCII-characters
 +
0..$1F, $7F: ;
 +
$20..$7E:
 +
begin
 +
writeLn('You entered an ASCII printable character.');
 +
end;
 +
else
 +
begin
 +
writeLn('You entered a non-ASCII character.');
 +
end;
 +
end;
 +
end.</syntaxhighlight>
 +
Note, <syntaxhighlight lang="pascal" inline>case</syntaxhighlight>-statements accept expressions as selector.
 +
<!-- https://bugs.freepascal.org/view.php?id=34091 Starting in FPC version X.Y.Z records are comparable as well. -->
  
=== WhatIsChar ===
+
Instead of writing <syntaxhighlight lang="pascal" inline>else</syntaxhighlight> the word <syntaxhighlight lang="pascal" inline>otherwise</syntaxhighlight> is allowed, too.
 +
This is an [[Extended Pascal]] extension.
  
<syntaxhighlight>
+
While the same semantics can be achieved by consecutive [[If|<syntaxhighlight lang="pascal" inline>if</syntaxhighlight>]]-[[Then|<syntaxhighlight lang="pascal" inline>then</syntaxhighlight>]]-branches, utilizing a [[Case Compiler Optimization|<syntaxhighlight lang="pascal" inline>case</syntaxhighlight>-statement allows the code generator to optimize the branch selection]].
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;
 
</syntaxhighlight>
 
  
== Variant Record ==
+
==== comparative remarks ====
 +
There is no “fall-through” as it is the case with other languages such as shell or C.
 +
In Pascal exactly one case matches, is processed, and program flow continues after the final [[End|<syntaxhighlight lang="pascal" inline>end</syntaxhighlight>]] of the <syntaxhighlight lang="pascal" inline>case</syntaxhighlight>-statement.
 +
The [[Break|<syntaxhighlight lang="pascal" inline>break</syntaxhighlight>-statement]] with its special meaning only appears in loops.
  
Case is used in Variant [[Record]], too. A Variant Record is also called a tagged union.
+
=== variant part in records ===
 +
A [[Record|<syntaxhighlight lang="pascal" inline>record</syntaxhighlight>]] may contain a variant part.
 +
The <syntaxhighlight lang="pascal" inline>case</syntaxhighlight>-selector has to be the name of a data type, but an identifier for accessing the current variant can be provided, too.
 +
<syntaxhighlight lang="pascal" line highlight="12-18">program variantRecordDemo(input, output, stderr);
  
<syntaxhighlight>
+
type
  type
+
sex = (female, male);
     
+
clothingSize = record
  ScaleKelvin = 223 .. 323;
+
// FIXED PART
  ScaleCelsius = -50 .. 50;
+
shoulderWidth: word;
   
+
armLength: word;
  TemperatureScale = ( celsius, kelvin ) ;
+
bustGirth: word;
  Temperature = record
+
waistSize: word;
    case scale :   TemperatureScale of
+
hipMeasurement: word;
    celcius : (celsius_value : ScaleCelsius);
+
// VARIABLE PART
    kelvin : (kelvin_value : ScaleKelvin);
+
case body: sex of
  end;
+
female: (
 +
underbustMeasure: word;
 +
);
 +
male: (
 +
);
 +
end;
 +
begin
 +
end.
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== See also ==
+
== see also ==
* [http://www.freepascal.org/docs-html/ref/refsu40.html#x110-11700010.2.2 The Case statement]
+
* [https://www.freepascal.org/docs-html/ref/refsu55.html “The <syntaxhighlight lang="pascal" inline>case</syntaxhighlight> statement” in the “FreePascal reference manual”]
* [http://www.freepascal.org/docs-html/ref/refsu15.html#x38-450003.3.2  Record types]
+
* [[Basic Pascal Tutorial/Chapter 3/CASE|“<syntaxhighlight lang="pascal" inline>case</syntaxhighlight>” as part of the “Object Pascal introduction”]]
* [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]
+
== external references ==
* [[Dialog_Examples#ShowMessage|ShowMessage]]
+
* [https://lazarus-ccr.sourceforge.io/pascal/pas3cb.html Tao Yue: “Learn Pascal!. Chapter “case”]
* [[Type]]
+
 
 +
[[Category: Code]]

Revision as of 06:07, 25 January 2023

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

The reserved word case starts a clause where alternatives are chosen.

structure

The general structure of case-clauses looks like this:

case selector of
	caseValue0: ;
	caseValue1: ;
	caseValue2, caseValue3: ;
	caseValue4..caseValue7: ;

The following constraints have to be met:

  • The data type of selector has to be an ordinal type. FreePascal additionally allows strings.
  • All case values have to be the same data type as selector is.
  • They have to be constant expressions, i.e. known at compile-time.
  • All case-labels have to be mutually disjoint. For every discrete value there applies exactly one or no cases.
  • A case-clause has to have at least one case-label. Imperative case-statements can have at most one anonymous cases (at most one else/otherwise-branches).

The actual order of case-labels is not relevant. They may be ascending, descending, or mixed up, it does not matter.

Originally, standard Pascal also set the constraint, that all possible values of the selector have to have a corresponding match. This is no longer the case with modern compilers.

case can have both, imperative and declarative meanings, depending on where it is written. The former are “statements”, whereas the latter start a “variant part” of records.

case-statements

case-statements are a concise way of writing branches. They suit best, where alternative paths are taken exclusively. They may contain an else-branch catching all cases that are not listed.

 1program asciiTest(input, output, stderr);
 2
 3var
 4	c: char;
 5
 6begin
 7	read(c);
 8	case ord(c) of
 9		// empty statement, so the control characters are not
10		// considered by the else-branch as non-ASCII-characters
11		0..$1F, $7F: ;
12		$20..$7E:
13		begin
14			writeLn('You entered an ASCII printable character.');
15		end;
16		else
17		begin
18			writeLn('You entered a non-ASCII character.');
19		end;
20	end;
21end.

Note, case-statements accept expressions as selector.

Instead of writing else the word otherwise is allowed, too. This is an Extended Pascal extension.

While the same semantics can be achieved by consecutive if-then-branches, utilizing a case-statement allows the code generator to optimize the branch selection.

comparative remarks

There is no “fall-through” as it is the case with other languages such as shell or C. In Pascal exactly one case matches, is processed, and program flow continues after the final end of the case-statement. The break-statement with its special meaning only appears in loops.

variant part in records

A record may contain a variant part. The case-selector has to be the name of a data type, but an identifier for accessing the current variant can be provided, too.

 1program variantRecordDemo(input, output, stderr);
 2
 3type
 4	sex = (female, male);
 5	clothingSize = record
 6			// FIXED PART
 7			shoulderWidth: word;
 8			armLength: word;
 9			bustGirth: word;
10			waistSize: word;
11			hipMeasurement: word;
12			// VARIABLE PART
13			case body: sex of
14				female: (
15					underbustMeasure: word;
16				);
17				male: (
18				);
19		end;
20begin
21end.

see also

external references