typestar

PERFORM TIMES in COBOL

The simplest loop: ring the ship's bell a counted number of times.

IDENTIFICATION DIVISION.
PROGRAM-ID. PERFORM-TIMES.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 BELL-COUNT  PIC 9 VALUE 6.
PROCEDURE DIVISION.
*> An inline PERFORM repeats its block a fixed number of times.
    PERFORM BELL-COUNT TIMES
        DISPLAY "ding"
    END-PERFORM
    DISPLAY "Six bells: three hours into the watch"
    STOP RUN.

How it works

  1. PERFORM BELL-COUNT TIMES repeats the block once per bell.
  2. The body is a single DISPLAY; END-PERFORM closes the loop.
  3. Six bells on the dot: the count lives in data, not in the code.

Keywords and builtins used here

The run, in numbers

Lines
12
Characters to type
315
Tokens
34
Three-star pace
45 tpm

At the three-star pace of 45 tokens a minute, this run takes about 45 seconds.

Type this snippet

Step 1 of 3 in Loops, step 16 of 27 in Language basics.

← Previous Next →