CASE-ELSE statement

The CASE-ELSE statement is a conditional expression, which is very similar to IF-THEN-ELSE, except that it allows more than two choices of outcomes for the evaluation of the controlling expression. The syntax of the CASE-ELSE statement is as follows:

CASE <ExpressionToBeEvaluated> OF 
<Value Set 1> : <Action Statement 1>;
<Value Set 2> : <Action Statement 2>;
<Value Set 3> : <Action Statement 3>;
...
...
<Value Set n> : <Action Statement n>;
[ELSE <Action Statement n + 1>;
END;

The ExpressionToBeEvaluated must not be a record. The data type of the Value Set must be able to be automatically converted to the data type of the ExpressionToBeEvaluated. Each ValueSet must be an expression, a set of values, or a range of values. The following example illustrates a typical instance of a CASE-ELSE statement:

 CASE Customer."Salesperson Code" OF 
'2','5','9': Customer."Territory Code" := 'EAST';
'16'..'20': Customer."Territory Code" := 'WEST';
'N': Customer."Territory Code" := 'NORTH';
'27'..'38': Customer."Territory Code" := 'SOUTH';
ELSE Customer."Territory Code" := 'FOREIGN';
END;

In the preceding code example, we see several alternatives for the Value Set. The first line (EAST) Value Set contains a list of values. If "SalespersonCode" is equal to '2', '5', or '9', the value EAST will be assigned to Customer."Territory Code". The second line, (WEST) Value Set, is a range, any value from '16' through '20'. The third line, (NORTH) ValueSet, is just a single value ('N'). If we look through the standard NAV code, we will see that a single value is the most frequently used CASE structure in NAV. In the fourth line of our example (SOUTH), the Value Set is again a range ('27'..'38'). If nothing in any Value Set matches ExpressionToBeEvaluated, the ELSE clause will be executed which sets Customer."Territory Code" equal to 'FOREIGN'.

An example of an IF-THEN-ELSE statement equivalent to the preceding CASE-ELSE statement is as follows:

 IF Customer."Salesperson Code" IN ['2','5','9'] THEN  
Customer."Territory Code" := 'EAST'
ELSE IF Customer."Salesperson Code" IN ['16'..'20'] THEN
Customer."Territory Code" := 'WEST'
ELSE IF Customer."Salesperson Code" = 'N' THEN
Customer."Territory Code" := 'NORTH'
ELSE IF Customer."Salesperson Code" IN ['27'..'38'] THEN
Customer."Territory Code" := 'SOUTH'
ELSE Customer."Territory Code" := 'FOREIGN';

The following is a slightly less intuitive example of the CASE-ELSE statement. In this instance, ExpressionToBeEvaluated is a simple TRUE and the Value Set statements are all conditional expressions. The first line containing a Value Set expression that evaluates to TRUE will be the line whose Action Statement is executed. The rules of execution and flow in this instance are the same as in the previous example:

 CASE TRUE OF Salesline.Quantity < 0: 
BEGIN
CLEAR(Salesline."Line Discount %");
CredTot := CredTot - Salesline.Quantity;
END;
Salesline.Quantity > QtyBreak[1]:
Salesline."Line Discount %" := DiscLevel[1];
Salesline.Quantity > QtyBreak[2]:
Salesline."Line Discount %" := DiscLevel[2];
Salesline.Quantity > QtyBreak[3]:
Salesline."Line Discount %" := DiscLevel[3];
Salesline.Quantity > QtyBreak[4]:
Salesline."Line Discount %" := DiscLevel[4];
ELSE
CLEAR(Salesline."Line Discount %");
END;
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.103.77