The IF statement causes a condition to be evaluated (see the topic Conditional Expressions in the chapter Procedure Division).
The subsequent action of the runtime element depends on whether the value of the condition is true or false.
General Format
Syntax Rules
- Statement-1 and statement-2 represent either an imperative statement or a conditional statement optionally preceded by an
imperative statement.
- If the END-IF phrase is specified, the NEXT SENTENCE phrase must not be specified.
NEXT SENTENCE can be specified with END-IF.
If the NEXT SENTENCE phrase is executed, control does not pass to the next statement following the END-IF, but instead passes
to the statement after the closest following period.
General Rules
- The scope of an IF statement can 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.
- When an IF statement is executed, the following transfers of control occur:
- If the condition is true, statement-1 is executed if specified. If statement-1 contains a procedure branching or conditional
statement, control is explicitly transferred in accordance with the rules of that statement. If statement-1 does not contain
a procedure branching or conditional statement, 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, statement-1 or its surrogate NEXT SENTENCE is ignored, and statement-2, if specified, is executed.
If statement-2 contains a procedure branching or conditional statement, control is explicitly transferred in accordance with
the rules of that statement. If statement-2 does not contain a procedure branching or conditional statement, control passes
to the end of the IF statement. If the ELSE statement-2 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, if specified, and control
passes to the next executable sentence.
- Statement-1 and/or statement-2 can contain an IF statement. In this case the IF statement is said to be nested.
IF statements within IF statements are considered matched IF, ELSE, and END-IF ordered combinations, proceeding from left
to right. Thus, any ELSE encountered is matched with the nearest preceding IF that has not been already matched with an ELSE
or has not been implicitly or explicitly terminated. Any END-IF encountered is matched with the nearest preceding IF that
has not been implicitly or explicitly terminated.