Scope of Statements

The scope of a statement designates when a statement ends. A period terminates all currently active statements. In a delimited-scope statement, the scope delimiter ends the statement. An imperative statement ends at the beginning of the next statement.

Conditional and delimited-scope statements can contain other statements. These statements can be terminated implicitly by elements of the containing statement. In the following example, the ELSE clause terminates the ADD and DISPLAY statements. The period terminates the MOVE and IF statements.

    IF VAR-1 = VAR-2
        ADD VAR-X TO VAR-Y
            ON SIZE ERROR DISPLAY "OVERFLOW"
    ELSE
        MOVE VAR-2 TO VAR-1.

Clauses or scope-delimiters that terminate a statement always terminate the statement that begins with the closest preceding unpaired verb of the appropriate type. In the following example, the first ELSE terminates the DISPLAY statement, the second ELSE terminates the MOVE statement and the second IF statement, and the period terminates the ADD statement and the first IF statement.

    IF VAR-1 = VAR-2
        IF VAR-3 = VAR-4
            DISPLAY "CONDITION 1"
        ELSE
            MOVE "CONDITION 2" TO VAR-5
    ELSE
        ADD 1 TO VAR-3.

A delimited-scope statement is one that contains a scope-delimiter. A scope-delimiter is always a reserved word that begins with END- and finishes with a verb name. For example, END-ADD and END-START are scope delimiters for the ADD and START statements. The presence of a scope-delimiter converts a conditional statement into a delimited-scope statement. A delimited-scope statement may be used anywhere an imperative statement can appear.

Note: Only imperative statements may be nested within another statement, unless the other statement is an IF statement.

The following is illegal:

ADD VAR-1 TO VAR-2 ON SIZE ERROR
       IF VAR-1 < ZERO
          DISPLAY "VAR-1 IS NEGATIVE"
       ELSE
          DISPLAY "VAR-1 TOO LARGE".

Since the IF statement is conditional, it may not be nested within the ADD statement. The correct version of this construct is:

    ADD VAR-1 TO VAR-2 ON SIZE ERROR
       IF VAR-1 < ZERO
          DISPLAY "VAR-1 IS NEGATIVE"
       ELSE
          DISPLAY "VAR-1 TOO LARGE"
       END-IF.

The presence of the END-IF converts the conditional IF statement into a delimited-scope statement which may be used in place of an imperative statement. Thus it may appear nested within the ADD statement.