Iteration groups:
in-line iteration (old, until the 80s)
block iteration (modern, containing an iteration header, the body or core, and the closing part)
All variables are static and global. That means an iteration variable will remain in memory and stay accessable with its last value after exiting the loop. Always check variable namings and intialize the proper variable before entering an iteration.
The initialization will be taken care of by the PERFORM VARYING loop.
This corresponds to the WHILE and DO WHILE/REPEAT iterations but with inverted condition in the head: the flow does not enter the core when the condition (becomes) true.
The WITH TEST BEFORE/AFTER clause is optional:
WITH TEST BEFORE (default, WHILE, the core will be executed minimum 0 times)
WITH TEST AFTER (DO-WHILE, the core will be executed minimum once)
EXAMPLE FOR PERFORM UNTIL:
This corresponds to the FOR iterations: initial value, step interval, closing condition. The condition is inverted: exits when the condition becomes true.
There can be one or multiple loop variables in the same iteration. In the latter case every variable will be increased or decreased by the given value by upon re-entering the loop core until the common condition turns to be true.
EXAMPLE FOR PERFORM VARYING:
This corresponds to a FOR iteration where the number of executions are defined directly. In case of only one statement in the core, the inline version can be used as well.
EXAMPLE FOR PERFORM TIMES:
Iteration is possible:
subscript (counter)
index (optimized, pointer-like)
EXAMPLE FOR ARRAY ITERATION:
Structured iteration: this iteration calls a whole paragraph or section (formed by more paragraphs).
Widely used for file reading.
In COBOL-74, COBOL-85, before the appearance of the block iterations it had an inline format only. In this case the iteration is a one-line parapgraph, always calling another sub-program. Due to modernalization, when the block iterations appeared, the ending . became optional.
EXAMPLE FOR PERFORM A SUB-PROGRAM:
This is a paragraph-range iteration: it executes paragraphs after each other, in order, as they are written in the source code.
Without defining the condition or repetition part, it will execute the paragraph-range once.
EXAMPLE FOR PROGRESS THRU:
This is a natural iteration for processing files. The most used iteration type.
EOF (or named as END-OF-FILE) is a level-88 variable defined to have boolean functionality, see COBOL literals for more on the topic.
EXAMPLE FOR READ AT END: