Iterative Statements in COBOL

Introduction

  • Iterative statements are also known as loop statements.
  • The loop statements are required for repeated execution of the same task based on some requirement.

Loop statement

Following loop statements are used in COBOL:

1. PERFORM THRU

  • Using PERFORM THRU, a series of paragraph is executed by giving the first and last paragraph names in the sequence.
  • The function is returned after executing the last paragraph.
In-line perform
The statements inside the PERFORM are executed till the END-PERFORM  is reached.

Syntax for In-line perform:
PERFORM
      DISPLAY 'HELLO WORLD'
END-PERFORM

Out-of-line Perform
In Out-of-line Perform, a statement in one paragraph is executed and then the control is transferred to another paragraph.

Syntax for Out-of-line perform:
PERFORM PARAGRAPH1 THRU PARAGRAPH2

Example : Program to demonstrate PERFORM THRU

IDENTIFICATION DIVISION.
PROGRAM-ID. PT.

PROCEDURE DIVISION.
   PARA-A.
   PERFORM DISPLAY 'IN PARA-A'
   END-PERFORM.
   PERFORM PARA-C THRU PARA-E.
   
   PARA-B.
   DISPLAY 'IN PARA-B'.
  
   STOP RUN.
  
   PARA-C.
   DISPLAY 'IN PARA-C'.
  
   PARA-D.
   DISPLAY 'IN PARA-D'.
   
   PARA-E.
   DISPLAY 'IN PARA-E'.
   
    PARA-F.
   DISPLAY 'IN PARA-F'.


Output:
IN PARA-A
IN PARA-C
IN PARA-D
IN PARA-E
IN PARA-B

2. PERFORM UNTIL

  • Until the given condition becomes true, a paragraph is executed in PERFORM UNTIL.
  • TEST BEFORE is the default condition. Before the execution of statements in a paragraph, the given condition will be tested.
  • TEST BEFORE is similar to DO WHILE statement in other programming language.
  • TEST AFTER is tested after the execution of the program.
Syntax:
PERFORM PARA-A UNTIL COUNT=5
PERFORM PARA-A WITH TEST BEFORE UNTIL COUNT=5
PERFORM PARA-A WITH TEST AFTER UNTIL COUNT=5

Example : Program to demonstrate PERFORM UNTIL

IDENTIFICATION DIVISION.
PROGRAM-ID. PU.

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

PROCEDURE DIVISION.
   PARA-A.
   PERFORM PARA-B WITH TEST AFTER UNTIL CNT>4.
   STOP RUN.
   
   PARA-B.
   DISPLAY 'CNT : 'CNT.
   ADD 1 TO CNT.


Output:
CNT : 1
CNT : 2
CNT : 3
CNT : 4

3. PERFORM TIMES

'PERFORM TIMES' tells the number of times a paragraph should be executed.

Syntax:
PERFORM A-PARA 5 TIMES

Example : Program to demonstrate PERFORM TIMES

IDENTIFICATION DIVISION.
PROGRAM-ID. PT.

PROCEDURE DIVISION.
   PARA-A.
   PERFORM PARA-B 4 TIMES.
   STOP RUN.
   
   PARA-B.
   DISPLAY 'This is para B'.


Output:
This is para B
This is para B
This is para B
This is para B

4. PERFORM VARYING

In PERFORM VARYING, a statement in paragraph will be executed till the condition in UNTIL is true.

Syntax:
PERFORM PARA-A VARYING A FROM 1 BY 1 UNTIL A=5.

Example : Program to demonstrate PERFORM VARYING

IDENTIFICATION DIVISION.
PROGRAM-ID. PV.

DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 WS-A PIC 9(2) VALUE 0.

PROCEDURE DIVISION.
   PARA-A.
   PERFORM PARA-B VARYING WS-A FROM 2 BY 2 UNTIL WS-A=12
   STOP RUN.
   
   PARA-B.
   DISPLAY 'IN PARA-B ' WS-A.


Output:
IN PARA-B 02
IN PARA-B 04
IN PARA-B 06
IN PARA-B 08
IN PARA-B 10