Case

From Free Pascal wiki
Jump to navigationJump to search

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: <statement>;
    caseValue1,caseValue7: <statement>;
    caseValue10..caseValue20: <statement>;
    else <statements>;
end;

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.

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.

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.

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.

see also

external references