Function
The IF statement causes a condition to be evaluated (see under section "Conditions"). The subsequent action of the program depends upon whether the condition is true or false.
Format 1
IF condition THEN statement-1 [ELSE statement-2]
END-IF
Format 2
IF condition THEN {statement-1 | NEXT SENTENCE} [ELSE {statement-2 | NEXT SENTENCE}]
Syntax rules
statement-1 and statement-2 may consist of one or more imperative statements and/or one conditional statement.
An IF statement with the NEXT SENTENCE phrase must not be contained within statement-1 or statement-2 of a format 1 IF statement which is immediately terminated with END-IF.
General rules
statement-1 and/or statement-2 may contain an IF statement. In this case the IF statement is said to be nested.
The scope of the IF statement may be terminated by any of the following:
An END-IF phrase at the same level of nesting.
A separator period.
If nested, by an ELSE phrase associated with an IF statement at a higher level of nesting.
IF statements within IF statements may be considered as paired IF, ELSE, and END-IF combinations, proceeding from left to right. Thus, any ELSE or END-IF encountered is considered to apply to the immediately preceding IF that has not been already paired with an ELSE or END-IF.
When an IF statement is executed, the following transfers of control occur:
If the condition is true and statement-1 is specified, control is transferred to the first statement of statement-1 and execution continues according to the rules for each statement specified in statement-1. If a procedure branching or conditional statement is executed which causes an explicit transfer of control, control is explicitly transferred in accordance with the rules of that statement. Upon completion of the execution of statement-1, the ELSE phrase, if specified, is ignored and control passes to the end of the IF statement.
If the condition is true and the NEXT SENTENCE phrase is specified instead of statement-1, the ELSE phrase, if specified, is ignored and control passes to the next executable sentence.
If the condition is false and statement-2 is specified, statement-1 or its surrogate NEXT SENTENCE is ignored, control is transferred to the first statement of statement-2, and execution continues according to the rules for each statement specified in >statement-2. If a procedure branching or conditional statement is executed which causes an explicit transfer of control, control is explicitly transferred in accordance with the rules of that statement. Upon completion of the execution of statement-2, control passes to the end of the IF statement.
If the condition is false and the ELSE phrase is not specified, statement-1 is ignored and control passes to the end of the IF statement.
If the condition is false and the ELSE NEXT SENTENCE phrase is specified, statement-1 is ignored and control passes to the next executable sentence.
Example 8-42
IF A = B THEN statement-1 END-IF statement-2.
If A = B is true, statement-1 and statement-2 are executed.
If A = B is false, only statement-2 is executed.
Example 8-43
IF A = B THEN statement-1 ELSE statement-2 END-IF statement-3.
If A = B is true, statement-1 and statement-3 are executed.
If A = B is false, statement-2 and statement-3 are executed.
Example 8-44
IF A = B THEN CONTINUE ELSE statement-1 END-IF statement-2.
If A = B is true, statement-2 is executed.
If A = B is false, statement-1 and statement-2 are executed.
Example 8-45
|-> IF MALE | |-> IF MARRIED | | ADD 1 TO MALES-MARRIED | |-> ELSE | | |-> IF DIVORCED | | | ADD 1 TO MALES-DIVORCED | | |-> ELSE | | | ADD 1 TO MALES-SINGLE | | |-> END-IF | |-> END-IF |-> ELSE | ADD 1 TO FEMALE |-> END-IF next statement
This is an example of the structure of a nested IF statement. The arrows are used to indicate the IF, ELSE and END-IF assignments.