5
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.

Bill Woodger
  • 12,968
  • 4
  • 38
  • 47
Siddharth
  • 197
  • 1
  • 14
  • Someone is downvoting answers on this question for no reason. It would be very good if they would stop, and reverse their votes. – Bill Woodger Oct 31 '14 at 12:18
  • (I'm 35, and when I started programming everyone did a little BASIC, so when I see `GOTO` I just sorta instinctively know what it's supposed to do. It's interesting that people coming online now have never seen a jump instruction and the default assumption is that it should behave like a subroutine. I have hope for the future!) – iluvcapra Nov 07 '14 at 20:33

5 Answers5

7

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
Bill Woodger
  • 12,968
  • 4
  • 38
  • 47
6

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.

Tamer Tas
  • 3,288
  • 13
  • 22
  • 1
    And the fact that it "continues from there" is precisely why it is a dangerous instruction to avoid as much as possible. To not say it turns your code into spaggetti making it efficient for the machine but hardly understandable to humans. – Thomas G Oct 31 '14 at 11:44
2

GO TO transfers the control the marked para and control does not come back. So, your output would be:

HELLO MAIN
 I AM IN PARA1
I AM IN PARA2
...
Bill Woodger
  • 12,968
  • 4
  • 38
  • 47
Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69
1

As @Tamer Tas indicated, GO TO in COBOL transfers control to the named label, that is the paragraph or section named in the GO TO. In the code you supplied in your question the output will be

HELLO MAIN
 I AM IN PARA1
I AM IN PARA2

and so on until a GOBACK or STOP RUN is encountered. If neither of these is encountered, your program will terminate in an unpredictable fashion.

GO TO is strongly discouraged; even so, you will see GO TO used in modern code. A contrived code example...

    [...]
    Perform 100-DO-IT Through 100-DO-IT-EXIT
    [...]
    GOBACK.

100-DO-IT.
    If A < B 
        GO TO 100-DO-IT-EXIT
    End-If

    Compute C = A - B
    .

100-DO-IT-EXIT.
    Exit.

Obviously the If statement is trivial and the GO TO could be avoided by moving the If outside of 100-EDIT to the Perform that transfers control there. When the controlling logic is complex the programmer must ask themself how "high up" or "far down" in the code should this controlling logic be pushed.

This particular coding style has its proponents; I am not one of them.

Bill Woodger
  • 12,968
  • 4
  • 38
  • 47
cschneid
  • 10,237
  • 1
  • 28
  • 39
1

The GO TO in COBOL is the same as the GO TO in C.

In your example, the GO TO will process sequentially all the instructions until the end of the program.

If you want it to execute only the PARA1, you can use the PERFORM PARA1 statement. In case there are more paragraphs that you want to execute, but not until the end of the program, you can use PERFORM THRU constructions. By using perform, the program will return after the paragraph is executed.

Also, please keep in mind, that in many places (corporate environment) you are not allowed to use GO TO as it creates a lot of hard to understand code. Also, the chance for an infinite loop is considerable.

bmakos
  • 159
  • 8