IF Statement
The IF
statement is a conditional statement that evaluates the truth of a condition, and provides alternative branches for the runtime to follow depending on whether the condition is true or false.
General Format:
IF condition-1 [THEN] { statement-list-1 } …
[ ELSE {statement-list-2 } … ]
[ END-IF ]
Syntax:
condition-n
is a condition name described in a level 88 statement.statement-list-n
consists of one or more imperative or conditional statements.
General Rules:
- If
condition-1
isTRUE
,statement-list-1
executes, and control is then passed to the next executable statement. - If
condition-1
isFALSE
, and if anELSE
clause exists, thenstatement-list-2
executes. If noELSE
clause exists, then control is transferred to the end of theIF
statement. - An
IF
statement can be terminated by:- A period “
.
”. - An
END-IF
statement at the same nesting level. - An
ELSE
phrase in anIF
statement at a higher nesting level.
- A period “
statement-list-2
is optional. If there is no statement list following anELSE
phrase, the end of the scope of theELSE
statement is reached, and control passes to a higher nesting level, or to the statement following theIF
statement.
Code Sample:
IDENTIFICATION DIVISION.
PROGRAM-ID. IF-1.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 NUMERIC-1 PIC9(5)V9(5) VALUE 10.
77 NUMERIC-2 PIC9(5)V9(5) VALUE 20.
77 DUMMY PIC X.
PROCEDURE DIVISION.
MAIN.
IF NUMERIC-1 > NUMERIC-2
THEN
DISPLAY "YOU WIN!" LINE 10 COL 10
ELSE
DISPLAY "YOU LOSE!" LINE 10 COL 10
DISPLAY "CHANGE VALUES AND TRY AGAIN." LINE 11 COL 10
END-IF.
ACCEPT DUMMY LINE 11 COL 40.
STOP RUN.