Description |
The Case keyword provides a structured equivalent to a sequence of if statements on the same variable.
The Case statement is more elegant, more efficient, and easier to maintain than multiple if nestings.
Version 2.
Is used for Records declarations. It is then called a Variant. It provides a means of mapping two or more differing sets of declarations onto the same section of the record.
It is mostly used when handling different types of dataset for a record, where the datasets have mostly the same content. See the example for clarification.
The Tag provides identification of the case element.
|
|
Related commands |
Else |
|
Starts false section of if, case and try statements |
End |
|
Keyword that terminates statement blocks |
If |
|
Starts a conditional expression to determine what to do next |
Packed |
|
Compacts complex data types into minimal storage |
Record |
|
A structured data type - holding fields of data |
Then |
|
Part of an if statement - starts the true clause |
|
|
|
Example code : Standard case statement usage |
var
colour : TPrimary;
number : Integer;
begin // Show the colour before it has an assigned value
ShowColour(colour);
// Now set the colour and try again
colour := Green;
ShowColour(colour);
// Calculations can also be used in the case statement
number := 17;
Case number mod 2 of
0 : ShowMessage(IntToStr(Number)+' mod 2 = 0');
1 : ShowMessage(IntToStr(Number)+' mod 2 = 1');
else ShowMessage(IntToStr(Number)+' mod 2 is unknown');
end;
end;
// Procedure to show the colour of a passed
procedure TForm1.ShowColour(colour : TPrimary);
begin // Use a case statement to see the colour of the passed var // Note how important the else clause is, even though we have // apparently covered all TPrimary values!
Case colour of
Red : ShowMessage('The colour is Red');
Green : ShowMessage('The colour is Green');
Blue : ShowMessage('The colour is Blue');
Yellow : ShowMessage('The colour is Yellow');
else ShowMessage('The colour is Unknown!');
end;
end;
|
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;
// 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"
|
|