Difference between revisions of "Case"

From Free Pascal wiki
Jump to navigationJump to search
m (→‎case-statements: remove invisible blank)
m (→‎see also: corrected the link to the case statement page as it was incorrectly pointing to the if statement page in the FreePascal reference manual.)
(3 intermediate revisions by one other user not shown)
Line 1: Line 1:
 
{{Case}}
 
{{Case}}
  
The [[Reserved word|reserved word]] <syntaxhighlight lang="pascal" enclose="none">case</syntaxhighlight> starts a clause where alternatives are chosen.
+
The [[Reserved word|reserved word]] <syntaxhighlight lang="pascal" inline>case</syntaxhighlight> starts a clause where alternatives are chosen.
  
 
== structure ==
 
== structure ==
The general structure of <syntaxhighlight lang="pascal" enclose="none">case</syntaxhighlight>-clauses looks like this:
+
The general structure of <syntaxhighlight lang="pascal" inline>case</syntaxhighlight>-clauses looks like this:
 
<syntaxhighlight lang="pascal">
 
<syntaxhighlight lang="pascal">
 
case selector of
 
case selector of
Line 13: Line 13:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
The following constraints have to be met:
 
The following constraints have to be met:
* The data type of <syntaxhighlight lang="pascal" enclose="none">selector</syntaxhighlight> has to be an ordinal type. [[FPC|FreePascal]] additionally allows [[Character and string types|strings]].
+
* 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" enclose="none">selector</syntaxhighlight> is.
+
* 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.
 
* They have to be constant expressions, i.e. known at compile-time.
* All <syntaxhighlight lang="pascal" enclose="none">case</syntaxhighlight>-labels have to be mutually disjoint. For every discrete value there applies exactly one or no cases.
+
* 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" enclose="none">case</syntaxhighlight>-clause has to have at least one <syntaxhighlight lang="pascal" enclose="none">case</syntaxhighlight>-label. Imperative <syntaxhighlight lang="pascal" enclose="none">case</syntaxhighlight>-statements can have at most one anonymous cases (at most one <syntaxhighlight lang="pascal" enclose="none">else</syntaxhighlight>/<syntaxhighlight lang="delphi" enclose="none">otherwise</syntaxhighlight>-branches).
+
* 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" enclose="none">case</syntaxhighlight>-labels is not relevant.
+
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.
 
They may be ascending, descending, or mixed up, it does not matter.
  
Line 25: Line 25:
 
This is no longer the case with modern compilers.
 
This is no longer the case with modern compilers.
  
<syntaxhighlight lang="pascal" enclose="none">case</syntaxhighlight> can have both, imperative and declarative meanings, depending on where it is written.
+
<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.
 
The former are “statements”, whereas the latter start a “variant part” of records.
  
=== <syntaxhighlight lang="pascal" enclose="none">case</syntaxhighlight>-statements ===
+
=== <syntaxhighlight lang="pascal" inline>case</syntaxhighlight>-statements ===
<syntaxhighlight lang="pascal" enclose="none">case</syntaxhighlight>-statements are a concise way of writing branches.
+
<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 suit best, where alternative paths are taken ''exclusively''.
They may contain an [[Else|<syntaxhighlight lang="pascal" enclose="none">else</syntaxhighlight>]]-branch catching all cases that are not listed.
+
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);
 
<syntaxhighlight lang="pascal" line highlight="8-20">program asciiTest(input, output, stderr);
  
Line 53: Line 53:
 
end;
 
end;
 
end.</syntaxhighlight>
 
end.</syntaxhighlight>
Note, <syntaxhighlight lang="pascal" enclose="none">case</syntaxhighlight>-statements accept ''ordinal'' expressions as selector.
+
Note, <syntaxhighlight lang="pascal" inline>case</syntaxhighlight>-statements accept expressions as selector.
In Delphi and FPC strings can be compared, too.
 
 
<!-- https://bugs.freepascal.org/view.php?id=34091 Starting in FPC version X.Y.Z records are comparable as well. -->
 
<!-- https://bugs.freepascal.org/view.php?id=34091 Starting in FPC version X.Y.Z records are comparable as well. -->
  
Instead of writing <syntaxhighlight lang="pascal" enclose="none">else</syntaxhighlight> the word <syntaxhighlight lang="pascal" enclose="none">otherwise</syntaxhighlight> is allowed, too.
+
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.
 
This is an [[Extended Pascal]] extension.
  
While the same semantics can be achieved by consecutive [[If|<syntaxhighlight lang="pascal" enclose="none">if</syntaxhighlight>]]-[[Then|<syntaxhighlight lang="pascal" enclose="none">then</syntaxhighlight>]]-branches, utilizing a <syntaxhighlight lang="pascal" enclose="none">case</syntaxhighlight>-statement allows the code generator to optimize the branch selection.
+
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]].
  
 
==== comparative remarks ====
 
==== comparative remarks ====
 
There is no “fall-through” as it is the case with other languages such as shell or C.
 
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" enclose="none">end</syntaxhighlight>]] of the <syntaxhighlight lang="pascal" enclose="none">case</syntaxhighlight>-statement.
+
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" enclose="none">break</syntaxhighlight>-statement]] with its special meaning only appears in loops.
+
The [[Break|<syntaxhighlight lang="pascal" inline>break</syntaxhighlight>-statement]] with its special meaning only appears in loops.
  
 
=== variant part in records ===
 
=== variant part in records ===
A [[Record|<syntaxhighlight lang="pascal" enclose="none">record</syntaxhighlight>]] may contain a variant part.
+
A [[Record|<syntaxhighlight lang="pascal" inline>record</syntaxhighlight>]] may contain a variant part.
The <syntaxhighlight lang="pascal" enclose="none">case</syntaxhighlight>-selector has to be the name of a data type, but an identifier for accessing the current variant can be provided, too.
+
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 lang="pascal" line highlight="12-18">program variantRecordDemo(input, output, stderr);
  
Line 94: Line 93:
  
 
== see also ==
 
== see also ==
* [https://www.freepascal.org/docs-html/ref/refsu56.html “The <syntaxhighlight lang="pascal" enclose="none">case</syntaxhighlight> statement” in the “FreePascal reference manual”]
+
* [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" enclose="none">case</syntaxhighlight>” as part of the “Object Pascal introduction”]]
+
* [[CASE|“<syntaxhighlight lang="pascal" inline>case</syntaxhighlight>” as part of the “Object Pascal introduction”]]
  
 
== external references ==
 
== external references ==
 
* [https://lazarus-ccr.sourceforge.io/pascal/pas3cb.html Tao Yue: “Learn Pascal!”. Chapter “case”]
 
* [https://lazarus-ccr.sourceforge.io/pascal/pas3cb.html Tao Yue: “Learn Pascal!”. Chapter “case”]
  
 
+
[[Category: Code]]
[[Category:Pascal]]
 
[[Category:Control Structures]]
 
[[Category:Code]]
 

Revision as of 22:42, 28 July 2020

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