Ugly Loops

In the context of a loop, there are four kinds of GO TO statements:
  1. A GO TO which enters the loop.
  2. A recurrent GO TO -- which returns to the beginning of the loop.
  3. An exit GO TO -- which leaves the loop.
  4. An internal GO TO, branching to a paragraph within the loop, but not to the beginning of the loop.
By definition, a simple loop contains none of the first kind, exactly one each of the second and third kinds, and none of the last kind. Any deviation from this pattern makes a loop ugly.

In order to turn an ugly loop into a simple loop, you must eliminate each of the following ugliness factors, roughly in the order listed:

[button] There may be multiple entry points into the loop.
[button] The loop may span multiple paragraphs.
[button] There may be multiple recurrent GO TOs.
[button] There may be multiple exit GO TOs.
[button] Control may enter the loop by a GO TO rather than a PERFORM THRU.

Having done so, you can easily turn the remaining GO TO loop into a PERFORM UNTIL loop.


[home]COBOL Home [style forum]COBOL Style Forum [spaghetti]Spaghetti code