COBOL Perform Types and Usage with examples
Perform Verb in COBOL is used to execute a set of COBOL statements. There exists different types of performs based on the way that is being used. It is vital in keeping a cobol program structured by making it possible to call different paragraphs or sections from another paragraph or section.
Following are some of the PERFORM types depending upon their usage
1. Inline perform.
2. Simple Perform.
3. PERFORM VARYING.
4. PERFORM VARYING…. UNTIL….
5. PERFORM THRU
6. PERFORM TIMES
Inline Perform:
It performs a set of Cobol statements between Perform & END-Perform. Basically this is to keep a particular logic in a boundary and execute it in a loop or number of times or execute depending upon a condition. This is useful if that set of code is only used once in thAT program. If it is used multiple times ‘Outline perform’ is the best choice.
Ex:
PERFORM COMPUTE AMT = SAL+EXT-INCME DISPLAY 'TOTAL INCOME' AMT ...... END-PERFORM
Ex: Inline perform based on a condition
PERFORM VARYING COUNT FROM 1 BY 1 UNTIL COUNT > 100 ............ END-PERFORM
In the above perform the loop executes until the count reaches 100, after that it comes out the loop.
Inline perform can also be performed number of times depending upon the requirement.
Ex:
PERFORM 10 TIMES -------- ADD AMOUNT = AMOUNT + INP GET INP ------------ END-PERFORM
Simple Perform:
This is used to execute a paragraph or section.
Ex: MAIN-PARA. PERFORM PARA-1 ............... STOP-RUN. PARA-1. ......... <some logic> EXIT.
PERFORM VARYING:
This format can be used both in inline and out line performs.
It is useful to limit the execution of a set of statements depending on any condition.
Ex: inline perform
PERFORM VARYING WW-COUNT1 FROM 1 BY 1 UNTIL WW-COUNT1 > WW-MAX-COUNT ....... END-PERFORM.
Ex: For Outline perform
PERFORM PARA-1 VARYING WW-COUNT1 FROM 1 BY 1 UNTIL WW-COUNT1 > WW-MAX-COUNT
Here it also gives the possibility to
– give the initial value for the WW-COUNT1
– Increment the ww-count1 by any other value
Ex:
PERFORM PARA-1 VARYING WW-COUNT1 FROM 10 BY 2 UNTIL WW-COUNT1 > WW-MAX-COUNT
Here COUNT1’s initial value is 10 and each time it gets incremented by 2.
PERFORM THRU :
It is useful to execute a continuous set of paragraphs/sections.
Ex: PERFORM PARA-1 THRU PARA-N PARA-1. .... .. EXIT. PARA-2. ... EXIT. ... pARA-N. ... EXIT. PERFORM PARA-1 THRU PARA-N executes all the paragraphs between para-1 and para-n.
Should be cautious to use ‘perform thru’ as it executes any new paragraph added in between these para’s.
Ex: PERFORM PARA-1 THRU PARA-1-EXIT. PARA-1. ... PARA-1-EXIT. EXIT.
Perform with TIMES:
It can be used to execute a piece of code ‘n’ number of times.
ex: PERFORM PARA-1 10 TIMES Ex: PERFORM 10 TIMES ... END-PERFORM.
PERFORM WITH TEST BEFORE/TEST AFTER:
With the Test Before option, the program checks the condition first and if the condition is false then it executes the piece of code. So with Test-Before, there is a possibility of not executing the code at least once. Whereas in Test After, the code gets executed at least once. It first executes the piece of code and then checks the condition and if the condition is met, control comes out of the loop. If nothing is specified, then the default one is TEST BEFORE.
Ex:
PERFORM WITH TEST BEFORE VARYING WW-CNT1 FROM 1 BY 1 UNTIL WW-CNT1 > WW-TRGT-CNT .... END-PERFORM. PERFORM PARA-1 WITH TEST BEFORE VARYING WW-CNT1 FROM 1 BY 1 UNTIL WF-END-OF-DATA OR WW-CNT1 > WW-MAX-CNT
Oui Perform A100 thru A100-exit varrying count from 6 by -1 until count < 1
Should -1 be a problem?
No, It is not a problem. You can code in that way.