Wednesday, February 17, 2010

7. Iterations

PERFORM statement
The simple PERFORM statement, is used to execute a specified routine from one or more points in a program.

The PERFORM statement will :
1. Execute all instructions in the named paragraph.
2. Transfer control to the next instruction in sequence, after the PERFORM.

Eg 7.1:
PROCEDURE DIVISION.
100-MAIN-MODULE.
:
PERFORM 400-HEADING-RTN.
:
200-CALC-RTN.
:
:
400-HEADING-RTN.
:
:

PERFORM..THRU
The PERFORM executes all statement beginning at pargraph-name-1 until the end of paragraph-name-2 is reached. Control is then transferred to the statement directly following the PERFORM.

Eg 7.2:
100-MAIN.
PERFORM 300-PARA THRU 500-PARA.
:
200-PARA.
:
300-PARA.
:
400-PARA.
:
500-PARA.
EXIT.
600-PARA.
:

The EXIT statement
EXIT is a COBOL reserved word that performs no operation. It is used to allow execution to pass over other statements or to transfer control back to the statement following the original PERFORM. It is used, when necessary, as an end point in a paragraph.

PERFORM..UNTIL
Iteration may be performed in COBOL using a PERFORM..UNTIL statement. The contents of the identifiers used in the UNTIL clause should be changed within the paragraph(s) being performed. The condition in a PERFORM .. UNTIL is tested before the named paragraph is executed even once. If the condition indicated in the UNTIL clause is met at the time of execution, then the name paragraph(s) will not be executed.

Procedure used in Looping
Paragraph containing the PERFORM that “Loops”
1. Initialize the field to be tested (e.g., MOVE 0 TO COUNTER1).
2. Code a PERFORM..UNTIL using a separate paragraph. For example, PERFORM UNTIL COUNTER1 = 5.

Loop to be performed
1. Code the steps required in the loop as a separated paragraph.
2. Increase or decrease the value in the field to be tested (e.g., ADD 1 TO COUNTER1).

Eg 7.3:
* This program displays Hello 3 times.
MOVE 1 TO COUNTER1.
PERFORM 200-DISP-RTN UNTIL COUNTER1 = 4.
:
STOP RUN.
200-DISP-RTN.
DISPLAY “HELLO”.
ADD 1 TO COUNTER1.

PERFORM..TIMES
The PERFORM..TIMES construct is used to execute a sequence of steps a fixed number of times. It is not necessary to establish a counter that must be incremented each time through the loop. When using the TIMES format (PERFORM paragraph-name-1 identifier-1 TIMES) : (1) the identifier must be specified in the DATA DIVISION; (2) it must have a numeric PICTURE clause; and (3) it must contain only integers or zeros.

Eg 7.4:
* This program displays the Hello 3 times.
MOVE 3 TO COUNTER1.
PERFORM 200-DISP-RTN COUNTER1 TIMES.
:
STOP RUN.
200-DISP-RTN.
DISPLAY “HELLO”.

GO TO
A GO TO permanently transfers control to another paragraph. It does not return control to the paragraph from where it was issued.

Back to COBOL Index

No comments: