Controlled Processing Loop
|
A computer is designed to do things over and over, but if it does the
same thing endlessly, the computer is limited to a single job.
In practice, a processing loop is brought to an end by some condition.
The condition is set up to be tested at the beginning of each pass
through the processing loop or at the last step in the loop.
Structured loop control
The condition is used to determine whether the processing loop should end or continue. The processing loop is the logic that is performed over and over. 003700 MOVE 1 TO THE-MULTIPLIER. 003800 PERFORM CALCULATE-AND-DISPLAY 003900 UNTIL THE-MULTIPLIER > 12. 004000 004100 CALCULATE-AND-DISPLAY. 004200 COMPUTE THE-PRODUCT = THE-NUMBER * THE-MULTIPLIER. 004300 DISPLAY 004400 THE-NUMBER " * " THE-MULTIPLIER " = " THE-PRODUCT. 004500 ADD 1 TO THE-MULTIPLIER.
The variable THE-MULTIPLIER first is set to a value of 1. The paragraph
CALCULATE-AND-DISPLAY is performed until THE-MULTIPLIER is greater than 12.
Because THE-MULTIPLIER starts with an initially correct value of 1, the
ADD 1 TO THE-MULTIPLIER logic is moved to the end of CALCULATE-AND-DISPLAY.
It is much quicker to figure out that the loop is performed with THE-MULTIPLIER ranging in value from 1 through 12. Listing above also illustrates a very common method of constructing and controlling a processing loop. These are the three steps of this construction:
|
Perform Command
003800 PERFORM CALCULATE-AND-DISPLAY 12 TIMES. 003900 004000 004100 CALCULATE-AND-DISPLAY. 004200 ADD 1 TO THE-MULTIPLIER. 004300 COMPUTE THE-PRODUCT = THE-NUMBER * THE-MULTIPLIER. 004400 DISPLAY 004500 THE-NUMBER " * " THE-MULTIPLIER " = " THE-PRODUCT.
The processing loop portion is the CALCULATE-AND-DISPLAY paragraph at
lines 004100 through 004500. This paragraph is performed over and over.
The control for the processing loop is the PERFORM 12 TIMES statement at line 003800. The condition that controls or ends the loop occurs when the paragraph has been performed 12 times. Perform Until in a Loop 003700 MOVE 1 TO THE-MULTIPLIER. 003800 PERFORM CALCULATE-AND-DISPLAY 003900 004000 UNTIL THE-MULTIPLIER > 12. 004100 004200 CALCULATE-AND-DISPLAY. 004300 COMPUTE THE-PRODUCT = THE-NUMBER * THE-MULTIPLIER. 004400 DISPLAY 004500 THE-NUMBER " * " THE-MULTIPLIER " = " THE-PRODUCT. 004600 ADD 1 TO THE-MULTIPLIER. 003700 003800 PERFORM CALCULATE-AND-DISPLAY 003900 VARYING THE-MULTIPLIER FROM 1 BY 1 004000 UNTIL THE-MULTIPLIER > 12. 004100 004200 CALCULATE-AND-DISPLAY. 004300 COMPUTE THE-PRODUCT = THE-NUMBER * THE-MULTIPLIER. 004400 DISPLAY 004500 THE-NUMBER " * " THE-MULTIPLIER " = " THE-PRODUCT. 004600 |

