Back to previous page...

The FOR ... ENDFOR Construct

The FOR ... ENDFOR functionality is achieved by the following syntax :
PERFORM paragraph-name VARYING variable-1 FROM start-value 
                              BY incr-value UNTIL condition.
or
PERFORM parag-first THRU parag-last VARYING variable-1 FROM start-value 
                              BY incr-value UNTIL condition.
Note : For a definition of the PARAGRAPH concept, you can have a look at DO UNTIL page.

The following statement will perform the indicated paragraph 10 times, but at each loop cycle, the value of variable K will be incremented by one:
      PERFORM VALIDITY-TEST VARYING K FROM 1 BY 1 UNTIL K > 10.
      ...
      ...
  VALIDITY-TEST.
      IF ARRAY-OF-VALUES (K) > 0
         ADD 1 TO VALID-ITEM-COUNT
         ADD ARRAY-OF-VALUES(K) TO SOME-VARIABLE
         DISPLAY "A VALID ENTRY FOUND".
  QUALITY-TEST.
      ....
      ....
  
An example as the following perfectly makes sense if you need to execute validity, color, weight and quality tests for the first 10 elements of the array.
      PERFORM VALIDITY-TEST THRU QUALITY-TEST VARYING K 
                            FROM 1 BY 1 UNTIL K > 10.
      ...
      ...
  VALIDITY-TEST.
      IF ARRAY-OF-VALUES (K) > 0
         ADD 1 TO VALID-ITEM-COUNT
         ADD ARRAY-OF-VALUES(K) TO SOME-VARIABLE
         DISPLAY "A VALID ENTRY FOUND".
  COLOR-TEST.
      ....
      ....
  WEIGHT-TEST.
      ....
      ....
  QUALITY-TEST.
      ....
      ....
  

This is simply the FOR .. ENDFOR contruct that you will need to produce structured code.


Back to Structured Programming Elements...

Back to First Page...