Enterprise COBOL for z/OS, Version 4.2, Programming Guide

Using nested IF statements

When an IF statement contains an IF statement as one of its possible branches, the IF statements are said to be nested. Theoretically, there is no limit to the depth of nested IF statements.

However, use nested IF statements sparingly. The logic can be difficult to follow, although explicit scope terminators and indentation help. When a program has to test a variable for more than two values, EVALUATE is probably a better choice.

The following pseudocode depicts a nested IF statement:

IF condition-p
  IF condition-q

In the pseudocode above, an IF statement and a sequential structure are nested in one branch of the outer IF. In this structure, the END-IF that closes the nested IF is very important. Use END-IF instead of a period, because a period would end the outer IF structure also.

The following figure shows the logic structure of the pseudocode above.

This image shows a flow chart of a nested IF statement. The pseudocode in preceding text describes the same nested IF statement.

related tasks  
Coding a choice of actions

related references  
Explicit scope terminators (Enterprise COBOL Language Reference)

Rate this page

[ Index | Table of contents ]