IF Statement [ HP COBOL II/XL Reference Manual ] MPE/iX 5.0 Documentation
HP COBOL II/XL Reference Manual
IF Statement
The IF statement evaluates a condition and, depending upon the truth
value of the condition, determines the subsequent action of the program.
Syntax
Parameters
statement-1 each are imperative or conditional statements, optionally
and followed by a conditional statement.
statement-2
condition-1 any valid COBOL condition as described under "Conditional
Expressions" in Chapter 8 .
Description
You may omit the ELSE NEXT SENTENCE phrase if it immediately precedes the
period used to terminate the sentence.
If the END-IF phrase is specified, the NEXT SENTENCE phrase must not be
specified.
The scope of the IF statement may be terminated by any of the following:
* An END-IF phrase at the same level of nesting.
* A separator period, which terminates IF statements at all levels
of nesting.
* If nested, by an ELSE phrase associated with an IF statement at a
higher level of nesting.
When an IF statement is executed, the following transfers of control
occur:
* If the truth value of the condition is "true" and statement-1 is
specified, then if statement-1 is a procedure branching or
conditional statement, control is explicitly transferred according
to the rules for that statement. If statement-1 does not contain
such a statement, then statement-1 is executed and control passes
to the end of the IF statement.
If the truth value of the condition is "true" and the NEXT
SENTENCE phrase is used instead of statement-1, control
immediately passes to the next executable sentence.
* If the truth value of the condition is "false" and if statement-2
is specified, then if statement-2 is a procedure branching or
conditional statement, control is explicitly transferred according
to the rules for that statement. If statement-2 is not such a
statement, then statement-2 is executed and control passes to the
end of the IF statements.
* If the truth value of the condition is "false" and statement-2 is
not specified, then the ELSE NEXT SENTENCE phrase, if specified,
causes transfer of control to the next executable sentence. If
the condition is false and the ELSE phrase is not specified then
statement-1 is ignored and control passes to the end of the IF
statement.
Statement-1 or statement-2 may be (or may contain) an IF statement,
according to their description in the previous paragraphs. This is
called a nested IF statement.
IF statements within IF statements may be considered as paired IF, ELSE,
and END-IF combinations, proceeding from left to right. Thus, any ELSE
or END-IF encountered is considered to apply to the immediately
preceding IF that has not been already paired with an ELSE or END-IF
respectively.
To clarify, the IF/ELSE pairing is shown in the following illustration:
Example
BEGIN SECTION.
DATA-IN.
READ REC-FILE RECORD INTO DATA-REC.
:
IF DATA-REC IS NOT ALPHABETIC
THEN
IF DATA-REC IS NOT NUMERIC
PERFORM ILLEGAL-CHARACTER
ELSE NEXT SENTENCE
ELSE PERFORM ALPHA-TYPE.
:
The IF statements above check that the data read into DATA-REC is either
all alphabetic or all numeric.
The first IF statement consists of the IF/ELSE pair:
IF DATA-REC IS NOT ALPHABETIC...ELSE PERFORM ALPHA-TYPE.
The second IF/ELSE pair is:
IF DATA-REC IS NOT NUMERIC...ELSE NEXT SENTENCE.
Thus, if DATA-REC has any nonnumeric character or characters not from the
English alphabet, the procedure ILLEGAL-CHARACTER is performed.
To clarify, the IF/ELSE and END-IF pairing is shown in the following
illustration.
MPE/iX 5.0 Documentation