Sign in Join
Wiki copytemporary

Revision 1 of 2, by FPC user on 10-17-2023. The page as it is now.

Case

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

structure

The general structure of case-clauses looks like this:

pascal
case selector of
    caseValue0: <statement>;
    caseValue1,caseValue7: <statement>;
    caseValue10..caseValue20: <statement>;
    else <statements>;
end;

The following constraints have to be met:

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.

pascal
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.

pascal
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


From the Free Pascal wiki, page "Case", as it stood on 2023-10-17 after a change by FPC user, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.