Using nested IF statements

If 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.

About this task

However, use nested IF statements sparingly. The logic can be difficult to follow, although explicit scope terminators and indentation can help. If 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
    statement-1
  ELSE
    statement-2
  END-IF
  statement-3
ELSE
  statement-4
END-IF

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 flowchart of a nested IF statement. The pseudocode in preceding text describes the nested IF statement.

Related tasks  
Coding a choice of actions

Related references  
Explicit scope terminators (COBOL for Linux® on x86 Language Reference)