Difference between revisions of "Case"

From Free Pascal wiki
Jump to navigationJump to search
(undo recent change by Vaal12: `end` is not part of the general structure of `case`-clauses, because there are no two `end`s in →‎variant part in records: but just one concluding the `record` clause)
(48 intermediate revisions by 16 users not shown)
Line 1: Line 1:
Case opens a case statement. The case statement compares the value of ordinal expression to each selector, which can be a [[Const|constant]], a subrange, or a list of them separated by [[Comma|commas]]. Selector field separated to action field by [[Colon]].
+
{{Case}}
  
The case statement include [[Reserved word|reserved words]] [[Of]] and [[End]] . Sometimes [[Else]]. too.
+
The [[Reserved word|reserved word]] <syntaxhighlight lang="pascal" inline>case</syntaxhighlight> starts a clause where alternatives are chosen.
  
 +
== 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).
  
<delphi>
+
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.
  
case place of
+
Originally, standard Pascal also set the constraint, that ''all possible'' values of the selector have to have a corresponding match.
  1: ShowMessage('Cold medal');
+
This is no longer the case with modern compilers.
  2: ShowMessage('Silver medal');
 
  3: ShowMessage('Bronze medal');
 
  else ShowMessage('Next time: Good luck');
 
end;
 
  
</delphi>
+
<syntaxhighlight lang="pascal" inline>case</syntaxhighlight> 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.
  
== Variant Record ==
+
=== <syntaxhighlight lang="pascal" inline>case</syntaxhighlight>-statements ===
 +
<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);
  
Case-word is used Variant [[Record]], too. Variant Record also called a tagged union.
+
var
 +
c: char;
  
<delphi>
+
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. -->
  
  type
+
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.
  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>
+
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]].
  
== Read more ==
+
==== 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.
  
* [[doc:/ref/refsu40.html#x110-11700010.2.2| The Case statement]]
+
=== variant part in records ===
* [[doc:/ref/refsu15.html#x38-450003.3.2|  Record types]]
+
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);
 +
 
 +
type
 +
sex = (female, male);
 +
clothingSize = record
 +
// FIXED PART
 +
shoulderWidth: word;
 +
armLength: word;
 +
bustGirth: word;
 +
waistSize: word;
 +
hipMeasurement: word;
 +
// VARIABLE PART
 +
case body: sex of
 +
female: (
 +
underbustMeasure: word;
 +
);
 +
male: (
 +
);
 +
end;
 +
begin
 +
end.
 +
</syntaxhighlight>
 +
 
 +
== see also ==
 +
* [https://www.freepascal.org/docs-html/ref/refsu55.html “The <syntaxhighlight lang="pascal" inline>case</syntaxhighlight> statement” in the “FreePascal reference manual”]
 +
* [[CASE|“<syntaxhighlight lang="pascal" inline>case</syntaxhighlight>” as part of the “Object Pascal introduction”]]
 +
 
 +
== external references ==
 +
* [https://lazarus-ccr.sourceforge.io/pascal/pas3cb.html Tao Yue: “Learn Pascal!”. Chapter “case”]
 +
 
 +
[[Category: Code]]

Revision as of 18:44, 3 January 2022

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