Thursday, July 23, 2009

IF THEN ELSE in Navision

IF THEN [ELSE]
A conditional statement is one type of control structure in C/AL.
Using Conditional Statements
By using a conditional statement, you can specify a condition and one or more commands that should be executed, according to whether the condition is evaluated as TRUE or FALSE. There are two types of conditional statements in C/AL:
1. IF THEN [ELSE], where there are two choices
2. CASE, where there are more than two choices.
IF THEN ELSE Statements
IF THEN ELSE statements have the following syntax.
IF (Condition) THEN (Statement1)
[
ELSE
(Statement2)
]
This means that if (Condition) is true, then (Statement1) is executed. If (Condition) is false, then (Statement2) is executed.
The square brackets around ELSE mean that this part of the statement is optional. The ELSE statement is used when different actions are executed, depending on the evaluation of (Condition).
You can build even more complex control structures by nesting IF THEN ELSE statements. The following example is a typical IF THEN ELSE statement.
IF (Condition1) THEN
IF (Condition2) THEN
Statement1;
ELSE
Statement2
Note: A semicolon preceding an ELSE is not allowed.
If 'Condition1' is false, then nothing is executed. If 'Condition1' and 'Condition2' are both true, then 'Statement1' is executed. If 'Condition1' is true and 'Condition2' is false, then 'Statement2' is executed.
Reading several nested IF THEN ELSE statements can be quite confusing but a general rule is that an ELSE belongs to the last IF that lacks an ELSE.
Example 1
The following example shows an IF statement without the optional ELSE part.
IF Amount (<) 1000 THEN Total := Total + Amount;
Example 2
The following example shows an IF statement with the optional ELSE part.
(1)...
(2) IF Amount (<1000) align="justify">(3) THEN BEGIN
(4) IF I > J THEN Max := I
(5) ELSE Max := J;
(6) Amount := Amount * Max;
(6) END
(7) ELSE
(8)...
A common error is to put an extraneous semicolon at the end of a line before an ELSE (line 4). As mentioned earlier, this is not valid according to the syntax of C/AL. The semicolon is used as a statement separator. (The end of line 4 is inside the inner IF statement.)
CASE Statements
CASE statements have the following syntax.
CASE (Expression) OF
(Value set 1) : (Statement 1);
(Value set 2) :(Statement 2);......
(Value set n) : (Statement n);
[ ELSE (Statement n+1) ]
CASE statements are also called multiple option statements and are typically used when you must choose between more than two different actions.
The function of the CASE statement is as follows:
1. The 'Expression' is evaluated, and the first matching value set executes the associated statement, if there is one.
2. If none of the value sets matches the value of the expression, and the optional ELSE part has been omitted, no action is taken. If the optional ELSE part is used, then the associated statement is executed.
The data type of the value sets must be the same as the data type of 'Expression' or at least be convertible to the same data type.
For Eg:
The following C/AL code prints various messages depending on the value of Number. If the value of Number does not match any of the entries in the CASE structure, the ELSE entry is used as the default.
CASE Number OF
1,2,9: MESSAGE('1, 2, or 9.');
10..100: MESSAGE('In the range from 10 to 100.');
ELSE MESSAGE('Neither 1, 2, 9, nor in the range from 10 to 100.');
END

No comments: