Back to previous page...
The DO WHILE ... ENDDO Construct
The functionality of this construct can be achieved by using the
following syntax :
PERFORM paragraph-name UNTIL condition.
Note : First, the condition is tested and PERFORM is performed if the test produces a logical TRUE value.
Here, the definition of "Paragraph" has to be emphasized and clarified :
A COBOL paragraph is a group of executable COBOL statements; start of which is identified
by a label and the end of which is identified by another label or end of source program.
To give you an example; in a sequence of COBOL statements in a PROCEDURE DIVISION :
....
ADD 1 TO SUB-TOTALS.
FOR-ALL-RECORDS.
MULTIPLY A BY B.
READ INPUT-FILE AT END MOVE 1 TO END-OF-FILE-REACHED.
IF VALUE-READ > 100
MOVE D TO E.
TEST-END-OF-FILE.
IF END-OF-FILE-REACHED
CLOSE INPUT-FILE
STOP RUN.
NOT-END-OF-FILE.
MOVE ZERO TO C.
.....
.....
* THIS IS THE LAST LINE OF THE SOURCE CODE FILE
In the above piece of code; the MULTIPLY, READ and IF statements are grouped
as a paragraph (paragraph name being; FOR-ALL-RECORDS); the IF block together with
the CLOSE and STOP RUn statements within its body is another paragraph (a paragraph MAY
contain only one statement; simple or compound). Finally; the last paragraph of the program
is the one named NOT-END-OF-FILE; which starts with a MOVE statement and contain all the
remaining statements in the source.
Please note that the execution order is not important when you define paragraphs. Only the
order of appearance within the source code is important.
Now the PERFORM statement makes sense.
A statement like
PERFORM FOR-ALL-RECORDS.
will cause the statements which make up the FOR-ALL-RECORDS paragraph to executed once.
Whereas, a statement like
PERFORM FOR-ALL-RECORDS 3 TIMES.
will cause the paragraph to be executed 3 times. Using variables as repetition counts is also permitted :
PERFORM FOR-ALL-RECORDS N TIMES.
If for some reason, you need to execute CONSECUTIVE paragraphs, you can use
PERFORM FOR-ALL-RECORDS THRU TEST-END-OF-FILE.
or
PERFORM FOR-ALL-RECORDS THROUGH TEST-END-OF-FILE.
If you need to execurte a paragraph or sequence of paragraphs UNTIL some condition
becomes true, you can code something like :
PERFORM FOR-ALL-RECORDS UNTIL VALUE-READ = 99.
or
PERFORM FOR-ALL-RECORDS THROUGH TEST-END-OF-FILE UNTIL VALUE-READ = 99.
This is simply the DO WHILE contruct that you will need to produce structured code.
Back to Structured Programming Elements...
Back to First Page...