COBOL Tutorial Series: Loop statements – Session 4

Hello, I’m Duke

In the previous 3 sessions, I have guided you on how to develop, compile, and create a simple calculation program with condition statements using COBOL.

Today, I will be giving a tutorial on loops in COBOL.

1. In-line Perform
The sta…


This content originally appeared on DEV Community and was authored by Duc Nguyen Thanh

Hello, I'm Duke

In the previous 3 sessions, I have guided you on how to develop, compile, and create a simple calculation program with condition statements using COBOL.

Today, I will be giving a tutorial on loops in COBOL.

1. In-line Perform
The statements inside PERFORM will be executed until END-PERFORM is reached.

2. Perform Varying

In perform varying, a paragraph will be executed till the condition in Until phrase becomes true.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. ProductList.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-PRODUCT-NAME        PIC X(30).
       01 WS-PRODUCT-PRICE       PIC 9(5)V99.
       01 WS-TOTAL-PRICE         PIC 9(7)V99 VALUE 0.
       01 WS-COUNTER             PIC 9(2) VALUE 1.
       01 WS-NUMBER-OF-PRODUCTS  PIC 9(2) VALUE 0.

       PROCEDURE DIVISION.
       MAIN-PROCESS.                
           DISPLAY "How many products?".
           ACCEPT WS-NUMBER-OF-PRODUCTS.

           PERFORM VARYING WS-COUNTER FROM 1 BY 1
            UNTIL WS-COUNTER > WS-NUMBER-OF-PRODUCTS
             DISPLAY "Name of product no. " WS-COUNTER ": "
             ACCEPT WS-PRODUCT-NAME 
             DISPLAY "Price of " WS-PRODUCT-NAME ": "
             ACCEPT WS-PRODUCT-PRICE

             COMPUTE WS-TOTAL-PRICE = WS-TOTAL-PRICE + WS-PRODUCT-PRICE
           END-PERFORM.             

           DISPLAY "Total price: " WS-TOTAL-PRICE.
           STOP RUN. 

  • WS-PRODUCT-NAME: This variable is used to store the product name, with a maximum of 30 characters (PIC X(30)).
  • WS-PRODUCT-PRICE: This variable is used to store the product price in a 5-digit and 2-decimal number format (PIC 9(5)V99).
  • WS-TOTAL-PRICE: This variable stores the total price of all products, initialized with the value 0. It has a 7-digit and 2-decimal format (PIC 9(7)V99).
  • WS-COUNTER: This variable is a counter, starting from 1 and has a 2-digit format (PIC 9(2)).
  • WS-NUMBER-OF-PRODUCTS: This variable stores the number of products entered by the user, initialized with the value 0
  • PERFORM VARYING: Starts a loop. This loop will continue until WS-COUNTER is greater than WS-NUMBER-OF-PRODUCTS.
  • FROM 1 BY 1: Sets the starting value of WS-COUNTER to 1 and increments it by 1 after each iteration.

and here is the result

result

Repository here


This content originally appeared on DEV Community and was authored by Duc Nguyen Thanh


Print Share Comment Cite Upload Translate Updates
APA

Duc Nguyen Thanh | Sciencx (2024-10-19T12:54:43+00:00) COBOL Tutorial Series: Loop statements – Session 4. Retrieved from https://www.scien.cx/2024/10/19/cobol-tutorial-series-loop-statements-session-4/

MLA
" » COBOL Tutorial Series: Loop statements – Session 4." Duc Nguyen Thanh | Sciencx - Saturday October 19, 2024, https://www.scien.cx/2024/10/19/cobol-tutorial-series-loop-statements-session-4/
HARVARD
Duc Nguyen Thanh | Sciencx Saturday October 19, 2024 » COBOL Tutorial Series: Loop statements – Session 4., viewed ,<https://www.scien.cx/2024/10/19/cobol-tutorial-series-loop-statements-session-4/>
VANCOUVER
Duc Nguyen Thanh | Sciencx - » COBOL Tutorial Series: Loop statements – Session 4. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/19/cobol-tutorial-series-loop-statements-session-4/
CHICAGO
" » COBOL Tutorial Series: Loop statements – Session 4." Duc Nguyen Thanh | Sciencx - Accessed . https://www.scien.cx/2024/10/19/cobol-tutorial-series-loop-statements-session-4/
IEEE
" » COBOL Tutorial Series: Loop statements – Session 4." Duc Nguyen Thanh | Sciencx [Online]. Available: https://www.scien.cx/2024/10/19/cobol-tutorial-series-loop-statements-session-4/. [Accessed: ]
rf:citation
» COBOL Tutorial Series: Loop statements – Session 4 | Duc Nguyen Thanh | Sciencx | https://www.scien.cx/2024/10/19/cobol-tutorial-series-loop-statements-session-4/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.