── IF ── <Boolean expression> ── THEN ── <statement> ─┬─────┬──────────► └─ ; ─┘ ►─┬───────────────────────┬────────────────────────────────────────────┤ └─ ELSE ── <statement> ─┘
Explanation
The IF statement enables a conditional decision to be made based on the evaluation of a Boolean expression. The statement following THEN is executed if the condition is TRUE. If the condition is FALSE, control passes to the next executable statement. When the ELSE option is specified and the condition is FALSE, the statement following ELSE is executed.
The optional semicolon (;) after the THEN clause does not terminate the IF statement, and thus does not affect the logic of nested IF statements.
For nested IF statements, the WFL compiler matches the first ELSE clause it encounters with the innermost IF statement. This matching can result in a logic error when an IF statement without an ELSE clause is nested within another IF statement that does have an ELSE clause. To guarantee the correct logic, you can either add an ELSE clause containing a null statement, or use a compound statement to enclose the inner IF statement within the reserved words BEGIN and END.
Examples
This first example shows simple IF statements.
IF T(TASKVALUE) = 5 THEN RUN X; IF FILE X/Y ISNT RESIDENT THEN DISPLAY "NO FILE X/Y" ELSE RUN X/Y;
In this example, the first ELSE clause that contains a null statement guarantees the correct logic for the nested IF statements.
?BEGIN JOB NESTEDIFS/LOGIC (BOOLEAN B1, BOOLEAN B2); IF B1 THEN IF B2 THEN DISPLAY "Both expressions are TRUE"; ELSE ; % Null statement to guarantee correct logic. ELSE DISPLAY "Expression 1 is FALSE"; ?END JOB.
In this example, the compound statement guarantees the correct logic for the nested IF statements.
?BEGIN JOB NESTEDIFS/TESTER; TASK TASKVAR1, TASKVAR2; START NESTEDIFS/LOGIC [TASKVAR1] FOR SYNTAX; IF TASKVAR1 IS COMPILEDOK THEN BEGIN START NESTEDIFS/LOGIC (B1 := FALSE, B2 := TRUE) [TASKVAR2]; IF TASKVAR2 IS COMPLETEDOK THEN DISPLAY "NESTEDIFS/LOGIC compiled and completed"; END; ELSE ABORT [TASKVAR1] "NESTEDIFS/LOGIC did NOT compile"; ?END JOB.