MODULE 5 · LOOPS AND PROGRAM STRUCTURE · 2/7

PERFORM n TIMES and inline PERFORM

12 min30 XPExercise

The simplest loop in COBOL repeats something a fixed number of times.

Out-of-line: PERFORM paragraph n TIMES

       IDENTIFICATION DIVISION.
       PROGRAM-ID. TIMESDEMO.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-COPIES      PIC 9(2) VALUE 2.
       01  WS-COUNT       PIC 9(2) VALUE ZERO.
       PROCEDURE DIVISION.
       0000-MAIN.
           PERFORM 1000-PRINT-LABEL 3 TIMES
           PERFORM WS-COPIES TIMES
               ADD 1 TO WS-COUNT
               DISPLAY "COPY " WS-COUNT
           END-PERFORM
           STOP RUN.

       1000-PRINT-LABEL.
           DISPLAY "*** ACME BANK ***".

Output:

*** ACME BANK ***
*** ACME BANK ***
*** ACME BANK ***
COPY 01
COPY 02

The count can be a literal or a numeric data item. If it is zero, the body does not run at all.

Inline: PERFORM ... END-PERFORM

The second PERFORM above has no paragraph name. The statements between PERFORM and END-PERFORM are the loop body. This is an inline PERFORM, added in COBOL-85. Before that, every loop body had to be a separate paragraph.

Which should you use?

  • Inline for short bodies of a few lines, where jumping elsewhere to read the code would just slow the reader down.
  • Out-of-line when the body is long, is used from more than one place, or is a meaningful step in its own right (2100-CALC-INTEREST).

No full stops inside an inline PERFORM

A full stop ends the whole sentence, including any open PERFORM or IF. A stray . inside the body is a compile error at best, and a logic bug at worst. Put the full stop only at the end of the paragraph.

The count is read once

COBOL evaluates the TIMES count once, when the loop starts. Changing the variable inside the loop has no effect on how many times it runs:

       IDENTIFICATION DIVISION.
       PROGRAM-ID. TIMESTWO.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-N           PIC 9(2) VALUE 3.
       PROCEDURE DIVISION.
           PERFORM WS-N TIMES
               DISPLAY "N IS " WS-N
               ADD 10 TO WS-N
           END-PERFORM
           STOP RUN.

This prints N IS 03, N IS 13, N IS 23: three passes, as fixed at the start.

No built-in counter

TIMES gives you no loop variable. If you need to know which pass you are on (to number report lines, say), keep your own counter and ADD 1 to it at the top of the body, as WS-COUNT does above. When you need a counter anyway, PERFORM VARYING, covered shortly, is usually a better fit.

On the job

PERFORM n TIMES is common in report programs: skipping a fixed number of lines, writing a block of header records, or retrying something a set number of times.

Your task

A savings account earns compound interest once a year. Read three lines from input:

  1. Opening balance, e.g. 1000.00
  2. Annual interest rate in percent, e.g. 5.00
  3. Number of years, e.g. 3

For each year, add the interest to the balance (rounded to the cent) and display a line like this:

YEAR 01 BALANCE:    1050.00

The starter already reads the input and declares the fields you need. WS-BAL-OUT (PIC Z(6)9.99) formats the balance. Use PERFORM ... TIMES to run 1000-ADD-INTEREST once per year.

fixed format
Run your program to see its output here. The first visible test's input and datasets are used.
Submit to grade your program against every test.