Outline Perform with Test Before Until Example


Scenario - Displaying loop iterations using outline PERFORM...WITH TEST BEFORE UNTIL.

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. PERFWTBU.
       AUTHOR. MTH.

       DATA DIVISION.                                     
       WORKING-STORAGE SECTION.                           
       01 WS-I         PIC 9(01) VALUE 1.                 

       PROCEDURE DIVISION.
           DISPLAY "Before PERFORM WITH TEST AFTER UNTIL".
           PERFORM 1000-ITERATION
              THRU 1000-EXIT
                   WITH TEST BEFORE UNTIL WS-I = 1.
           DISPLAY "After PERFORM WITH TEST AFTER UNTIL".

           STOP RUN.

       1000-ITERATION.
           DISPLAY "WS-I: " WS-I.
       1000-EXIT.
           EXIT.

Output -

Before PERFORM WITH TEST AFTER UNTIL
After PERFORM WITH TEST AFTER UNTIL 

Explaining Example -

In the above example:

  • The condition is validated before executing the satements in the loop. If the condition true, the loop gets executed. Otherwise, loop gets terminated.