Case Statements with String Selectors

Introduction

Delphi 2007 and earlier versions of Pascal require the case statement selector to be an ordinal such as an integer. See below demonstrations of the convenient string case selectors in Lazarus, Smart Pascal and Oxygene.

Case-string-of Selection in Lazarus

program CaseStringOf;

var
  direction: string;

begin
  write('Please enter North, South, East or West: ');
  readln(direction);
  direction := lowerCase(direction);
  case direction of
    'north': writeln(#13#10#24);  //new line and up arrow
    'south': writeln(#13#10#25);
    'east' : writeln(#13#10#32#26);//new line, space and right arrow
    'west' : writeln(#13#10#32#27);
  end;
  readln;
end.    

On a Raspberry Pi, use instead the code below.

program CaseStringOf;

var
  direction: string;

begin
  write('Please enter North, South, East or West: ');
  readln(direction);
  direction := lowerCase(direction);
  case direction of
    'north': writeln('North entered');
    'south': writeln('South entered');
    'east' : writeln('East entered');
    'west' : writeln('West entered');
  end;
  readln;
end.    

Case-string-of Selection in Smart Pascal

unit CaseStringOf;

interface

uses 
  W3System, W3Scroll, W3Console, W3Components, W3Application, W3ConsoleApp, W3Lists;

type
  TApplication = class(TW3CustomConsoleApplication)
  protected
    procedure PopulateConsole; override;
    procedure ProcessCommand(aCommand: string); override;
  end;

implementation

procedure TApplication.PopulateConsole;
begin
  Console.WriteLn('Please enter North, South, East or West: ' + chr(24));
  Console.writeln(chr(24));
end;
 
procedure TApplication.ProcessCommand(aCommand: string);
var
  direction: string;

begin
  direction := lowerCase(aCommand);
  case direction of
    'north': Console.writeln('<h2>'+chr($2191)+'</h2>');  //up arrow within h2 tag
    'south': Console.writeln('<h2>'+chr($2193)+'</h2>');
    'east' : Console.writeln('<h2>'+chr($2192)+'</h2>');
    'west' : Console.writeln('<h2>'+chr($2190)+'</h2>');
  end;
end;

end.
    

Case-string-of Selection in Oxygene for Java

namespace CaseStringOf;

interface

uses
  java.util;

implementation
var
  input: array[0..6] of SByte;
  direction: String;
begin
  write('Please enter North, South, East or West: ');
  System.in.read(input);
  direction := new String(input);
  direction := direction.trim.toLowerCase;
  case direction of
    'north': writeln(#13#10#24);  //new line and up arrow
    'south': writeln(#13#10#25);
    'east' : writeln(#13#10#32#26); //new line, space and right arrow
    'west' : writeln(#13#10#32#27);
  end;
  system.in.read;
end.

 

Programming - a skill for life!

Developments in Pascal, Oxygen for Java and Smart Mobile Studio