Conditional GO TO Example


Scenario - Passing control to PARA-3 conditionally.

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.                         
       PROGRAM-ID. CONDGOTO.                            
       AUTHOR. MTH.                                     
                                                        
       DATA DIVISION.                                   
       WORKING-STORAGE SECTION.                         
       01 WS-P       PIC 9(02).                         
                                                        
       PROCEDURE DIVISION.
       PARA-0.
           DISPLAY 'Para0'.
           MOVE 3      TO WS-P 

           GO TO PARA-1 PARA-2 PARA-3 DEPENDING ON WS-P.
           DISPLAY 'This will be skipped'.

       PARA-1.
           DISPLAY 'Para1'.
       PARA-2.
           DISPLAY 'Para2'.
       PARA-3.
           DISPLAY 'Para3'.
           STOP RUN.

Output -

Para0
Para3

Explaining Example -

In the above example:

  • If the value is 1, the control transfers to the first statement in PARA-1. If the value is 2, the control will transfer to the first statement in PARA-2 and so on.
  • No control transfer occurs if the value is other than a value range of 1 through n (where n is the number of paragraphs coded in GO TO statement). Instead, control passes to the next display statement and displays "This will be skipped".