|
6.3 PERFORM
The PERFORM verb is one of the most important in COBOL (alongside MOVE). PERFORM has already been encountered in the Four Divisions section, where
it was used to call paragraphs from within a control paragraph. Of course, it doesn't have to be a control (or main) paragraph.
000290 PROCEDURE DIVISION.
000300 XYZ-PARAGRAPH.
000310 PERFORM FIRST-PROCESS
000320 PERFORM SECOND-PARAGRAPH
000330 STOP RUN.
:
002000 FIRST-PROCESS.
002010 [statements]
: [last statement].
In the above code, the paragraph FIRST-PROCESS is executed. When the full stop at the end of this paragraph
is encountered the logic will return to XYZ-PARAGRAPH at the next line, i.e. line 320. This is called an Out-of-Line
PERFORM.
The PERFORM verb can form the bases of a repetitive loop (or sub-routine) until a certin condition has been met.
For Example:
:
000290 PROCEDURE DIVISION.
000300 XYZ-PARAGRAPH.
000310 PERFORM COUNT-PROCESS UNTIL W-COUNTER > 10
000320 STOP RUN.
001000
002000 COUNT-PROCESS.
002010 COMPUTE W-COUNTER = W-COUNTER + 1
002020 DISPLAY 'Number of loops is ' W-COUNTER.
In the above code, COUNT-PROCESS is executed until the value of W-COUNT has reached 11.
The format for an Out-of-Line PERFORM is:
|
PERFORM [paragraph-name] UNTIL [condition]
|
An In-Line PERFORM, rather than execute a paragraph (aka procedure), allows for the repeated
execution of a series of commands. The format for an In-Line PERFORM is:
PERFORM UNTIL
{action}...
END-PERFORM
|
Example:
:
000290 PROCEDURE DIVISION.
000300 XYZ-PARAGRAPH.
000305 MOVE ZERO TO W-COUNTER
000310 PERFORM UNTIL W-COUNTER > 10
000320 COMPUTE W-COUNTER = W-COUNTER + 1
000330 DISPLAY 'This is loop number: ' W-COUNTER
000340 END-PERFORM
000350 DISPLAY 'Counter is now equal to: ' W-COUNTER
000360 STOP RUN.
END-PERFORM defines the scope of the PERFORM loop, and is a Scope terminator.
Other such scope terminators exist for other commands that will be described further on.
The code above will loop 11 times (showning numbers 1 to 11). This is because when W-COUNTER is
equal to 10, the condition (W-COUNTER) is still false. 1 is then added, and W-COUNTER is displayed
as 11, and now when W-COUNTER is tested the condition will be true and the logic will then
jump to the statement that immediately follows END-PERFORM.
This type of PEFORM tests the condition before the following statements are allowed to proceed.
Using WITH TEST can be used to define when the test is done:
:
000290 PROCEDURE DIVISION.
000300 XYZ-PARAGRAPH.
000305 MOVE ZERO TO W-COUNTER
000310 PERFORM WITH TEST AFTER UNTIL W-COUNTER > 10
000320 COMPUTE W-COUNTER = W-COUNTER + 1
000330 DISPLAY 'This is loop number: ' W-COUNTER
000340 END-PERFORM
000350 DISPLAY 'Counter is now equal to: ' W-COUNTER
000360 STOP RUN.
Now the condition is tested after the commands within the PERFORM..END-PERFORM loop has be executed once.
(WITH TEST BEFORE has same effect as initial example).
If you wanted to loop a desired number of times you could use TIMES
PERFORM 5 TIMES
COMPUTE W-NUMBER = XYZ * 3
END-PERFORM
The format is:
PERFORM {identifier or literal} TIMES
{action}...
END-PERFORM
|
To have a loop using an increment (such as a 'for..do' loop in Pascal or FOR in BASIC), the PERFORM VARYING
statement is used. The Format is:
PERFORM {paragraph-name if out-of-line} VARYING {identifier-1}
FROM {identifier-2 or literal} BY {identifier-3 or literal}
UNTIL {condition}
END-PERFORM
|
What does all that mean? Well look at the example:
:
000290 PROCEDURE DIVISION.
000300 XYZ-PARAGRAPH.
000310 PERFORM VARYING W-COUNTER FROM 1 BY 2
000320 UNTIL W-COUNTER > 10
000330 DISPLAY 'This is loop number: ' W-COUNTER
000340 END-PERFORM
000350 DISPLAY 'Counter is now equal to: ' W-COUNTER
000360 STOP RUN.
This code will display:
This is loop number: 1
This is loop number: 3
This is loop number: 5
This is loop number: 7
This is loop number: 9
Counter is now equal to: 11
This because with each loop, W-COUNTER has increased from 1 by increments of 2.
When W-COUNT was equal to 11 then the condition W-COUNTER > 10 is now true and so the
loop is exited. If you wanted to count downwards you could code:
PERFORM VARYING W-COUNTER FROM 20 BY -1
UNTIL W-COUNTER < ZERO.
The last thing to mention is PERFORM..THRU. If a program had a series of paragraphs,
just for the sake of argument, called PROCESS-1, PROCESS-2, PROCESS-3 and PROCESS-4,
then if you wished to execute these paragraphs in the order that they are written you
could code: PERFORM PROCESS-1 THRU PROCESS-4 with any out-of-line loops and conditions
you might want. Seemingly, this is not good programming practise so is generally avoided.
|