PERFORM TIMES
PERFORM TIMES
- PERFORM...TIMES statement is used to execute a paragraph or section a certain number of times.
- It is both inline and outline.
Syntax -
Inline PERFORM...TIMES | Outline PERFORM...TIMES |
---|---|
|
|
Parameters -
- paragraph-1 - The starting paragraph to be executed.
- THRU paragraph-2 - (Optional) If coded, indicates the end of a range of paragraphs to be executed. After executing this ending paragraph, control returns to the start of the range until the coded number of times is met.
- integer-1 - The number of times the paragraph(s) should be executed. This can be a numeric literal or a data item.
Practical Examples -
Scenario1 - Displaying loop iterations using inline PERFORM..TIMES.
Code -
----+----1----+----2----+----3----+----4----+
...
WORKING-STORAGE SECTION.
01 WS-I PIC 9(01) VALUE 1.
...
PROCEDURE DIVISION.
DISPLAY "Before Inline PERFORM...TIMES".
PERFORM 6 TIMES
DISPLAY "Iteration: " WS-I
COMPUTE WS-I = WS-I + 1
END-PERFORM.
DISPLAY "After Inline PERFORM...TIMES".
...
Output -
Before Inline PERFORM...TIMES Iteration: 1 Iteration: 2 Iteration: 3 Iteration: 4 Iteration: 5 Iteration: 6 After Inline PERFORM...TIMES
Scenario2 - Displaying loop iterations using outline PERFORM..TIMES.
Code -
----+----1----+----2----+----3----+----4----+
...
WORKING-STORAGE SECTION.
01 WS-I PIC 9(01) VALUE 1.
...
PROCEDURE DIVISION.
DISPLAY "Before Outline PERFORM...TIMES".
PERFORM 1000-CALCULATE
THRU 1000-EXIT
6 TIMES.
DISPLAY "After Outline PERFORM...TIMES".
STOP RUN.
1000-CALCULATE.
DISPLAY "Iteration: " WS-I.
COMPUTE WS-I = WS-I + 1.
1000-EXIT.
EXIT.
OUTPUT -
Before Outline PERFORM...TIMES Iteration: 1 Iteration: 2 Iteration: 3 Iteration: 4 Iteration: 5 Iteration: 6 After Outline PERFORM...TIMES