In this section: |
How to: |
The IF command allows conditional processing depending on how an expression is evaluated.
The syntax of the IF command is
IF boolean_expr THEN maint_command [ELSE maint_command]
where:
Is an expression that resolves to a value of true (1) or false (0), and can include stack cells and user-defined fields. For more information about Boolean expressions, see Expressions Reference.
Maintain Data handles the format conversion in cases where the expressions have a format mismatch. If the conversion is not possible, a message displays. For additional information, see Expressions Reference.
It is highly recommended that parentheses be used when combining expressions. If parentheses are not used, the operators are evaluated in the following order:
**
* /
+ -
LT LE GT GE
EQ NE
OMITS CONTAINS
AND
OR
You can place any Maintain Data command inside an IF command except for CASE, DECLARE, DESCRIBE, END, MAINTAIN, and MODULE.
The following uses an IF command to compare variable values. The function No_ID is performed if the Current Area value of Emp_ID does not equal the value of Emp_ID in StackEmp:
IF Emp_ID NE StackEmp(StackEmp.FocIndex).Emp_ID THEN PERFORM No_ID; ELSE PERFORM Yes_ID;
You might also use an IF command to issue another Maintain Data command. This example causes a COMMIT if there are no errors:
IF FocCurrent EQ 0 THEN COMMIT;
This example executes a set of code depending on the value of Department. Additional IF commands could be placed within the BEGIN block of code:
IF Department EQ 'MIS' THEN BEGIN . . . ENDBEGIN ELSE IF Department EQ 'MARKETING' THEN BEGIN . . .
IF commands can be nested as deeply as needed, allowing only for memory constraints. The following shows an IF command nested two levels. There is only one IF command after each ELSE:
IF Dept EQ 1 THEN TYPE "DEPT EQ 1"; ELSE IF Dept EQ 2 THEN TYPE "DEPT EQ 2"; ELSE IF Dept EQ 3 THEN TYPE "DEPT EQ 3"; ELSE IF Dept EQ 4 THEN TYPE "DEPT EQ 4";
This example can be executed more efficiently by issuing the following command:
TYPE "DEPT EQ <Dept";
You can also use the BEGIN command to place another IF within a THEN phrase. For example:
IF A EQ 1 THEN BEGIN IF B EQ 1 THEN BEGIN IF C EQ 1 THEN PERFORM C111; IF C EQ 2 THEN PERFORM C112; IF C EQ 3 THEN PERFORM C113; ENDBEGIN ELSE IF B EQ 2 THEN BEGIN IF C EQ 1 THEN PERFORM C121; IF C EQ 2 THEN PERFORM C122; IF C EQ 3 THEN PERFORM C123; ENDBEGIN ENDBEGIN ELSE IF A EQ 2 THEN BEGIN IF B EQ 1 THEN BEGIN IF C EQ 1 THEN PERFORM C211; IF C EQ 2 THEN PERFORM C212; IF C EQ 3 THEN PERFORM C213; ENDBEGIN ELSE IF B EQ 2 THEN BEGIN IF C EQ 1 THEN PERFORM C221; IF C EQ 2 THEN PERFORM C222; IF C EQ 3 THEN PERFORM C223; ENDBEGIN ENDBEGIN ELSE TYPE "A, B AND C did not have expected values";
To assign a value to a variable, and the value you assign is conditional upon the truth of an expression, you can use a conditional COMPUTE command. Maintain Data offers you two methods of coding this, using either:
IF Amount GT 100 THEN COMPUTE Tfactor/I6 = Amount; ELSE COMPUTE Tfactor = Amount * (Factor - Price) / Price;
COMPUTE Tfactor/I6 = IF Amount GT 100 THEN Amount ELSE Amount * (Factor - Price) / Price;
The two methods are equivalent.