The GO TO Statement

There’s a good reason why the GO TO statement has taken a lot of criticism over the past 50 years. It was the famed Dutch computer scientist Dijkstra who led the debate about whether this statement is actually worth using when it comes to program control flow. Take a look at this example below:

Cobol




IDENTIFICATION DIVISION.
PROGRAM-ID. DISPLAY-HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
    01 Greeting PIC A(5) VALUE "Hello".
PROCEDURE DIVISION.
MAIN-PROCEDURE.
    PERFORM DisplayHello
    STOP RUN.
DisplayHello.
    DISPLAY Greeting
    GO TO DisplayHello.   
END PROGRAM DISPLAY-HELLO.


The program gets caught in a never-ending loop because of the ill-placed GO TO statement in the DisplayHello paragraph and where the output prints “Hello!” an infinite number of times. At all costs, this unconditional GO TO statement is to be avoided.

Of course, if you absolutely must use a GO TO statement, use one that is conditional. Here’s an example of such a GO TO statement:

Cobol




IDENTIFICATION DIVISION.
PROGRAM-ID. DISPLAY-HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
    01 Greeting PIC A(5) VALUE "Hello".
    01 Answer PIC A(1) VALUE "N".
PROCEDURE DIVISION.
MAIN-PROCEDURE.
    PERFORM DisplayHello
    STOP RUN.
DisplayHello.
    DISPLAY Greeting
    DISPLAY "Terminate Greeting? (Y/N): "
    ACCEPT Answer
    IF Answer = "N"
        GO TO DisplayHello.
END PROGRAM DISPLAY-HELLO.


As you can tell, you can terminate this loop set up by the GO TO statement if you change the value of Answer to “Y” by input, as shown in the output below:

Yet most experts think that it is best to avoid GO TO statements altogether and in the case of COBOL, using the PERFORM verb for loops is best.

Loop Statements in COBOL

Every programming language not only has a selection construct but also one that involves iteration.

With this construct, it is possible for a block of code to run repeatedly. In fact, the programmer himself can code it in by selecting a specific type of loop. 

Speaking of types of loops, modern programming languages offer both for and while loops while some others provide a repeat construct. While the former runs for a certain number of times as specified by the programmer, the latter can run indefinitely if the set condition is not satisfied. Here’s the Python 3 ‘for’ loop:

Python3




for i in range(0,5,1):
    print(i)


As you can tell, the numbers 0 to 4 are printed with this for-loop, as shown in the output below:

0
1
2
3
4

Now, let’s look at the ‘while’ loop in Java:

Java




int x = 0;
while (x < 5) {
System.out.println(x);
x++;
}


You get the same output with this while loop in Java too, as shown in the output below:

0
1
2
3
4

As simple as these iterative constructs are, COBOL also provides such loop constructs. Even if the syntax might seem very different from the examples shown above.

Similar Reads

Loop Statements in COBOL:

...

The Simple PERFORM Statement:

...

The PERFORM-THRU Statement:

Simply put, loops in COBOL are carried out using the PERFORM verb but it has other functions that are worth mentioning here....

The PERFORM TIMES Statement

As mentioned earlier, the PERFORM statement will execute a block of code just once in its simplest form. For this example, we have used the PERFORM statement to run the code within the AddTwoNumbers and SubtractTwoNumbers paragraphs....

The PERFORM UNTIL Statement:

...

The PERFORM VARYING Statement:

When we use the PERFORM-THRU statement, the objective is to run several paragraphs in one go. Of course, the statement has to provide the starting and the final paragraph, as shown in the code below:...

The GO TO Statement:

...

Conclusion:

This statement is the first way in COBOL by which we can repeat a block of code as many times as we choose to. As you can tell, we can specify the number of times – 3 times – that the block of code has to run, as shown below:...