Search code examples
ibm-midrangecobol

How does GO TO control flow work in COBOL?


PROCEDURE DIVISION
MAINPARA
    DISPLAY "HELLO MAIN".
    GO TO PARA1.
    DISPLAY " SECOND DISPLAY".
    STOP RUN.

PARA1.
    DISPLAY " I AM IN PARA1".

PARA2.
    DISPLAY "I AM IN PARA2"
....
PARA200

I have little understanding of the flow. But, I am confused. When control jump to GO TO PARA1, it will execute PARA1.

Now my question is:

  1. Will it execute PARA2and return to MAINPARA?
  2. Or will it execute from PARA2 towards the end of the program?

I am not a COBOL programmer, and I need to understand code from a migration tool/process, AMXW COBOL. The target system is an IBM AS/400.


Solution

  • GO TO statement permanently transfers execution from one part of program to another part of program. After GO TO PARA1, execution will jump to PARA1 label, execute the following paragraph and then continue from there.

    OUTPUT:

    HELLO MAIN
     I AM IN PARA1
    I AM IN PARA2
    .
    .
    I AM IN PARA200
    

    So, the execution will continue until it encounters a STOP RUN statement or a runtime error.

    Note: GO TO statements are usually considered bad practice for a reason. It becomes harder to keep track of where the GO TO statements go so to speak. I'd suggest using PERFORM instead. Which returns control to where it was after executing a procedure.