Open In App

Loop Statements in COBOL

Improve
Improve
Like Article
Like
Save
Share
Report

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.

Loop Statements in COBOL:

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

In its simplest form, PERFORM merely transfers control to a block of code that will be executed just once. This could either be a paragraph or a section. Alternatively, it could execute several blocks of code contained in a number of consecutive paragraphs by using the PERFORM THRU construct.

As for iteration constructs that are the focus of this discussion, you can use the PERFORM verb to execute a block of code, a set number of times by using the PERFORM TIMES loop.

You can also set the PERFORM verb to execute blocks of code until a particular condition is satisfied by using the PERFORM UNTIL loop.

Lastly, the PERFORM VARYING loop works a certain number of times and depends on the FROM BY values that we use.

Let’s begin with the syntax of the simple PERFORM statement.

The Simple PERFORM 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.

Cobol




IDENTIFICATION DIVISION.
       PROGRAM-ID. ADD-SUBTRACT-NUMBERS.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
           01 my-var PIC 99 VALUE 12.
           01 my-var2 PIC 99 VALUE 23.
           01 resultAdd PIC 999.
           01 resultSub PIC 99.
       PROCEDURE DIVISION.
       MAIN-PROCEDURE.
           PERFORM AddTwoNumbers
           PERFORM SubtractTwoNumbers
           PERFORM DisplayResult
           STOP RUN.
       AddTwoNumbers.
           COMPUTE resultAdd = my-var + my-var2.
       SubtractTwoNumbers.
           COMPUTE resultSub = my-var2 - my-var.
       DisplayResult.
           DISPLAY "Total: " resultAdd
           DISPLAY "Difference: " resultSub.
       END PROGRAM ADD-SUBTRACT-NUMBERS.


 Once the results for the operations are complete, the DisplayResult paragraph then prints the results as shown below:

Total: 035
Difference: 11

Next, we will look at the syntax of the PERFORM THRU statement and how it differs from using the PERFORM statement just once.

The PERFORM-THRU 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:

Cobol




IDENTIFICATION DIVISION.
       PROGRAM-ID. ADD-SUBTRACT-NUMBERS.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
           01 my-var PIC 99 VALUE 12.
           01 my-var2 PIC 99 VALUE 23.
           01 resultAdd PIC 999.
           01 resultSub PIC 99.
       PROCEDURE DIVISION.
       MAIN-PROCEDURE.
           PERFORM AddTwoNumbers THRU DisplayResult
           STOP RUN.
       AddTwoNumbers.
           COMPUTE resultAdd = my-var + my-var2.
       SubtractTwoNumbers.
           COMPUTE resultSub = my-var2 - my-var.
       DisplayResult.
           DISPLAY "Total: " resultAdd
           DISPLAY "Difference: " resultSub.
       END PROGRAM ADD-SUBTRACT-NUMBERS.


As you can see, the PERFORM statement above begins by executing the code in AddTwoNumbers until it reaches the DisplayResult paragraph. Make no mistake: it follows the natural order of the paragraphs from start to finish.

Not surprisingly, we obtain the same output since we used the same inputs:

Total: 035
Difference: 11

This brings us to the third way by which the PERFORM verb is used in COBOL.

The PERFORM TIMES Statement

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:

Cobol




IDENTIFICATION DIVISION.
PROGRAM-ID. DISPLAY-HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
    01 my-var PIC X(6) VALUE "Hello!".
PROCEDURE DIVISION.
MAIN-PROCEDURE.
    PERFORM DisplayHello 3 TIMES
    STOP RUN.
DisplayHello.
    DISPLAY my-var.
END PROGRAM DISPLAY-HELLO.


When you run the code, you’ll find that the string “Hello!” is displayed thrice, as shown below. That’s it. This is all there is to the PERFORM TIMES statement.

Hello!
Hello!
Hello!

Of course, the next PERFORM statement includes a pre-test or post-test to decide whether the loop should terminate or not.

 The PERFORM UNTIL Statement:

Now, the PERFORM UNTIL statement acts like a while loop where the statements within the loop are executed repeatedly until a value is reached. In other words, the loop terminates as soon as a certain value is reached or condition is satisfied:

Cobol




IDENTIFICATION DIVISION.
PROGRAM-ID. DISPLAY-NUMBERS.
DATA DIVISION.
WORKING-STORAGE SECTION.
    01 MyCounter PIC 9 VALUE 1.
PROCEDURE DIVISION.
MAIN-PROCEDURE.
    PERFORM DisplayNumbers UNTIL MyCounter > 5
    STOP RUN.
DisplayNumbers.
    DISPLAY MyCounter.
    COMPUTE MyCounter = MyCounter + 1.
END PROGRAM DISPLAY-NUMBERS.


In the example above, when the MyCounter value reaches the value of 6, the loop terminates. So, for output, what you will obtain is a list of numbers from 1 to 5, as shown below:

1
2
3
4
5

In addition to this statement, you can add the WITH TEST BEFORE and WITH TEST AFTER clauses before the UNTIL word. The former acts like a regular while loop and the latter resemble that of a do-while loop in modern languages. If you must know, the WITH TEST BEFORE clause is the default and does not need to be explicitly stated.

Having said that, let us look at the PERFORM VARYING statement next.

The PERFORM VARYING Statement:

The PERFORM VARYING statement is what modern languages refer to as a for a loop. As you know, a for loop usually has a start and an end value. With each iteration, the value is incremented by a certain number until it reaches the end value. As you can see below, the PERFORM VARYING statement attempts to do just that:

Cobol




IDENTIFICATION DIVISION.
PROGRAM-ID. DISPLAY-NUMBERS.
DATA DIVISION.
WORKING-STORAGE SECTION.
    01 MyCounter PIC 9 VALUE 1.
PROCEDURE DIVISION.
MAIN-PROCEDURE.
    PERFORM DisplayNumbers VARYING MyCounter FROM 1 BY 1
    UNTIL MyCounter > 5
    STOP RUN.
DisplayNumbers.
    DISPLAY MyCounter.
END PROGRAM DISPLAY-NUMBERS.


The MyCounter value begins from 1 and increments by 1 until it reaches a value that is greater than 5. So, your output displays all the numbers from 1 to 5, as shown below:

1
2
3
4
5

As you can tell, this statement is similar to the PERFORM TIMES statement but does not require us to update the MyCounter value.

Finally, we will look at the GO TO statement and how it is used in COBOL.

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.

Conclusion:

Now, that we’ve covered loops in COBOL, it shouldn’t be hard to note that the language has options for the popular repeat, while, for, and do-while loops that we see in modern languages. This is achieved by using the all-powerful PERFORM verb. Alternatively, the GO TO verb is not used much due to the issues that crop up when it is used without a condition.

So, is there anything else you’d like to add when it comes to loops in COBOL? Please feel free to share your thoughts in the comments section below.



Last Updated : 29 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads