Multi-value Conditional Judgment (case Statement)

Multi-branch Judgment with Single Expression

If you need to execute different logic based on distinct values of the same expression, the case .. of .. end statement is highly suitable.

case SomeValue of
  0: DoSomething;
  1: DoSomethingElse;
  2: begin
       IfItsTwoThenDoThis;
       AndAlsoDoThis;
     end;
  3..10: DoSomethingInCaseItsInThisRange;
  11, 21, 31: AndDoSomethingForTheseSpecialValues;
else
  DoSomethingInCaseOfUnexpectedValue;
end;Code language: JavaScript (javascript)

The else branch is optional, equivalent to default inside the switch statement of C-family languages.

If no branches match and no else is written, no code will be executed at all.

If you are familiar with C-style languages and compare with the switch statement, you will find that Pascal’s case has no automatic fall-through. This is a deliberate design advantage of Pascal. You do not need to manually add break statements; at most one case branch will run each time, and the entire case block exits immediately after execution.

5 Valid Matching Syntax Forms

case num of
  5: Writeln('Equals 5');                  // Single value
  1,3,5: Writeln('Matches one of 1, 3, 5');    // Multiple values separated by commas
  10..20: Writeln('Range from 10 to 20');        // Continuous numeric range
  'a'..'z': Writeln('Lowercase letters');        // Character ranges are also supported
else
  Writeln('No matching value');
end;Code language: JavaScript (javascript)

Multi-statement Branches Must Be Wrapped with begin..end

case n of
  2: begin
    Writeln('Branch 2');
    Writeln('Execute multiple lines of code');
  end;
end;Code language: JavaScript (javascript)

The following example accepts an ASCII number between 1 and 128 and classifies the corresponding ASCII character based on the input value.

{$ifdef FPC} {$mode objfpc} {$H+} {$endif}
{$codepage utf8}
program AsciiJudge;

// Wrapped procedure: pass ASCII value 0-128 directly to judge its category
procedure JudgeAsciiCode(code: Integer);
begin
  case code of
    // 1. Single value match + multi-line begin end (Enter ASCII=13)
    13: begin
      Writeln('======== Special Enter Key ========');
      Writeln('ASCII Code: 13 Carriage Return Control Character');
      Writeln('=====================================');
    end;

    // 2. Multiple discrete values separated by commas: all common control codes 0-12, 14-31
    0,1,2,3,4,5,6,7,8,9,10,11,12,14,15..31:
      Writeln('Type: Control Character');

    // 3. Range match: Digits 0~9 ASCII 48~57
    48..57: Writeln('Type: Digit 0-9');

    // Range: Uppercase letters A-Z
    65..90: Writeln('Type: Uppercase English Letters A-Z');
    // Range: Lowercase letters a-z
    97..122: Writeln('Type: Lowercase English Letters a-z');

    // 4. else default branch + multi-line begin end (Symbols & Punctuation)
  else
    begin
      Writeln('Type: Punctuation / Symbol');
      Writeln('Input ASCII Code: ', code);
    end;
  end;
end;

var
  asciiNum: Integer;
begin
  Write('Enter an ASCII number between 0 and 128: ');
  ReadLn(asciiNum);

  // Restrict input range
  if (asciiNum >= 0) and (asciiNum <= 128) then
    JudgeAsciiCode(asciiNum)
  else
    Writeln('Input out of range 0~128, invalid!');

  ReadLn;
end.Code language: PHP (php)

Readers may refer to the following article to learn more about ASCII codes: ASCII table – Table of ASCII codes, characters and symbols

Multi-value Conditional Judgment (case Statement)

Leave a Reply

Your email address will not be published. Required fields are marked *