sign in
 
   
 
 
 
   
  COBOL TUTORIAL FOR BCA STUDENTS OF M G UNIVERSITY  
  UNIT 4: PROCEDURE DIVISION . . .  
   
     
 

CONDITIONAL STATEMENTS

 
     
  NESTED IF STATEMENT  
 

The then and else statement of an IF statement can contain other IF statements. The included IF statements in their turn may also contain other IF statements. Such inclusion of one or more IF statements within the scope of an IF statement is called nesting. Note that the most inclusive IF statement must have a terminating period and thus this statement along with all the included statements is often called or NESTED IF STATEMENT.

Since the else phrase in an IF statement is optional, a nested If sentence may have fewer ELSEs than Ifs.

The nested IF sentence should be examined in a left- to-right manner to encounter each ELSE in the order of its appearance. As soon as an ELSE is encountered, it must be paired with the immediately preceding IF that has not yet been paired with another ELSE.

Example:

 
   
 

This nested IF sentence contains two IFs and one ELSE. The IF-ELSE pair has been marked by the number 1 within a circle. The IF marked with 2 within a circle is an IF without any ELSE.

Example:


 
 
 
 

The use of NEXT SENTENCE either in the then or else parts of an IF statement is recommended. The only exception can be when the ELSE NEXT SENTENCE phrase is immediately followed by the terminating period of the IF sentence, this is because the control will be transferred to the next sentence regardless of whether or not the ELSE NEXT SENTENCE phrase is explicitly included.