HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to COBOLVariablesArithmeticConditions (IF)Loops (PERFORM)
REFERENCE
📖 Overview
🖥️ Structure & Output
📦 Variables
🔢 Arithmetic
🔀 Condition
🔁 Loop
COBOL›Loops (PERFORM)

Loops (PERFORM)

Counter-controlled loop with PERFORM VARYING.

Simple enough for a 5-year-old to understand

PERFORM VARYING

Loops by starting i from FROM, incrementing by BY until the UNTIL condition is met.

Example code
1
IDENTIFICATION DIVISION.
2
PROGRAM-ID. H.
3
DATA DIVISION.
4
WORKING-STORAGE SECTION.
5
01 I PIC 9.
6
PROCEDURE DIVISION.
7
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 3
From 1, until I>3.
LoopFrom 1, until I>3.
8
DISPLAY I
1, 2, 3
Output / Input1, 2, 3
9
END-PERFORM.
End of loop.
LoopEnd of loop.
10
STOP RUN.
LoopOutput / Input
💡 Hint: UNTIL I > 3 → prints 1,2,3 (stops when condition is met). PERFORM 3 TIMES also exists.

About this topic

Iteration in COBOL is done with a single verb, and this verb has several forms: repetition for a specific count, repetition until a condition is met, and repetition via a counter. All of them start with the same keyword.

This verb is also used to call another paragraph; in COBOL, there is a single common mechanism for calling subroutines and creating loops. Combining the call with the loop makes it possible to run a paragraph repeatedly.

The loop condition is written in the "until" format: the loop stops when the condition becomes true. This is the reverse of the "while" logic in many languages, and writing the condition backwards is the most common mistake.

What you will learn

  • Repetition for a specific count
  • Repetition until a condition
  • The counter-based repetition form
  • Inline and paragraph calling
  • The timing of the condition test

Who is this for?

  • Batch processing code writers
  • Those dealing with infinite loops
  • Those tracing the flow in legacy code

When to use?

In batch jobs reading files record by record, the repetition-until format is the standard pattern; the end-of-file indicator is used as the condition.

If the number of repetitions is known, the numeric repetition format is the shortest and most readable option; if a counter is needed, the counter-based format is used.

Common mistakes

Wrong

01 WS-I PIC 9(2) VALUE 1.
PROCEDURE DIVISION.
    PERFORM UNTIL WS-I < 5
        DISPLAY WS-I
        ADD 1 TO WS-I
    END-PERFORM.
    STOP RUN.

The condition is written backwards: the loop stops when the condition is true, and here, since the condition is already true on the first iteration, the body never runs. If the intention is to run it five times, you need to write the reverse of the condition. This logical inversion is the most common mistake in COBOL loops.

Correct

01 WS-I PIC 9(2) VALUE 1.
PROCEDURE DIVISION.
    PERFORM UNTIL WS-I > 5
        DISPLAY WS-I
        ADD 1 TO WS-I
    END-PERFORM.
    STOP RUN.

Wrong

01 WS-I PIC 9(2) VALUE 1.
PROCEDURE DIVISION.
    PERFORM UNTIL WS-I > 5
        DISPLAY WS-I
    END-PERFORM.
    STOP RUN.

Because the counter is not incremented, the condition is never met, and the loop goes on infinitely. In batch processing, this means the job loops for hours and consumes resources.

Correct

01 WS-I PIC 9(2) VALUE 1.
PROCEDURE DIVISION.
    PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 5
        DISPLAY WS-I
    END-PERFORM.
    STOP RUN.

Wrong

01 WS-SAYAC PIC 9(2) VALUE ZERO.
PROCEDURE DIVISION.
    PERFORM UNTIL WS-SAYAC >= 0
        DISPLAY 'I expect it to run at least once'
        ADD 1 TO WS-SAYAC
    END-PERFORM.
    STOP RUN.

The condition is tested before the body by default; since it is already true at the start, the body never runs. If you want the body to run at least once, you must explicitly specify that the test will be done afterwards.

Correct

01 WS-SAYAC PIC 9(2) VALUE ZERO.
PROCEDURE DIVISION.
    PERFORM WITH TEST AFTER UNTIL WS-SAYAC >= 0
        DISPLAY 'ran at least once'
        ADD 1 TO WS-SAYAC
    END-PERFORM.
    STOP RUN.

Frequently asked questions

What is the difference between an inline loop and a paragraph call?

In the inline format, the body is written inside the loop and remains local. In a paragraph call, the body is defined elsewhere and can be used from multiple places. Logic to be reused is written in a paragraph, and local repetition is written inline.

Can I exit a loop early?

A standard exit statement exists in modern versions, but the common pattern in legacy code is to use a flag variable and include it in the loop condition. Jumping out with a branch command is not recommended because it makes the flow untraceable.

How do you write a nested loop?

In the counter-based format, you can write two levels in a single statement by defining a second counter; or the loops are nested inside each other. When processing two-dimensional tables, the first format is short but hard to read; nested writing is generally clearer.

Summary

Several forms of repetition are written with a single verb.

The loop stops when the condition becomes true.

Forgetting to increment the counter produces an infinite loop.

The test is performed before the body by default.

The same verb is used for a paragraph call.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code