typestar

PERFORM VARYING in COBOL

The counted loop: FROM, BY, and an UNTIL test driving a sled-dog workout.

IDENTIFICATION DIVISION.
PROGRAM-ID. PERFORM-VARYING.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 LAP-NO    PIC 99.
01 LAP-SHOW  PIC Z9.
PROCEDURE DIVISION.
*> VARYING is the counted loop: FROM, BY, and an UNTIL test.
    PERFORM VARYING LAP-NO FROM 2 BY 2 UNTIL LAP-NO > 8
        MOVE LAP-NO TO LAP-SHOW
        DISPLAY "Sled dog lap" LAP-SHOW
    END-PERFORM
    DISPLAY "Cool-down walk"
    STOP RUN.

How it works

  1. PERFORM VARYING LAP-NO FROM 2 BY 2 UNTIL LAP-NO > 8 counts by twos.
  2. The MOVE to Z9 strips the leading zero before each DISPLAY.
  3. The test runs before each pass, so the loop stops cleanly after lap 8.

Keywords and builtins used here

The run, in numbers

Lines
14
Characters to type
371
Tokens
49
Three-star pace
40 tpm

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

Type this snippet

Step 2 of 3 in Loops, step 17 of 27 in Language basics.

← Previous Next →