Skip to content

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:

  1. condition-n is a condition name described in a level 88 statement.
  2. statement-list-n consists of one or more imperative or conditional statements.

General Rules:

  1. If condition-1 is TRUE, statement-list-1 executes, and control is then passed to the next executable statement.
  2. If condition-1 is FALSE, and if an ELSE clause exists, then statement-list-2 executes. If no ELSE clause exists, then control is transferred to the end of the IF statement.
  3. An IF statement can be terminated by:
    • A period “.”.
    • An END-IF statement at the same nesting level.
    • An ELSE phrase in an IF statement at a higher nesting level.
  4. statement-list-2 is optional. If there is no statement list following an ELSE phrase, the end of the scope of the ELSE statement is reached, and control passes to a higher nesting level, or to the statement following the IF 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.
Back to top