IF

In this section: How to: |

The IF command allows conditional processing depending on how an expression is evaluated.

Syntax: How to Use the IF Command

The syntax of the IF command is

IF boolean_expr THEN maint_command [ELSE maint_command]

where:

- boolean_expr
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 handles the format conversion in cases where the expressions have a format mismatch. If the conversion is not possible, an error message is displayed. 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

- maint_command
You can place any Maintain command inside an IF command except for CASE, DECLARE, DESCRIBE, END, MAINTAIN, and MODULE.

Example: Simple Conditional Branching

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 command. This example causes a COMMIT if there are no errors:

IF FocCurrent EQ 0 THEN COMMIT;

Example: Using BEGIN to Execute a Block of Conditional Code

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 . . .

Example: Nesting IF Commands

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";

Coding Conditional COMPUTE Commands

When you need 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 offers you two methods of coding this, using either:

- An IF command
with two COMPUTE commands embedded within it. For example:
IF Amount GT 100 THEN COMPUTE Tfactor/I6 = Amount; ELSE COMPUTE Tfactor = Amount * (Factor - Price) / Price;

- A conditional
expression within a COMPUTE command. For example:
COMPUTE Tfactor/I6 = IF Amount GT 100 THEN Amount ELSE Amount * (Factor - Price) / Price;

The two methods are equivalent.

WebFOCUS |