In this section, we will explore the various looping constructs available in COBOL. Looping constructs are essential for executing a block of code multiple times, which is a common requirement in programming. We will cover the following topics:

  1. PERFORM UNTIL
  2. PERFORM VARYING
  3. PERFORM TIMES

  1. PERFORM UNTIL

The PERFORM UNTIL loop executes a block of code repeatedly until a specified condition is met.

Syntax

PERFORM
    [paragraph-name]
    UNTIL [condition]

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. PerformUntilExample.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 COUNTER PIC 9(2) VALUE 1.

PROCEDURE DIVISION.
    PERFORM UNTIL COUNTER > 10
        DISPLAY 'Counter: ' COUNTER
        ADD 1 TO COUNTER
    END-PERFORM.
    STOP RUN.

Explanation

  • COUNTER is initialized to 1.
  • The PERFORM UNTIL loop will execute the block of code until COUNTER is greater than 10.
  • Inside the loop, the current value of COUNTER is displayed, and then COUNTER is incremented by 1.

  1. PERFORM VARYING

The PERFORM VARYING loop is used to iterate over a range of values, similar to a for loop in other programming languages.

Syntax

PERFORM
    [paragraph-name]
    VARYING [index-variable] FROM [start-value] BY [increment-value]
    UNTIL [condition]

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. PerformVaryingExample.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 INDEX PIC 9(2).

PROCEDURE DIVISION.
    PERFORM VARYING INDEX FROM 1 BY 1 UNTIL INDEX > 10
        DISPLAY 'Index: ' INDEX
    END-PERFORM.
    STOP RUN.

Explanation

  • INDEX is used as the loop variable.
  • The PERFORM VARYING loop starts with INDEX set to 1 and increments it by 1 each iteration.
  • The loop continues until INDEX is greater than 10.
  • Inside the loop, the current value of INDEX is displayed.

  1. PERFORM TIMES

The PERFORM TIMES loop executes a block of code a specified number of times.

Syntax

PERFORM
    [paragraph-name]
    TIMES [number-of-times]

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. PerformTimesExample.

DATA DIVISION.
WORKING-STORAGE SECTION.

PROCEDURE DIVISION.
    PERFORM 5 TIMES
        DISPLAY 'Hello, COBOL!'
    END-PERFORM.
    STOP RUN.

Explanation

  • The PERFORM TIMES loop will execute the block of code 5 times.
  • Each iteration, the message "Hello, COBOL!" is displayed.

Practical Exercises

Exercise 1: Using PERFORM UNTIL

Write a COBOL program that uses a PERFORM UNTIL loop to display the numbers from 1 to 20.

Solution

IDENTIFICATION DIVISION.
PROGRAM-ID. Exercise1.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 COUNTER PIC 9(2) VALUE 1.

PROCEDURE DIVISION.
    PERFORM UNTIL COUNTER > 20
        DISPLAY 'Number: ' COUNTER
        ADD 1 TO COUNTER
    END-PERFORM.
    STOP RUN.

Exercise 2: Using PERFORM VARYING

Write a COBOL program that uses a PERFORM VARYING loop to display even numbers from 2 to 20.

Solution

IDENTIFICATION DIVISION.
PROGRAM-ID. Exercise2.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 INDEX PIC 9(2).

PROCEDURE DIVISION.
    PERFORM VARYING INDEX FROM 2 BY 2 UNTIL INDEX > 20
        DISPLAY 'Even Number: ' INDEX
    END-PERFORM.
    STOP RUN.

Exercise 3: Using PERFORM TIMES

Write a COBOL program that uses a PERFORM TIMES loop to display "Learning COBOL!" 10 times.

Solution

IDENTIFICATION DIVISION.
PROGRAM-ID. Exercise3.

DATA DIVISION.
WORKING-STORAGE SECTION.

PROCEDURE DIVISION.
    PERFORM 10 TIMES
        DISPLAY 'Learning COBOL!'
    END-PERFORM.
    STOP RUN.

Common Mistakes and Tips

  • Off-by-One Errors: Ensure that your loop conditions are correctly set to avoid off-by-one errors.
  • Infinite Loops: Always make sure that the loop condition will eventually be met to avoid infinite loops.
  • Variable Initialization: Initialize your loop variables properly before entering the loop.

Conclusion

In this section, we covered the three primary looping constructs in COBOL: PERFORM UNTIL, PERFORM VARYING, and PERFORM TIMES. These constructs allow you to execute blocks of code multiple times under different conditions. By practicing the provided exercises, you should now have a solid understanding of how to implement loops in COBOL. In the next section, we will delve into nested control structures, which will build upon the concepts learned here.

© Copyright 2024. All rights reserved