Iterations and Looping in COBOL can be achieved using ‘PERFORM’.
Tasks which needs to perform again and again in a repetitive manner can be done using ‘PERFORM’
PERFORM
There are 2 Types of PERFORM-
- In-Line perform
- Out-Line perform
In-Line PERFORM
In-Line PERFORM is used to perform the repetitive tasks within a paragraph or section itself. In other words, In-Line Perform does not call another paragraph/s or section/s.
Most used In-Line PERFORM Format/Example 1
PERFORM UNTIL test-condition
{STATEMENT/S}
END-PERFORM.
Example:
PERFORM UNTIL A > 100
ADD 1 TO A
END-PERFORM.
Most used In-Line PERFORM Format/Example 2
PERFORM WITH TEST BEFORE/AFTER UNTIL test-condition
{STATEMENT/S}
END-PERFORM.
Examples:
1. PERFORM WITH TEST BEFORE UNTIL A > 100
ADD 1 TO A
END-PERFORM.
2. PERFORM WITH TEST AFTER UNTIL A > 100
SUBTRACT 1 TO A
END-PERFORM.
Most used In-Line PERFORM Format/Example 3
PERFORM
{STATEMENT/S}
END-PERFORM.
Examples:
PERFORM
MOVE A TO B
COMPUTE X = Y + (Z*T)
END-PERFORM.
Note: This format is used rarely. It is better to use ‘GO TO’ (which is similar to In-Line perform) instead of just using this type of In-Line perform.
Out Line PERFORM
Out-Line PERFORM is used to perform the repetitive tasks by calling another paragraph/s or section/s.
Most used Out Line PERFORM Format/Example 4
PERFORM {PARA/SECTION}.
Example:
PERFORM PARA-NAME-1.
Most used Out Line PERFORM Format/Example 5
PERFORM {PARA-1/SECTION-1} THRU {{PARA-N/SECTION-N}{NUMBER} TIMES
END-PERFORM.
Here, NUMBER is an integer value.
Examples:
PERFORM PARA-NAME-1 3 TIMES
PERFORM PARA-NAME-1 THRU PARA-NAME-4 3 TIMES
Most used Out Line PERFORM Format/Example 6
PERFORM {PARA-NAME} [WITH TEST BEFORE/AFTER] UNTIL {Condition-is-true}
Examples:
PERFORM PARA-NAME-1 WITH TEST BEFORE UNTIL AGE > 10
PERFORM PARA-NAME-1 WITH TEST AFTER UNTIL AGE > 10
PERFORM PARA-NAME-1 UNTIL AGE > 10
Most used Out Line PERFORM Format/Example 7
PERFORM {PARA-NAME-1} [THRU PARA-N][WITH TEST BEFORE/AFTER]
VARYING {Index} FROM {initial_value} BY {increment_counter}
UNTIL {CONDITION}
END-PERFORM
Example:
PERFORM PARA-NAME-1 VARYING A FROM 1 BY 2
UNTIL A > 20
END-PERFORM
Most used Out Line PERFORM Format/Example 8
PERFORM {PARA-NAME-1/SECTION-1} THRU/THROUGH {PARA-NAME-N/SECTION-N}
Example:
PERFORM PARA-NAME-1 THRU PARA-NAME-3.