Although, we can use the if statement for very complex (nested) condition testing, the case statement is usually easier to read (debug!) and the code runs more quickly.
The case statement makes it clear that a program has reached a point with many branches; multiple if-then statements do not.
Sintax :
Case <parameters> of
<number_1>:<execute_1>
. . .
. . .
. . .
<number_n>:<execute_n>
else < execute_n+1 >;examples :
begin
case days of
1 : 'Monday';
2 : 'Tuesday';
3 : 'Wednesday';
4 : 'Thursday';
5 : 'Friday';
6 : 'Saturday';
7 : 'Sunday';
end;
end;
What follows the case keyword is usually called the selector. The selector is a variable or expression taken from either the char type or any integer type (an ordinal type). String type are invalid!. However, the StringToCaseSelect custom function enables you to use the Case statement with string type variables
As you can see, the individual case statements use a single constant, a group of constants (separated by comma), or a range of constants (double dot separated). We can even add an else keyword to take care of all the remaining cases at once.
Note 1: Only one case statement will be executed, we cannot have overlapping conditions in the case statements.
Note 2: If you want to include more than one statement in the part following the colon (:), place the begin and end keywords around the multiple statements.
examples case statements usage
No comments:
Post a Comment