Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 38 ยท Procedures and Control Flow

PERFORM Loops: TIMES, UNTIL and VARYING

Write loops in COBOL: PERFORM n TIMES, PERFORM UNTIL, TEST BEFORE and AFTER, PERFORM VARYING with nested counters, inline PERFORM, EXIT PERFORM and the read-until-EOF pattern.

COBOL has no for or while keywords. Every loop is a PERFORM with an extra phrase: TIMES for a fixed count, UNTIL for a condition, VARYING for a counter. The loop body is either a paragraph or, since COBOL-85, an inline block ended by END-PERFORM. This lesson covers every form and shows what each is equal to in a language you may already know.

PERFORM n TIMES

COBOL
           PERFORM 5 TIMES
               DISPLAY "Hello"
           END-PERFORM

           PERFORM PRINT-LINE 3 TIMES

The count may be a literal or a numeric data item. It is evaluated once, when the loop starts. With a paragraph, write the name first: PERFORM PRINT-LINE 3 TIMES.

PERFORM UNTIL

COBOL
           MOVE 1 TO COUNTER
           PERFORM UNTIL COUNTER > 5
               DISPLAY "Counter: " COUNTER
               ADD 1 TO COUNTER
           END-PERFORM

This behaves like while (!(COUNTER > 5)): the condition is the exit condition. The loop runs until the condition is true. By default the test is made before each pass (WITH TEST BEFORE), so the body can run zero times.

TEST AFTER

COBOL
           PERFORM WITH TEST AFTER UNTIL ANSWER = "Y" OR ANSWER = "N"
               DISPLAY "Continue? (Y/N) " WITH NO ADVANCING
               ACCEPT ANSWER
           END-PERFORM

WITH TEST AFTER checks the condition after each pass, so the body runs at least once, like do ... while. It suits prompts and input validation.

Python TEST BEFORE and TEST AFTER (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

PERFORM VARYING

VARYING adds a counter that starts, steps and stops, as in a for loop:

COBOL
           PERFORM VARYING I FROM 1 BY 1 UNTIL I > 10
               DISPLAY I
           END-PERFORM

The three parts are initial value (FROM), step (BY, may be negative or any size) and the exit condition (UNTIL). It is equal to for (i = 1; !(i > 10); i += 1). Counting down:

COBOL
           PERFORM VARYING I FROM 10 BY -1 UNTIL I < 1
               ...
           END-PERFORM

When the loop ends, the counter holds the first value that satisfied the condition (11 above), not the last one used inside the body. Remember that when you use it afterwards.

Nested counters with AFTER

AFTER adds a second counter that runs fastest, producing a nested loop without writing two PERFORMs:

COBOL
           PERFORM VARYING ROW FROM 1 BY 1 UNTIL ROW > 3
                     AFTER COL FROM 1 BY 1 UNTIL COL > 4
               DISPLAY ROW "," COL
           END-PERFORM

This produces 1,1 1,2 1,3 1,4 2,1 .... COBOL allows up to three AFTER levels (more in some compilers); this form is common for tables with several dimensions.

Python VARYING with AFTER (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Inline PERFORM versus paragraph PERFORM

Use an inline block for short bodies and a paragraph for bodies that are long or reused:

COBOL
           PERFORM 2000-PROCESS-RECORD
               UNTIL END-OF-FILE

Both forms accept TIMES, UNTIL and VARYING. Inline loops need END-PERFORM, paragraph loops do not.

EXIT PERFORM and CYCLE

Since COBOL 2002 (and in GnuCOBOL):

COBOL
           PERFORM VARYING I FROM 1 BY 1 UNTIL I > 100
               IF TABLE-ENTRY(I) = SEARCH-KEY
                   MOVE I TO FOUND-INDEX
                   EXIT PERFORM
               END-IF
           END-PERFORM

EXIT PERFORM leaves the loop (like break); EXIT PERFORM CYCLE skips to the next pass (like continue). Older IBM compilers do not have them, so older code uses a flag in the UNTIL condition (UNTIL I > 100 OR FOUND).

The read-until-end-of-file pattern

File processing almost always uses a "read ahead" loop built from the pieces above. The structure is: read the first record, loop until end of file, and read the next record at the end of each pass.

COBOL
           PERFORM 1000-READ-RECORD
           PERFORM UNTIL END-OF-FILE
               PERFORM 2000-PROCESS-RECORD
               PERFORM 1000-READ-RECORD
           END-PERFORM

Files are covered fully in the File Handling chapter; the pattern is the same one used for keyboard input and tables.

Avoiding infinite loops

A PERFORM UNTIL whose body never changes the tested variable never ends. Check that:

  1. The variable in the UNTIL condition changes inside the body.
  2. The step moves toward the end condition (a BY of 0 or the wrong sign never terminates).
  3. The counter picture is large enough: PIC 99 and UNTIL I > 99 can never be true because I wraps from 99 to 00 on overflow.

Common mistakes

  • Reading UNTIL as "while": the loop ends when the condition becomes true.
  • Expecting the counter to keep its last in-loop value.
  • Forgetting END-PERFORM on an inline loop.
  • Too-small counter pictures.
  • Forgetting the read-ahead: reading at the top of the loop processes the last record twice or an empty record.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. When does PERFORM UNTIL X > 5 stop?

    UNTIL names the exit condition. The loop stops when it becomes true.
  2. 2. Which phrase makes the body run at least once?

    WITH TEST AFTER checks the condition after the body, like do-while.
  3. 3. After PERFORM VARYING I FROM 1 BY 1 UNTIL I > 10 finishes, what is I?

    The counter was incremented to 11, which satisfied the exit condition.
  4. 4. What does the AFTER phrase of PERFORM VARYING add?

    VARYING a ... AFTER b ... makes b the inner (fastest-changing) loop.

Summary

  • Loops are PERFORM forms: n TIMES, UNTIL condition, VARYING var FROM a BY b UNTIL cond.
  • UNTIL is the exit condition. Test is before by default; WITH TEST AFTER gives do-while behaviour.
  • AFTER nests counters; the counter ends one step past the last used value.
  • EXIT PERFORM and EXIT PERFORM CYCLE are break and continue.
  • File loops use read-ahead; watch for counters that overflow.

Next: MOVE and Edited Pictures