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.

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