If..Else Example


Scenario - Validating gender using IF..ELSE.

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. IFELSE.
       AUTHOR. MTH.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-VAR.
          05 WS-GENDER      PIC X(01).

       PROCEDURE DIVISION.

           MOVE "F"      TO WS-GENDER.
           IF WS-GENDER EQUAL "M"
              DISPLAY "Person is Male"  
           ELSE
              DISPLAY "Person is Female"
           END-IF.

           STOP RUN.

Output -

Person is Female

Explaining Example -

In the above example:

  • IF condition validates WS-GENDER EQUAL "M" and based on its truth value, the control flow gets decided.
  • The condition is false, so the control transferred to ELSE part and displays "Person is Female".