Conditional statements
A conditional statement is either a simple conditional
statement (IF
, EVALUATE
, SEARCH
)
or a conditional statement made up of an imperative statement that
includes a conditional phrase or option.
You can end a conditional statement with an implicit or explicit scope terminator. If you end a conditional statement explicitly, it becomes a delimited scope statement (which is an imperative statement).
You can use a delimited scope statement in these ways:
- To delimit
the range of operation for a COBOL conditional statement
and to explicitly show the levels of nesting
For example, use an
END-IF
phrase instead of a period to end the scope of anIF
statement within a nestedIF
. - To code a conditional
statement where the COBOL syntax calls for
an imperative statement
For example, code a conditional statement as the object of an inline
PERFORM
:PERFORM UNTIL TRANSACTION-EOF PERFORM 200-EDIT-UPDATE-TRANSACTION IF NO-ERRORS PERFORM 300-UPDATE-COMMUTER-RECORD ELSE PERFORM 400-PRINT-TRANSACTION-ERRORS END-IF READ UPDATE-TRANSACTION-FILE INTO WS-TRANSACTION-RECORD AT END SET TRANSACTION-EOF TO TRUE END-READ END-PERFORM
An explicit scope terminator is required for the inline
PERFORM
statement, but it is not valid for the out-of-linePERFORM
statement.
For
additional program control, you can use the NOT
phrase
with conditional statements. For example, you can provide instructions
to be performed when a particular exception does not occur, such as NOT
ON SIZE ERROR
. The NOT
phrase cannot be
used with the ON OVERFLOW
phrase of the CALL
statement,
but it can be used with the ON EXCEPTION
phrase.
Do not nest conditional statements. Nested statements must be imperative statements (or delimited scope statements) and must follow the rules for imperative statements.
The following statements are examples of conditional statements if they are coded without scope terminators:
- Arithmetic statement
with
ON SIZE ERROR
- Data-manipulation statements
with
ON OVERFLOW
CALL
statements withON OVERFLOW
I/O
statements withINVALID KEY
,AT END
, orAT END-OF-PAGE
RETURN
withAT END