Last Updated:

Delphi case construct

Delphi case construct

The "caseof" construct is a choice operator in the Delphi language and is used to execute a single action among several possible variants.

The selection operator has the following form:

case<selector>of
<list 1>: <instructions to perform>
<list 2>: <instructions to perform>
...
<listn>: <instructions to perform>
else<instructions to perform>
end;

The selector here is an expression whose meaning determines the further course of events in the program.

The lists show the possible options for the selector value. Depending on them, the necessary instructions are made. Lists can consist of a single value, several separated by commas, or a range of values.

A block with the word else is optional. The instructions in it are executed if the selector value is not found in the lists. The case statement can also be implemented using multiple if statements. The constructions below will be equivalent to each other.

Multiple nested conditional statements.

ifn = 1 then// a rather cumbersome construct for three possible executable operators
writeln('n = 1')
else
if n = 0 then
writeln('n = 0')
else
writeln('n > 1');

Single case statement

case n of
0 :writeln(‘n = 0’);
1 :writeln(‘n = 1’);
elsewriteln(‘n > 1’);
end;

Also, for each of the lists, several operators can be presented at once. They are in the begin-end construction.

An example of such a view:

var x: integer;
x := 0;
casexof
0, 1 : begin
writeln('x = 0');// there can be as many writeln('or' statements between the begin and end statements
you want);
writeln(‘x = 1’);
end;
2 :writeln(‘x = 2’);
elsewriteln(‘x > 2’);
end;

Inference:

x = 0
or
x = 1

Unlike TurboPascal, Delphi does not allow duplicate indexes in lists. Therefore, the entry below will be considered incorrect in this language.

var n: integer;
begin
n := 1;
case n of
1..5 : writeln(‘1..5’);
3 :writeln(‘3’); 3 is already in the range from 1 to 5, so this code is not compiled by
the end;
end.

Using case in practice

Case is applicable not only for integers, but also for any standard type.

Example of using the case statement with a range of integers

var n: integer;
begin
n := 1;
case n of 1..5: writeln ('1..5');// the range of
numbers is indicated by
6,7: writeln('6..7');// it is also possible to write list values separated by a
comma end;
end.

Inference:

1..5

Using a Case Operator with a Boolean Type

varb: boolean;
begin
b := false;
case b of
false: writeln (‘Result: false’);
true: writeln(‘Result: true’);
end;
end.

Inference:

‘Result: false’

Applying a case with an enumerable type

type TCol = (Red, Blue, Green, Yellow);
varcol: TCol;
begin
col := Blue;
case col of
Blue: writeln (‘Color is blue!’);
Green: writeln(‘Color is green!’);
Red: writeln(‘Color is red!’);
Yellow: writeln(‘Color is yellow!’);
end;
end.

Inference:

‘Result: false’

 

Case with strings in Delphi

 

In Delphi, the case construct cannot be used with variables of type string. The code below, unlike the Pascal language, will not compile in Delphi:

var n: integer;
s: string;
begin
n := 1;
s := ‘str’;
casesof // a compilation error occurs on this line due to the incompatibility of the 'str' : writeln('String One' types
);
‘str2’ :writeln(‘String Two’);
end;
end.

You can solve this problem by casting strings to an enumerable type. This can be done using the GetEnumValue function from the TypInfo module. An example of using string in a choice statement in Delphi:

uses
SysUtils, TypInfo;
type
TStrings = (black, blue, white, yellow); create an enumerable type
var
V :TStrings; declaring a variable of an enumerable type
s :string;
begin
s := ‘blue’;
v := TStrings(GetEnumValue(TypeInfo(TStrings), s));///converting a string to an enumerable type
casevof// selecting from values of the enumerable type
black: writeln('Color: black');
blue: writeln(‘Color: blue’);
white: writeln(‘Color: white’);
yellow: writeln(‘Color: yellow’);
else
writeln(‘Other color’);
end;
end.