Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 738
Siddharth Avatar asked Oct 31 '14 10:10

Siddharth


People also ask

What is the use of goto statement in Cobol?

For a COBOL program compiled without hooks being inserted by the compiler and with optimization, if you compiled with the NOEJPD suboptions of the TEST compiler option, you can use the GOTO command if the SET WARNING is set to OFF and the runtime level allows GOTO without compiler enablement.


2 Answers

Your program will display :

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

Because GO TO moves the execution point of your program. Then it executes sequentially from where it has been moved to.

On this opposit if you replace GO TO verb by PERFORM verb, the program :

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

PARA1.
    DISPLAY " I AM IN PARA1".

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

Would display :

HELLO MAIN
 I AM IN PARA1
SECOND DISPLAY
like image 88
Julien Mousset Avatar answered Sep 30 '22 20:09

Julien Mousset


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.

like image 42
Tamer Tas Avatar answered Sep 30 '22 20:09

Tamer Tas