Enterprise COBOL for z/OS, V4.2, Language Reference

IF statement

The IF statement evaluates a condition and provides for alternative actions in the object program, depending on the evaluation.

Read syntax diagramSkip visual syntax diagram
Format

                                .-------------.     
                                V             |     
>>-IF--condition-1--+------+--+---statement-1-+-+--------------->
                    '-THEN-'  '-NEXT SENTENCE---'   

>--+---------------------------+--+------------+---------------><
   |         .-------------.   |  |        (1) |   
   |         V             |   |  '-END-IF-----'   
   '-ELSE--+---statement-2-+-+-'                   
           '-NEXT SENTENCE---'                     

Notes:
  1. END-IF can be specified with statement-2 or NEXT SENTENCE.

condition-1
Can be any simple or complex condition, as described in Conditional expressions.
statement-1, statement-2
Can be any one of the following:
  • An imperative statement
  • A conditional statement
  • An imperative statement followed by a conditional statement
NEXT SENTENCE
The NEXT SENTENCE phrase transfers control to an implicit CONTINUE statement immediately following the next separator period.

When NEXT SENTENCE is specified with END-IF, control does not pass to the statement following the END-IF. Instead, control passes to the statement after the closest following period.

END-IF phrase

This explicit scope terminator serves to delimit the scope of the IF statement. END-IF permits a conditional IF statement to be nested in another conditional statement. For more information about explicit scope terminators, see Delimited scope statements.

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

Transferring control

If the condition tested is true, one of the following actions takes place:

  • If statement-1 is specified, statement-1 is executed. If statement-1 contains a procedure branching or conditional statement, control is transferred according to the rules for that statement. If statement-1 does not contain a procedure-branching statement, the ELSE phrase, if specified, is ignored, and control passes to the next executable statement after the corresponding END-IF or separator period.
  • If NEXT SENTENCE is specified, control passes to an implicit CONTINUE statement immediately preceding the next separator period.

If the condition tested is false, one of the following actions takes place:

  • If ELSE statement-2 is specified, statement-2 is executed. If statement-2 contains a procedure-branching or conditional statement, control is transferred, according to the rules for that statement. If statement-2 does not contain a procedure-branching or conditional statement, control is passed to the next executable statement after the corresponding END-IF or separator period.
  • If ELSE NEXT SENTENCE is specified, control passes to an implicit CONTINUE STATEMENT immediately preceding the next separator period.
  • If neither ELSE statement-2 nor ELSE NEXT SENTENCE is specified, control passes to the next executable statement after the corresponding END-IF or separator period.

When the ELSE phrase is omitted, all statements following the condition and preceding the corresponding END-IF or the separator period for the sentence are considered to be part of statement-1.

Nested IF statements

When an IF statement appears as statement-1 or statement-2, or as part of statement-1 or statement-2, that IF statement is nested.

Nested IF statements are considered to be matched IF, ELSE, and END-IF combinations proceeding from left to right. Thus, any ELSE encountered is matched with the nearest preceding IF that either 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.



Rate this page

[ Index | Table of contents ]