Benjer
Benjer

Reputation: 173

Is there a way I could make a perform not end (like an infinite while loop)?

Is there anyway to do this? I want the perform to exit only when a exit statement is executed. If not I want it to keep looping.

Upvotes: 3

Views: 4143

Answers (4)

Bruce Martin
Bruce Martin

Reputation: 10543

There are three basic ways to do this in Cobol:

  • Loop unwinding - you repeat some code so the P
  • EXIT PERFORM
  • Go To

  • Using Next Sentence instead of Go To would probably work, but I do not advise this

Loop Unwinding

In the following Part-a is the code to be executed prior to the test.

Perform Part-a
Perform until condition
   .....
   Perform Part-a
end-perform

Exit Perform

    Perform until end-of-the-world
       .....
       if condition
          Exit Perform
       end-if
       ...
    end-perform

Exit-Label.
    continue.

Go To

    Perform until end-of-the-world
       .....
       if condition
          Go To Exit-Label
       end-if
       ...
    end-perform

Exit-Label.
    continue.

Upvotes: 1

Bill Woodger
Bill Woodger

Reputation: 13076

Here's the syntax diagram for PERFORM from Gary Cutler's GnuCOBOL 2.0 Programming Guide:

enter image description here

From the description which follows, point 4:

The UNTIL EXIT option will repeatedly execute the code within the scope of the PERFORM with no conditions defined on the PERFORM statement itself for termination of the repetition. It will be up to the programmer to include an EXIT PERFORM within the scope of the PERFORM that will break out of the loop.

And, not originally realising it was in a seperate point, point 5:

The FOREVER option has the same effect as UNTIL EXIT.

Pending further clarification, this may or may not be what you want.

Get hold of the relevant Programming Guide, and use it. Read. Experiment. Repeat until understood or no progress. If no progress, ask. Colleagues, GnuCOBOL Discussion area, or here.

Upvotes: 1

Valdis Grinbergs
Valdis Grinbergs

Reputation: 453

I like using "PERFORM FOREVER" because it clearly identifies the code as an infinite loop. "PERFORM UNTIL EXIT" works too. Below is some example code that uses an infinite loop and an "EXIT PERFORM" statement to print the numbers 1 to 10. This code works with GNUCobol 2.0.

   IDENTIFICATION DIVISION.
   PROGRAM-ID. INFINITE-LOOP.

   ENVIRONMENT DIVISION.
   INPUT-OUTPUT SECTION.
   FILE-CONTROL.

   DATA DIVISION.
   FILE SECTION.

   WORKING-STORAGE SECTION.
   01  COUNTER                          PIC 99 VALUE ZERO.

   PROCEDURE DIVISION.

  * USE EITHER OF THE TWO FOLLOWING LINES
  * WHICHEVER YOU FIND MORE MEANINGFUL
  *    PERFORM UNTIL EXIT
       PERFORM FOREVER
           ADD 1 TO COUNTER
           DISPLAY COUNTER
           IF COUNTER > 9
               EXIT PERFORM
           END-IF
       END-PERFORM
       STOP RUN
       .

Upvotes: 5

Norbert
Norbert

Reputation: 6084

You can use a "perform until" with a given condition for your exit

Upvotes: 0

Related Questions