Menu
CoddyTech

Inline PERFORM

Part of the Decisions and Loops section of Coddy's COBOL Programming journey. Lesson 15 of 20.

An inline PERFORM holds its statements between PERFORM and END-PERFORM, with no paragraph:

PERFORM VARYING I FROM 1 BY 1 UNTIL I > 3
    DISPLAY "Pass " I
END-PERFORM.

Every form works inline: TIMES, UNTIL and VARYING. Like END-IF, END-PERFORM closes the loop, so no period goes inside it.

A loop over input reads once before the loop and again at the end of each pass. The UNTIL test then sees each value before it is used, and the 0 that ends the input is never printed:

ACCEPT NUM
PERFORM UNTIL NUM = 0
    DISPLAY "Read " NUM
    ACCEPT NUM
END-PERFORM.

Inline loops suit a short body used once. A long body, or one that runs from several places, reads better as its own paragraph.

challenge icon

Challenge

Easy

Read N and, with an inline PERFORM VARYING, print the square of each number from 1 to N, like 02 squared is 04.

Example input, one line per ACCEPT: 3.

Try it yourself

       IDENTIFICATION DIVISION.
       PROGRAM-ID. SQUARES.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 N PIC 9.
       01 I PIC 99.
       01 SQUARE PIC 99.
       PROCEDURE DIVISION.
           ACCEPT N.
           *> Print the squares with an inline PERFORM
           STOP RUN.

All lessons in Decisions and Loops