Branching Statements
Branching statements control program flow based on evaluation of conditional expressions.
If, Then, and Else statements
The syntax of If, Then, and Else statements is:
If condition Then
[statement_list_1;]
[Else
[statement_list_2;]]
End-If;
This statement evaluates the Boolean expression condition. If condition is True, then the If statement executes the statements in statement_list_1. If condition is False, then the program executes the statements in the Else clause; if there is no Else clause, the program continues to the next statement.
Evaluate Statement
Use the Evaluate statement to check multiple conditions. Its syntax is:
Evaluate left_term
When [relop_1] right_term_1
[statement_list;]
[When [relop_n] right_term_n
[statement_list;]]
[When-other
[statement_list;]]
End-Evaluate;
The Evaluate statement takes an expression, left_term, and compares it to compatible expressions (right_term) using the relational operators (relop) in a sequence of When clauses. If relop is omitted, then the equal sign is assumed. If the result of the comparison is True, the program executes the statements in the When clause, and then moves on to evaluate the comparison in the following When clause. The program executes the statements in all of the When clauses for which the comparison evaluates to True. If none of the When comparisons evaluates to True, the program executes the statement in the When-other clause, if one is provided. For example, the following Evaluate statement executes only the first When clause. &USE_FREQUENCY in the following example can only have one of three string values:
Evaluate &USE_FREQUENCY
When = "never"
&PROD_USE_FREQ = 0;
When = "sometimes"
&PROD_USE_FREQ = 1;
When = "frequently"
&PROD_USE_FREQ = 2;
When-Other
Error "Unexpected value assigned to &USE_FREQUENCY."
End-Evaluate;
To end the Evaluate statement after the execution of a When clause, you can add a Break statement at the end of the clause, as in the following example:
Evaluate &USE_FREQUENCY
When = "never"
&PROD_USE_FREQ = 0;
Break;
When = "sometimes"
&PROD_USE_FREQ = 1;
Break;
When = "frequently"
&PROD_USE_FREQ = 2;
Break;
When-Other
Error "Unexpected value assigned to &USE_FREQUENCY."
End-Evaluate;
In rare cases, you may want to make it possible for more than one When clause to execute, as shown in the following example:
Evaluate &PURCHASE_AMT
When >= 100000
&BASE_DISCOUNT = "Y";
When >= 250000
&SPECIAL_SERVICES = "Y";
When >= 1000000
&MUST_GROVEL = "Y";
End-Evaluate;
For Statement
The For statement repeats a sequence of statements a specified number of times. Its syntax is:
For count = expression1 to expression2 [Step i]
statement_list;
End-For;
The For statement initializes the value of count to expression1, and then increments count by i each time after it executes the statements in statement_list. The program continues in this loop until count is equal to expression2. If the Step clause is omitted, then i equals one. To count backwards from a higher value to a lower value, use a negative value for i. You can exit a For loop using a Break statement.
The following example demonstrates the For statement:
&MAX = 10;
Local integer &COUNT;
For &COUNT = 1 To &MAX;
WinMessage("Executing statement list, count = " | &COUNT);
End-For;