Day 6: Understanding Loops and Iterative Processing in COBOL Programs Using the PERFORM Statement Effectively

 


1. Introduction: Automating Repetitive Tasks in COBOL

In business applications, many tasks need to be repeated, such as processing multiple employee records, calculating monthly bills, or generating reports. Instead of writing the same code repeatedly, COBOL uses the PERFORM statement to handle loops and iterations. This not only reduces redundancy but also makes programs more readable and maintainable.

The PERFORM statement is flexible — you can execute a block of code a fixed number of times, perform conditional iterations, or even loop over a range of records. Mastering PERFORM is essential for building efficient, real-world COBOL programs.


2. Basic PERFORM Loop Syntax

A simple PERFORM loop executes a block of code a fixed number of times. Example:

PERFORM 5 TIMES DISPLAY "HELLO COBOL" END-PERFORM.
  • This prints “HELLO COBOL” five times.

  • The loop count can also be stored in a variable for dynamic iteration.

COBOL allows PERFORM…VARYING for more complex loops with counters:

PERFORM VARYING I FROM 1 BY 1 UNTIL I > 5 DISPLAY "COUNT: " I END-PERFORM.

Here, the counter I increments with each iteration until it exceeds 5.


3. Using PERFORM for Modular Code and Repeated Logic

PERFORM can also call paragraphs or sections elsewhere in the program, making code modular. For example:

PERFORM CALCULATE-SALARY END-PERFORM. CALCULATE-SALARY. COMPUTE TOTAL = BASE + BONUS DISPLAY "TOTAL SALARY: " TOTAL .

This approach lets you reuse logic without rewriting code, improving maintainability — a key strength of COBOL in enterprise applications.


🧠 Key Takeaways

  • PERFORM is COBOL’s primary mechanism for loops and iteration.

  • You can execute a block a fixed number of times or use a VARYING counter.

  • PERFORM enhances modularity by calling paragraphs or sections repeatedly.


💻 Practice Task

  1. Create a COBOL program named EmployeeSalaryLoop.cbl.

  2. Accept the number of employees as input.

  3. Use a PERFORM loop to input each employee’s name, base salary, and bonus.

  4. Compute total salary for each employee and display it.

Example structure:

IDENTIFICATION DIVISION. PROGRAM-ID. EMPLOYEE-SALARY-LOOP. DATA DIVISION. WORKING-STORAGE SECTION. 01 NUM-EMPLOYEES PIC 9(2). 01 I PIC 9(2) VALUE 1. 01 EMP-NAME PIC X(25). 01 BASE-SALARY PIC 9(6)V99. 01 BONUS PIC 9(5)V99. 01 TOTAL-SALARY PIC 9(7)V99. PROCEDURE DIVISION. DISPLAY "ENTER NUMBER OF EMPLOYEES: ". ACCEPT NUM-EMPLOYEES. PERFORM VARYING I FROM 1 BY 1 UNTIL I > NUM-EMPLOYEES DISPLAY "ENTER EMPLOYEE NAME: " ACCEPT EMP-NAME DISPLAY "ENTER BASE SALARY: " ACCEPT BASE-SALARY DISPLAY "ENTER BONUS: " ACCEPT BONUS COMPUTE TOTAL-SALARY = BASE-SALARY + BONUS DISPLAY "TOTAL SALARY FOR " EMP-NAME " IS " TOTAL-SALARY END-PERFORM. STOP RUN.

"This Content Sponsored by SBO Digital Marketing.

Mobile-Based Part-Time Job Opportunity by SBO!

Earn money online by doing simple content publishing and sharing tasks. Here's how:

  • Job Type: Mobile-based part-time work
  • Work Involves:
    • Content publishing
    • Content sharing on social media
  • Time Required: As little as 1 hour a day
  • Earnings: ₹300 or more daily
  • Requirements:
    • Active Facebook and Instagram account
    • Basic knowledge of using mobile and social media

For more details:

WhatsApp your Name and Qualification to 9994104160

a.Online Part Time Jobs from Home

b.Work from Home Jobs Without Investment

c.Freelance Jobs Online for Students

d.Mobile Based Online Jobs

e.Daily Payment Online Jobs

Keyword & Tag: #OnlinePartTimeJob #WorkFromHome #EarnMoneyOnline #PartTimeJob #jobs #jobalerts #withoutinvestmentjob"



Comments