Rational Developer for System z
COBOL for Windows, Version 7.5, Language Reference


IF statement

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

Format
Read syntax diagramSkip visual syntax diagram                                .-------------.    
                                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:
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:

Transferring control

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

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

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.


Terms of use | Feedback

Copyright IBM Corporation 1996, 2008.
This information center is powered by Eclipse technology. (http://www.eclipse.org)