TSO-ISPF JCL COBOL VSAM DB2 CICS Tools Articles Job Portal Forum Quiz Interview Q&A

COBOL - Perform Statements


Perform verb in COBOL is used to execute a set of COBOL statements.

There are some set of statements in a program that needs to be executed repeatedly, such as reading each record of a file up to its end. The statements in the program are running in a series until or unless if any statement executed in the flow alters the execution sequence.

PERFORM statements are used for looping in COBOL program,

PERFORM is mainly classified into two types:
  1. Inline Perform
  2. Outline Perform
Inline PerformOutline
It executes a series of statements or a block of statements between PERFORM and END-PERFORM.It also execute a series of statements or blocks of statements coded in a separate section or paragraph that are not coded in along with the PERFORM statement.
This requires no separate PARAGRAPH or SECTION that needs to be coded, which are to be executed.It requires a separate PARAGRAPH or SECTION that needs to be coded for statements that are to be executed.
Scope terminator (END-PERFORM) is mandatory.Scope terminator (END-PERFORM) is not required.
Syntax:
PERFORM
  DISPLAY 'HELLO WORLD'
END-PERFORM.
Syntax:
PERFORM MAIN-PROCESS
   THRU PROCESS-EXIT.

Let's see an simple example for PERFORM statement:

IDENTIFICATION DIVISION.
PROGRAM-ID. TSTPERFM.
PROCEDURE DIVISION.
A-PARA.
   PERFORM DISPLAY 'A-PARA'
   END-PERFORM.
   PERFORM C-PARA THRU E-PARA.

B-PARA.
   DISPLAY 'B-PARA'.
   STOP RUN.

C-PARA.
    DISPLAY 'C-PARA'.

D-PARA.
    DISPLAY 'D-PARA'.

E-PARA.
    DISPLAY 'E-PARA'.

Output:

A-PARA
C-PARA
D-PARA
E-PARA
B-PARA

Following are some of the PERFORM types depending upon their usage
  1. Inline PERFORM.

  2. Simple PERFORM.

  3. PERFORM TIMES.

  4. PERFORM UNTIL.

  5. PERFORM VARYING…. UNTIL….

  6. PERFORM THRU

  7. PERFORM WITH TEST BEFORE/TEST AFTER


Inline PERFORM:

It performs a set of Cobol statements between Perform & END-Perform. Basically this is to keep a particular logic in a boundary and execute it in a loop or number of times or execute depending upon a condition.

This is useful if that set of code is only used once in thAT program. If it is used multiple times ‘Outline perform’ is the best choice.

Example:

PERFORM
    COMPUTE TOT = MARK1+MARK2
    DISPLAY 'TOTAL MARKS' TOT
    ......
END-PERFORM

Simple PERFORM:

This is used to execute a paragraph or section.

Example:

MAIN-PARA.
   PERFORM PARA-1
   ...............
   STOP-RUN.
PARA-1.
  .........
  EXIT.

PERFORM TIMES:

PERFORM TIMES is mainly used to execute the block of statements or paragraphs/sections repetitively with the number of times specified.

Syntax:

PERFORM A-PARA 5 TIMES.

Example 1:

PERFORM 100-MAIN-PARA
   THRU 100-MAIN-PARA-X  10 TIMES.

In this example the number of times perform statement is going to be executed is predetermined. Hence this perform statement will be executed 10 times.

Example 2:

PERFORM WS-CNT
   DISPLAY WS-CNT
   SUBSTRACT 1 FROM WS-CNT
END-PERFORM.

In this example the number of times the perform statement is to be executed is not predetermined. It depends on value of the variable WS-INDX.

If I-CNT is 0 or negative, this perform statement is not all executed.

Example 3:

IDENTIFICATION DIVISION.
PROGRAM-ID. TSTPRM03.
PROCEDURE DIVISION.
A-PARA.
    PERFORM B-PARA 3 TIMES.
    STOP RUN.
B-PARA.
    DISPLAY 'B-PARA'.

Output:

B-PARA
B-PARA
B-PARA

PERFORM UNTIL:

A block of statements or a paragraph/section will be executed in the PERFORM UNTIL statement until the specified condition becomes true.
  • In the UNTIL phrase format, the procedure(s) referred to are performed until the condition specified by the UNTIL phrase is true. Control is then passed to the next executable statement following the PERFORM statement.

  • Here the condition is tested before only at the beginning of each execution by default.

  • But this default is overridden by TEST AFTER phrase. If the TEST AFTER phrase is specified, the statements to be performed are executed at least once before the condition is tested (corresponds to DO UNTIL).

  • In either case, if the condition is true, control is transferred to the next executable statement following the end of the PERFORM statement. If neither the TEST BEFORE nor the TEST AFTER phrase is specified, the TEST BEFORE phrase is assumed.
The default condition is ' WITH TEST BEFORE, 'and it specifies that the condition is tested before the execution of statements in a paragraph.

Syntax:

PERFORM A-PARA UNTIL COUNT=5

PERFORM A-PARA WITH TEST BEFORE UNTIL COUNT=5

PERFORM A-PARA WITH TEST AFTER UNTIL COUNT=5

Example 1:

MOVE 10 TO WS-CNT
PERFORM UNTIL WS-CNT = ZERO
    DISPLAY WS-CNT
    SUBSTRACT 1 FROM WS-CNT
END-PERFORM.

In the above example, the SUBTRACT statements will be executed until the WS-CNT becomes zero. Here perform will be executed 10 times.

Example 2:

IDENTIFICATION DIVISION.
PROGRAM-ID. TSTPRM02.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-CNT PIC 9(1) VALUE 0.
PROCEDURE DIVISION.
A-PARA.
    PERFORM B-PARA WITH TEST AFTER UNTIL WS-CNT>3.
    STOP RUN.
B-PARA.
    DISPLAY 'WS-CNT : 'WS-CNT.
    ADD 1 TO WS-CNT.
END-PERFORM.

Output:

WS-CNT : 0
WS-CNT : 1
WS-CNT : 2
WS-CNT : 3

PERFORM VARYING…. UNTIL….:

A block of a statement or a paragraph/section will be performed in PERFORM VARYING until the condition becomes true in the UNTIL phrase.
  • After-phrase provides for varying more than one identifier.

  • If any of the operands specified in cond-1 or cond-2 is subscripted or reference modified, the subscript or reference-modifier is evaluated each time the condition is tested.

  • When TEST BEFORE is indicated, all specified conditions are tested before the first execu ion, and the statements to be performed are executed, if at all, only when all specified tests fail. When TEST AFTER is indicated, the statements to be performed are executed at least once, before any condition is tested.

  • changing the values of identifiers and/or index-names in the VARYING, FROM, and BY phrases during execution changes the number of times the procedures are executed.
Syntax:

PERFORM A-PARA VARYING A FROM 1 BY 1 UNTIL A = 5.

Example 1:

PERFORM 1000-MAIN-PARA
   THRU 1000-MAIN-PARA-X
VARYING  WS-I FROM 1 BY 1 UNTIL WS-I > 50
  AFTER WS-J FROM 1 BY 1 UNTIL WS-J > 10.

  • In the above example the 1000-MAIN-PARA to 1000-MAIN-PARA-X will be executed 500 times.

  • First with WS-I as 1 and WS-J varying from 1 to 10 in step of 1, then WS-I as 2 and again WS-J varying from 1 to 10 and so on. Every time WS-I changes value, WS-J must vary from 1 to 10.

  • Each time the loop varying WS-J is completed, WS-J is initialized before changing the value of WS-I.

  • Thus after this perform statement is executed, value of WS-I will 51 and that of WS-J will 1 and not 11.

Example 2:

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-A PIC 9(2) VALUE 0.
PROCEDURE DIVISION.
A-PARA.
    PERFORM B-PARA VARYING WS-A FROM 2 BY 2 UNTIL WS-A=12
    STOP RUN.
B-PARA.
    DISPLAY 'B-PARA ' WS-A.
    DISPLAY 'B-PARA'.

Output:

B-PARA 02
B-PARA 04
B-PARA 06
B-PARA 08
B-PARA 10

PERFORM THRU:

It is useful to execute a continuous set of paragraphs/sections.

Example:

PERFORM PARA-1 THRU PARA-N
PARA-1.
....
..
EXIT.
PARA-2.
...
EXIT.
...
PARA-N.
...
EXIT.

PERFORM PARA-1 THRU PARA-N executes all the paragraphs between PARA-1 and PARA-N.
PERFORM WITH TEST BEFORE/TEST AFTER:

With Test Before option, program checks the condition first, if the condition false it executes the piece of code. So with Test Before there is a possibility of not executing the code for at least once.Where as in Test After(which is the default one) the code gets executes at least once.

Example:

PERFORM WITH TEST BEFORE VARYING WW-CNT1 FROM 1
          BY 1 UNTIL WW-CNT1 > WW-TRGT-CNT
....
END-PERFORM.

PERFORM PARA-1 WITH TEST BEFORE VARYING WW-CNT1
              FROM 1 BY 1 UNTIL WF-END-OF-DATA OR
                    WW-CNT1 > WW-MAX-CNT
END-PERFORM.

Tips:
  1. Use the PERFORM statement to access code which needs to be executed from more than one location.

  2. Use the PERFORM statement to structure your code. Make a main routine that PERFORMs other routines which in turn PERFORM other routines which themselves PERFORM other routines until the level of complexity of the PERFORMed routines is easily comprehended.

  3. Use the WITH TEST AFTER option to force the code to be PERFORMed at least once.

  4. Use the VARYING ... AFTER option to pass through all the entries in multiple dimensional tables.

  5. If the number of lines in the routine is relatively small, use an in-line PERFORM instead of PERFORMing a separate paragraph. The next programmer will appreciate not having to look all through the program to find the one or two lines of code in the paragraph you PERFORMed.
Perform - Overlap Rules:
  1. When the performed procedures executes another PERFORM the procedures associated with the 2nd level PERFORM must be totally included in or totally excluded from the procedures of the first level PERFORM statement.

  2. Two or more such active PERFORM must not have a common exit.
Perform (Times option) Rules:
  1. Once he perform statement is initiated any changes to the id-1 will have no effect on the number of times the Para is to be executed.

  2. If identifier-1 is zero or a negative number at the time the PERFORM statement is initiated, control passes to the statement following the PERFORM statement.

If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!

Are you looking for Job Change? Job Portal