Menu
CoddyTech

PERFORM UNTIL

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

PERFORM ... UNTIL repeats a paragraph until a condition is true. Something in the paragraph must change the condition, or the loop never ends:

PERFORM COUNT-UP UNTIL N > 3
COUNT-UP.
    DISPLAY N.
    ADD 1 TO N.

The condition is tested before each pass. When it is already true, the paragraph runs zero times.

WITH TEST AFTER tests after each pass instead, so the paragraph always runs at least once:

PERFORM ASK-PIN WITH TEST AFTER
    UNTIL PIN-OK

A loop whose condition never comes true runs until the runner stops it, because the output grew too large or the time ran out.

challenge icon

Challenge

Easy

The program reads N. Count down to 1 with COUNT-DOWN, then print Liftoff!. For 3 the output is 3, 2, 1, Liftoff! on four lines.

Example input, one line per ACCEPT: 3.

Try it yourself

       IDENTIFICATION DIVISION.
       PROGRAM-ID. LIFTOFF.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 N PIC 9.
       PROCEDURE DIVISION.
       MAIN-PARA.
           ACCEPT N.
           *> Count down to 0
           DISPLAY "Liftoff!".
           STOP RUN.
       COUNT-DOWN.
           DISPLAY N.
           SUBTRACT 1 FROM N.

All lessons in Decisions and Loops