DEV Community

Cover image for How to make loops in Common Business-Oriented Language (COBOL)
Raziq Din
Raziq Din

Posted on

How to make loops in Common Business-Oriented Language (COBOL)

Hello everyone! In this article, we will explore how to create loop statements in Common Business-Oriented Language (COBOL). Again , a couple of twists and turns when it comes to this language (that's why I write a lot about this language haha).
 
For starters, COBOL does not have direct for or while loops. Instead, it uses the PERFORM statement to handle loops. COBOL can implement loops similar to:
 

  1. Count-controlled loops (similar to for loops)
  2. Condition-controlled loops (similar to while loops)  

 

First, Let's go through Count-controlled Loops , a loop similar to For-Loop.
 

 

1. Count-Controlled Loops (For Loop)

COBOL Count-Controlled Loops

 
Let's go through the fundamental part of the code snippet!
 

1.1 User Input Field

DISPLAY "Enter a number between 1 and 10: ".
ACCEPT userInput.

Enter fullscreen mode Exit fullscreen mode

This is how we get input from the user in COBOL. The DISPLAY statement shows a message on the terminal, prompting the user to enter a value. The ACCEPT statement then reads the input from the user and assigns it to the variable userInput. In this example, userInput is defined to store integer values.

 

1.2 PERFORM VARYING STATEMENT


 PERFORM VARYING COUNTER FROM 1 BY 1 UNTIL COUNTER > userInput
    DISPLAY "Hello World " COUNTER " times."
 END-PERFORM.

Enter fullscreen mode Exit fullscreen mode

This is how we do a 'for loop' in COBOL. Below are the orders:-

 

  • PERFORM VARYING is the first statement needs to be made to make a for loop in COBOL. It is used to run a block of code (eg. this for loop) in the main program.

 

  • VARYING will introduce a loop controlled variable that will change on each iteration. In this case , VARYING will change the value on COUNTER variable on each iteration. We are technically saying "VARYING is modifying COUNTER values on each loop".

 

  • COUNTER is the loop control variable. The value of COUNTER will change on each iteration. From this snippet, COUNTER will start from 1.

 

  • FROM 1 specifies the starting value of the loop control variable. From here , COUNTER starts from 1.

 

  • BY 1 specifies the increment value for each iterations of the loop. The code explains that COUNTER increases by 1 for one iteration.

 

  • UNTIL COUNTER > userInput is the exit condition of the loop. The loop will continue until the value of COUNTER is more than userInput value that we set. After that , the loop stops.

 

  • DISPLAY "Hello World" COUNTER "times" prints the message once per loop, showing the current value of COUNTER each time, until COUNTER exceeds userInput.

 

  • END-PERFORM closes the loop and tells COBOL where the repeated statements stop in the program!

 

Output

For Loop Output
(NOTE: the Enter a number between 1 and 10: is an input field , I typed in 10 lol)

So there you have it! This is how you do a Counter-Controlled Loop , technically a For-Loop. Now , let's move on to another half of the article , a while loop or in COBOL terms, Condition-Controlled Loop!

 

2. Condition-Controlled Loop (While Loop)

While Loop

 

Pretty similar to Counter-Controlled Loop right? Well , the algorithm is pretty much the same, through identification and declaration of variables. Let's jump to the keyword that enables the program to perform a Condition -Controlled Loop.

 

2.1 PERFORM UNTIL COUNTER > 10

 PERFORM UNTIL COUNTER > 10

           DISPLAY "Counter count: " COUNTER

           ADD 1 TO COUNTER

           END-PERFORM.

Enter fullscreen mode Exit fullscreen mode

A definite explanation is down below!:

 

  • UNTIL is a unique keyword to enable COBOL program to perform Condition-Controlled Loop. This keyword is used to compare the current value of COUNTER to the value 100. In this case , we made a condition that the operation will perform the display message until the value of COUNTER is more than 100. If COUNTER value is more than the value 100, the loop terminates and display operation will stop.

 

Output

While Loop Output

This is the only difference between these 2 loops in COBOL!

 

Key Takeaways

  • COBOL doesn’t have for or while keywords, but PERFORM covers both use-cases cleanly.

  • Choose PERFORM VARYING when your loop is strictly numeric and predictable.

  • Choose PERFORM UNTIL when the loop should continue until some business rule or external condition is satisfied.

 

Thank you very much for your time to read this! Stay tuned for more COBOL series and share us your thoughts and recommendations to further improve this series!

Top comments (0)