GO TO with IF Example


Scenario - GO TO with IF.

Code -

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

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-P       PIC 9(02).

       PROCEDURE DIVISION.
       PARA0.
           DISPLAY 'Para0'.
           MOVE 3      TO WS-P
           PERFORM PARA1
              THRU PARA1-EXIT.
           DISPLAY 'Return to para0'.
           STOP RUN.
       PARA1.
           DISPLAY 'Para1'.
           IF WS-P > 1 
              GO TO PARA1-EXIT
           ELSE 
              COMPUTE WS-P = WS-P + 1
           END-IF.
           DISPLAY 'WS-P: ' WS-P.
       PARA1-EXIT.
           EXIT.

Output -

Para0
Para1
Return to para0 

Explaining Example -

In the above example:

  • PARA0 initiates the process, sets a value, invokes PARA1, and displays a message upon return.
  • PARA1 performs the logic based on the value of WS-P and displays Para1.
  • If WS-P is greater than 1, PARA1 exits (GO TO) and continue with the program execution.