COBOL Looping Statements
Iterative programming involves making a set of statements run in a repetitive, iterative, or looping manner. In iterative programming, the statements execute repeatedly until the coded condition is true. The PERFORM statement is part of iterative programming in COBOL and is also called a looping statement.
PERFORM Statement
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.
END-PERFORM -
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 -
PERFORM statement has five different formats and those are -
- Simple PERFORM Detailed Tutorial - 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.
- PERFORM...THRU
or PERFORM...THROUGH Detailed Tutorial - PERFORM...THRU statement is used to execute a specific paragraph or a range of paragraphs. It is outline PERFORM. - PERFORM...TIMES Detailed Tutorial - PERFORM...TIMES statement is used to execute a specific paragraph or section a certain number of times. It is both inline and outline.
- PERFORM...UNTIL Detailed Tutorial - PERFORM...UNTIL statement is used to execute a statement block repetitively until a certain condition is true. It is both inline and outline.
- PERFORM...VARYING Detailed Tutorial - 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.
