PERFORM statement executes the statement block repeatedly. The repetitive execution can be conditional or unconditional. If any condition is coded with it, the statements block gets executed until the coded condition is true.
The END-PERFORM is used to end the scope of the in-line PERFORM statement. It is optional if the statements under PERFORM end with a period. The period is considered as a logical end of the PERFORM statement.
PERFORM Types -
PERFORM statement is mainly two types at high level based on how it is coded to perform the task –
- Inline PERFORM
- Outline PERFORM
The below table describes the differences between inline an outline PERFORM statements –
|Inline PERFORM||Outline PERFORM|
|The PERFORM statement executes the statements block that is coded inline (between PERFORM and END-PERFORM)||The PERFORM statement executes statements block that are coded outline (in a separate paragraph or section outside of PERFORM).|
|This requires no seperate paragraph or section.||It requires a separate paragraph or seection that needs to be coded with statements block.|
|Not required to transfer the control to paragraph or section.||Control transfers to the paragraphs or sections|
|Scope terminator END-PERFORM is mandatory||Scope terminator END-PERFORM is not required|
- A PERFORM statement should not call itself and it can produce unpredictable results.
- The inline and outline PERFORM formats can't be coded together.
Different PERFORM Formats -
The PERFORM statement has five different formats to complete the tasks and those are -
- Simple PERFORM
- PERFORM...THRU or PERFORM...THROUGH
Simple PERFORM -
The simple PERFORM is a way to execute a given paragraph or section once, and control is passed to the next statement in the flow. It is both inline and outline.
|Inline PERFORM||Outline PERFORM|
- paragraph-name - The name of the paragraph we want to execute.
- section-name - The name of the section we want to execute.
Example - Simple Outline PERFORM
... PROCEDURE DIVISION. MAIN-PARA. DISPLAY 'Start of Main Paragraph'. PERFORM 1000-DISPLAY1 DISPLAY 'End of Main Paragraph'. STOP RUN. 1000-DISPLAY1. DISPLAY 'Inside DISPLAY1 Paragraph'.
The PERFORM statement is used to execute a specific paragraph or a range of paragraphs. PERFORM...THROUGH (or PERFORM...THRU) specifies a block of consecutive paragraphs to be executed. PERFORM THROUGH is outline PERFORM.
PERFORM paragraph-1 THROUGH|THRU paragraph-2
Example - Executing paragraphs using PERFORM...THRU
... PROCEDURE DIVISION. PERFORM 1000-DISPLAY1 THRU 3000-DISPLAY3. STOP RUN. 1000-DISPLAY1. DISPLAY "Paragraph1". 2000-DISPLAY2. DISPLAY "Paragraph2". 3000-DISPLAY3. DISPLAY "Paragraph3". 4000-DISPLAY3. DISPLAY "Paragraph4".
Paragraph1 Paragraph2 Paragraph3
In the above example, we have four paragraphs coded. PERFORM statement only executes 1000-DISPLAY1 THRU 3000-DISPLAY3. After 3000-DISPLAY3 execution, control returns back to PROCEDURE DIVISION.
The PERFORM...TIMES statement is used to execute a specific paragraph or section a certain number of times. PERFORM...TIMES is both inline and outline.
|Inline PERFORM...TIMES||Outline PERFORM...TIMES|
Example - Displaying loop iterations using inline PERFORM..TIMES.
... 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". STOP RUN.
Before Inline PERFORM...TIMES Iteration: 1 Iteration: 2 Iteration: 3 Iteration: 4 Iteration: 5 Iteration: 6 After Inline PERFORM...TIMES
The PERFORM...UNTIL statement is used to execute a statement block repetitively until a certain condition is true. The variable used in condition initialization, increment or decrement should be taken care separately. PERFORM UNTIL statement is both inline and outline.
|Inline PEFORM...UNTIL||Outline PEFORM...UNTIL|
- condition - The condition that determines when the loop should terminate. Once the condition becomes true, the loop gets terminated.
- WITH TEST BEFORE - tests the condition before executing the statements block. It is default with PERFORM if not coded any.
- WITH TEST AFTER - tests the condition after executing the statements block.
Example - Displaying loop iterations using inline PERFORM WITH TEST AFTER...UNTIL.
... PROCEDURE DIVISION. DISPLAY "Before Inline PERFORM WITH TEST AFTER UNTIL". PERFORM WITH TEST AFTER UNTIL WS-I = 1 DISPLAY "Iteration: " WS-I END-PERFORM. DISPLAY "After Inline PERFORM WITH TEST AFTER UNTIL". STOP RUN.
Before Inline PERFORM WITH TEST AFTER UNTIL Iteration: 1 After Inline PERFORM WITH TEST AFTER UNTIL
In the above example, the statements in the loop get executed once, and then the condition is validated. So, we have a display "Iteration: " once even though the condition is true for the first time.
The PERFORM...VARYING is used to execute a specific paragraph or section repetitively while varying the value of one or more control variables. It is both inline and outline. It is similar to PERFORM...UNTIL, but no initialization, increment, or decrement is required to code separately for the variable used in the condition.
|Inline PEFORM...VARYING||Outline PEFORM...VARYING|
Example - Displaying loop iterations using inline PERFORM...VARYING.
... PROCEDURE DIVISION. DISPLAY "Before Inline PERFORM...VARYING". PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 2 DISPLAY "Iteration: " WS-I END-PERFORM. DISPLAY "After Inline PERFORM...VARYING". STOP RUN.
Before Inline PERFORM...VARYING Iteration: 1 Iteration: 2 After Inline PERFORM...VARYING