0% found this document useful (0 votes)
604 views

Delphi Basics - Case Command

The case command provides a structured way to execute different code blocks based on the value of an expression. It can be used with records to select different field declarations based on a tag. Example code shows using case with integers, records, and within record definitions.

Uploaded by

nadut
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
604 views

Delphi Basics - Case Command

The case command provides a structured way to execute different code blocks based on the value of an expression. It can be used with records to select different field declarations based on a tag. Example code shows using case with integers, records, and within record definitions.

Uploaded by

nadut
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

26.08.

2017 Delphi Basics : Case command

DelphiBasics
Case A mechanism for acting upon different values of an Ordinal
Keyword

1 case Ordinal expression of


Ordinal value {,Ordinal value...} : Statement;
{...}
else Statement;
end;
2 type Name = record
Declarations ...
case {Tag :} Ordinal type of
Ordinal value {,Ordinal value...} : (Declarations);
{...}
end;

Description Example code : Standard case statement usage

The Case keyword provides a structured equivalent to a var


sequence of if statements on the same variable. colour : TPrimary;
number : Integer;
The Case statement is more elegant, more efficient, and
easier to maintain than multiple if nestings.
begin
Version 2. Is used for Records declarations. It is then called a // Show the colour before it has an assigned value
Variant. It provides a means of mapping two or more differing ShowColour(colour);
sets of declarations onto the same section of the record.
// Now set the colour and try again
It is mostly used when handling different types of dataset for a colour := Green;
record, where the datasets have mostly the same content. See
ShowColour(colour);
the example for clarification.

The Tag provides identification of the case element. // Calculations can also be used in the case statement
number := 17;
Case number mod 2 of
Related commands 0 : ShowMessage(IntToStr(Number)+' mod 2 = 0');
1 : ShowMessage(IntToStr(Number)+' mod 2 = 1');
Else Starts false section of if, case and try statements else ShowMessage(IntToStr(Number)+' mod 2 is unknown');
End Keyword that terminates statement blocks end;
If Starts a conditional expression to determine what to end;
do next
Packed Compacts complex data types into minimal storage
Record A structured data type - holding fields of data // Procedure to show the colour of a passed
Then Part of an if statement - starts the true clause procedure TForm1.ShowColour(colour : TPrimary);
begin
// Use a case statement to see the colour of the passed var
Author links // Note how important the else clause is, even though we have
// apparently covered all TPrimary values!
Case colour of
Buy Website Traffic at Red : ShowMessage('The colour is Red');
Buywebsitetrafficexperts.com
Green : ShowMessage('The colour is Green');
Buy Proxies at Blue : ShowMessage('The colour is Blue');
Buyproxies.io Yellow : ShowMessage('The colour is Yellow');
else ShowMessage('The colour is Unknown!');
end;
end;
Download this web site as a Windows program.
Show full unit code

The colour is Unknown!


The colour is Green
17 mod 2 is 1

Example code : Case within a record

type
// Declare a fruit record using case to choose the
// diameter of a round fruit, or length and height ohterwise.
TFruit = record
name : string[20];
Case isRound : Boolean of // Choose how to map the next section
True :
(diameter : Single); // Maps to same storage as length
False :
(length : Single; // Maps to same storage as diameter
width : Single);
end;

var
apple, banana, fruit : TFruit;

begin
// Set up the apple as round, with appropriate dimensions
apple.name := 'Apple';
apple.isRound := True;
apple.diameter := 3.2;

// Set up the banana as long, with appropriate dimensions


banana.name := 'Banana';
banana.isRound := False;
banana.length := 7.65;
banana.width := 1.3;
http://www.delphibasics.co.uk/RTL.asp?Name=Case 1/2
26.08.2017 Delphi Basics : Case command

// Show the attributes of the apple


fruit := apple;
if fruit.isRound
then ShowMessage(fruit.name +' diameter = '+
FloatToStrF(fruit.diameter, ffFixed, 2, 1)+'"')
else ShowMessage(fruit.name +' length = '+
FloatToStrF(fruit.length, ffFixed, 2, 1)+'" width = '+
FloatToStrF(fruit.width, ffFixed, 2, 1)+'"');

// Show the attributes of the banana


fruit := banana;
if fruit.isRound
then ShowMessage(fruit.name +' diameter = '+
FloatToStrF(fruit.diameter, ffFixed, 2, 1)+'"')
else ShowMessage(fruit.name +' length = '+
FloatToStrF(fruit.length, ffFixed, 2, 1)+'" width = '+
FloatToStrF(fruit.width, ffFixed, 2, 1)+'"');
end;
Show full unit code

Apple diameter = 3.2"


Banana length = 7.7" width = 1.3"

Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved. | Home Page

http://www.delphibasics.co.uk/RTL.asp?Name=Case 2/2

You might also like