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.

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.

Structured loop control
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:
  • Set up a variable with the value that it must have when the loop is entered for the first time. This variable is called the loop control variable. In this case, THE-MULTIPLIER must start off with a value of 1 at line 003700.
  • Request a PERFORM of the loop until the variable is out of range--in this case, PERFORM CALCULATE-AND-DISPLAY (at line 003800) UNTIL THE-MULTIPLIER > 12 (at line 003900)
  • In the loop, do whatever processing is called for. At the end of the loop or after each pass through the loop, increment the loop control variable. In this case, the loop control variable is increased by 1 at line 004500
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.
Perform Varying Until
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