NESTED IF STATEMENT:
The then and else statement of an IF statement can hold other IF statements. The involved IF statements in their turn may also hold other IF statements. These inclusion of one or more IF statements within the scope of the IF statement is termed as nesting. Note that the most inclusive IF statement should have a terminating period and therefore this statement along with all the included statements is frequently termed as NESTED IF STATEMENT.
As the else phrase in an IF statement is optional, the nested If sentence may have less ELSEs than Ifs. This makes the interpretation of the nested IF sentence quite difficult. The initial step in interpreting such a sentence would be to find out that ELSE belongs to which IF and which are the IFs that do not have the corresponding ELSEs. Once this is completed, the actions specified for the various cases can be recognized easily. To avoid any ambiguity in an interpretation, the COBOL rule is as shown below.
The nested IF sentence must be examined in the left- to-right manner to encounter each
ELSE in the order of its appearance. As soon as an ELSE is encountered, it should be paired with the instantly preceding IF which has not yet been paired with the other ELSE.
Note that the above rule can also helps in detecting those Ifs for which the ELSE phrase may be absent.
The above rule states how the COBOL compiler will interpret a nested IF sentence. And hence, while writing such a sentence this rule should be applied to verify that the interpretation of the compiler will not be different from what is intended. The illustrations below are used to show how the meaning of a nested IF sentence can be obtained by applying the above rules.