Skip to content
systemcoding.dev
Mainframe

Write Loops With PERFORM UNTIL, VARYING and TIMES in COBOL

COBOL has no for or while keyword. PERFORM does all looping, with UNTIL, VARYING and TIMES forms, inline or out of line, and tests before or after each pass.

6 min read
Summarize in AI
Flat illustration of a 3270 terminal showing JCL beside a mainframe.

COBOL has no for and no while. A single verb, PERFORM, does all looping, and it also calls paragraphs, which makes it the main control statement of the language. It has several forms that look different, and a beginner who learns them as one idea finds them easy: PERFORM runs something, and optional phrases say how many times or until what. This post covers the forms you will use with Enterprise COBOL for z/OS 6.4, with a complete program, its output and the traps in each. It assumes you know the divisions and PIC clauses, and it comes before the post on tables, which depends on PERFORM VARYING.

Inline and out of line

PERFORM can run a block of statements written right after it, called inline, closed by END-PERFORM, or run a paragraph elsewhere in the program, called out of line:

cobol
           PERFORM 3 TIMES
               DISPLAY 'HELLO'
           END-PERFORM

           PERFORM SAY-HELLO 3 TIMES

The second form runs the paragraph named SAY-HELLO three times and returns to the statement after the PERFORM. Use inline for short loops and out of line when the same code is used from several places or when it is long enough to need a name.

You can run a series of paragraphs with PERFORM FIRST-PARA THRU LAST-PARA, which runs all paragraphs from the first to the last in source order. It works, but it makes code fragile, since a paragraph added in between is run as well, so most modern code performs one paragraph at a time.

The TIMES form

PERFORM n TIMES repeats a fixed number of times. The count can be a literal or a numeric field:

cobol
           MOVE 4 TO WS-N
           PERFORM WS-N TIMES
               ADD 1 TO WS-COUNT
           END-PERFORM

The count is read when the PERFORM starts, so changing WS-N inside the loop does not alter the number of passes.

The UNTIL form

PERFORM UNTIL condition repeats until the condition becomes true, testing it before each pass by default:

cobol
           PERFORM UNTIL WS-COUNT > 3
               ADD 1 TO WS-COUNT
           END-PERFORM

If the condition is already true when the loop starts, the body never runs. That is the usual behaviour of a while loop in other languages, with the condition inverted: COBOL says "until done", where other languages say "while not done".

To test after the pass instead, add WITH TEST AFTER:

cobol
           PERFORM WITH TEST AFTER UNTIL WS-COUNT > 3
               ADD 1 TO WS-COUNT
           END-PERFORM

This form runs the body at least once, like do ... while. The default is WITH TEST BEFORE, and you can write it out to make the intent plain.

The VARYING form

PERFORM VARYING is a counted loop. It sets a variable to a start value, tests a condition, runs the body and then adds a step:

cobol
           PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 5
               ADD WS-I TO WS-SUM
           END-PERFORM

FROM 1 sets the starting value, BY 1 is the step and UNTIL WS-I > 5 is the stop condition, tested before each pass. When the loop ends, the variable holds the first value that made the condition true, here 6, which surprises people who read it afterwards.

You can nest varying loops with the AFTER phrase, which acts as an inner loop for two-dimensional tables:

cobol
           PERFORM VARYING WS-ROW FROM 1 BY 1 UNTIL WS-ROW > 2
                     AFTER WS-COL FROM 1 BY 1 UNTIL WS-COL > 3
               DISPLAY WS-ROW WS-COL
           END-PERFORM

The inner variable cycles fastest.

A complete program

loops.cblcobol
       IDENTIFICATION DIVISION.
       PROGRAM-ID. LOOPS.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-I        PIC 9(2).
       01  WS-SUM      PIC 9(3) VALUE 0.
       01  WS-COUNT    PIC 9(2) VALUE 0.
       PROCEDURE DIVISION.
       MAIN-PARA.
           PERFORM 3 TIMES
               ADD 1 TO WS-COUNT
           END-PERFORM
           DISPLAY 'AFTER TIMES: COUNT=' WS-COUNT
           PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 5
               ADD WS-I TO WS-SUM
           END-PERFORM
           DISPLAY 'SUM 1..5=' WS-SUM ' I=' WS-I
           MOVE 10 TO WS-COUNT
           PERFORM WITH TEST AFTER UNTIL WS-COUNT > 5
               DISPLAY 'AT LEAST ONCE, COUNT=' WS-COUNT
               ADD 1 TO WS-COUNT
           END-PERFORM
           PERFORM SHOW-DONE
           GOBACK.
       SHOW-DONE.
           DISPLAY 'DONE'.
Text
AFTER TIMES: COUNT=03
SUM 1..5=015 I=06
AT LEAST ONCE, COUNT=10
DONE

The TIMES loop adds 1 three times. The varying loop adds 1, 2, 3, 4 and 5 for a sum of 15, and ends with WS-I at 6. The TEST AFTER loop runs once although the count is already more than 5, which shows the difference. The paragraph SHOW-DONE is out of line and is reached by the PERFORM. The final GOBACK stops the program before the program flow could run into SHOW-DONE a second time by falling through, which is why paragraphs that are only performed go after the stopping statement.

Common traps

An endless loop is the commonest bug. The UNTIL condition must be able to become true, and the body must change what it tests. A loop that reads a file until end of file, but forgets to read the next record inside the body, never ends.

A counter that is too small is another. A field PIC 9 that is varied up to 10 wraps to 0 and never exceeds 9, so UNTIL WS-I > 9 never becomes true. Make the counter at least one digit wider than the limit.

A third is performing a paragraph that falls through into the next one. A paragraph ends where the next paragraph name begins, and execution continues past it only in a plain flow. With a PERFORM, control returns after the end of the named paragraph, so the paragraphs after it do not run.

A PERFORM of a paragraph that itself performs the same paragraph, directly or indirectly, is recursion, which plain COBOL programs do not allow unless the program is marked RECURSIVE. The result of an accidental one is unpredictable, so avoid it.

Choosing the form

Use TIMES when the count is known and the loop needs no index. Use VARYING when you need the counter, for example to index a table. Use UNTIL when you loop on a condition, such as end of file. Prefer inline loops for short bodies and named paragraphs for the rest.

The statement is described in the Procedure Division chapter of the Enterprise COBOL for z/OS Language Reference.

A loop that reads a file

The most common loop in a batch program reads records until the file ends. The shape is called the priming read: read once before the loop, process the record inside the loop and read the next at the bottom of the body.

cobol
           PERFORM READ-NEXT
           PERFORM UNTIL WS-EOF = 'Y'
               PERFORM PROCESS-RECORD
               PERFORM READ-NEXT
           END-PERFORM

The paragraph READ-NEXT issues the READ and sets WS-EOF to Y in its AT END branch. Because the test is before the pass, an empty file never enters the loop, and the last record is processed before the end-of-file read sets the flag. If you put the read at the top of the body instead, the end-of-file read still runs the rest of the body once, with the previous record's data, and the last record is processed twice. That duplicate-last-record bug is one of the best-known in the language, and the priming read is its cure.

Choosing the loop variable

A loop counter should be a field of its own, with a picture wide enough for the limit plus one. A common choice for table subscripts is PIC 9(4) or PIC S9(4) COMP. The binary form is faster for subscripts, and the next posts on tables and numeric usage explain why. For now, keep the counter separate from any data field, and never change it inside a VARYING loop except through the BY clause, since the loop adds the step itself after the body.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.