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.
-
Can you give some more explanation, maybe pseudo-code as well, for what exactly it is that you want? It is very unclear at the moment. For instance, `exit` means different things in different contexts, including meaning nothing at all (a no-operation), although people commonly think otherwise. – Bill Woodger Mar 16 '15 at 09:27
4 Answers
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
.

- 453
- 2
- 6
-
Just to add, for anyone reading along. Along with `EXIT PERFORM` which leaves the loop, there is also `EXIT PERFORM CYCLE` which cycles back to the top, re-evaluating any conditionals, and falling back into the loop if needs be. – Brian Tiffin Jul 28 '15 at 05:20
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.

- 10,358
- 1
- 27
- 38
Here's the syntax diagram for PERFORM
from Gary Cutler's GnuCOBOL 2.0 Programming Guide:
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.

- 12,968
- 4
- 38
- 47
You can use a "perform until" with a given condition for your exit

- 6,026
- 3
- 17
- 40
-
Does this mean it will exit as soon as the condition is met? Because I want it to exit at a paticular point in the perform, I dont want it to go through the entire perform – Benjer Mar 16 '15 at 03:38
-
It will go till the end of the perform before it checks the condition. So you will have to add control structures to skip the code you do not want to execute, or rewrite the perform structure such that your condition is only met at the end of the inner code of the perform. A nice set of examples for perform is: http://documentation.microfocus.com/help/index.jsp?topic=%2Fcom.microfocus.eclipse.infocenter.studee60ux%2FGUID-E24BCF6B-CA21-4470-B2DB-83734822B936.html – Norbert Mar 16 '15 at 03:43
-
1That link is horrible. You'd have to explain "managed COBOL" to make head or tail of it. – Bill Woodger Mar 16 '15 at 11:08