Cobol

PERFORM statement in COBOL Program

PERFORM statement transfers control to a block of code that will be executed just once. This could either be a paragraph or a section. Alternatively, it could execute several blocks of code contained in a number of consecutive paragraphs by using the PERFORM THRU construct.

  • You can use the PERFORM verb to execute a block of code, a set number of times by using the PERFORM TIMES loop.
  • You can also set the PERFORM verb to execute blocks of code until a particular condition is satisfied by using the PERFORM UNTIL loop.
  • You can also set the PERFORM VARYING loop a certain number of times and depends on the FROM BY values that we use.

INLINE PERFORM Statement

If a set of statements is used only in one place then we can merge all of them within PERFORM & END-PERFORM structure and it is called INLINE PERFORM.

Syntax:
PERFORM
    <statemens>
END-PERFORM

Example:
PERFORM
  ADD ITEM-COUNT TO  ITEM-COUNT
  DISPLAY ‘TOTAL ITEM’ ITEM-COUNT 
END-PERFORM

PERFORM VARYING COUNT FROM 1 BY 1 
UNTIL COUNT > 100 OR END-OF-FILE = ‘Y’
  ADD ITEM-COUNT TO  ITEM-COUNT
  DISPLAY ‘TOTAL ITEM’ ITEM-COUNT 
  READ NEXT RECORD
END-PERFORM

PERFORM 10 TIMES
  ADD ITEM-COUNT TO  ITEM-COUNT
  DISPLAY ‘TOTAL ITEM’ ITEM-COUNT 
  READ NEXT RECORD
END-PERFORM

OUTLINE PERFORM Statement

A set of STATEMENTS merged together and placed in different and will call them using a paragraph when required is called OUTLINE PERFORM. i.e. we are calling paragraphs depending on our requirement.

  • Basic PERFORM
  • THRU phrase PERFORM
  • TIMES phrase PERFORM
  • UNTIL phrase PERFORM
  • VARYING phrase PERFORM

Basic PERFORM Phrase

This is used to execute a paragraph or section. On completion of paragraph execution, control is returned back to the next statement following PERFORM.

Syntax: PERFORM Para-Name

PERFORM TOTAL-ITEM-PARA
TOTAL-ITEM-PARA.
    ADD ITEM-COUNT TO  ITEM-COUNT
    DISPLAY ‘TOTAL ITEM’ ITEM-COUNT

PERFORM THRU Phrase

This is used to execute a set of paragraphs. On completion of paragraph execution, control is returned back to the next statement following the last PERFORM.

Syntax: PERFORM Para-Name-X THRU Para-Name-Y

PERFORM TOTAL-ITEM-PARA THRU TOTAL-AMOUNT-PARA
TOTAL-ITEM-PARA.
    ADD ITEM-COUNT TO  ITEM-COUNT
    DISPLAY ‘TOTAL ITEM’ ITEM-COUNT.
TOTAL-AMOUNT-PARA.
    ADD ITEM-AMOUNT TO  ITEM-AMOUNT
    DISPLAY ‘TOTAL AMOUNT’ ITEM-AMOUNT.

PERFORM TIMES Phrase

In ‘PERFORM TIMES’, a paragraph will be executed the number of times specified.

Syntax: PERFORM Para-Name N TIMES

PERFORM TOTAL-ITEM-PARA 5 TIMES
TOTAL-ITEM-PARA.
    ADD ITEM-COUNT TO  ITEM-COUNT
    DISPLAY ‘TOTAL ITEM’ ITEM-COUNT.

PERFORM UNTIL Phrase

PERFORM UNTIL phrase acts like a WHILE LOOP where the statements within the loop are executed repeatedly until a value is reached. In other words, the loop terminates as soon as a certain value is reached or the condition is satisfied. ‘WITH TEST BEFORE’ is the default condition and it indicates that the condition is checked before the execution of statements in a paragraph.

Syntax:
PERFORM Para-Name UNTIL COUNT=N
PERFORM Para-Name WITH TEST BEFORE UNTIL COUNT=N
PERFORM Para-Name WITH TEST AFTER UNTIL COUNT=N
  • TEST BEFORE is the default one. condition-1 will be tested before execution of statements. equivalent to DO WHILE statement in other programming langauges.
  • If TEST AFTER is used, condition-1 will be tested after one execution of statements. This is equivalent to DO UNTIL statement in other programming languages.

In either case, if the condition is true, control is transferred to the next executable statement after PERFORM statement.

PERFORM TOTAL-ITEM-PARA WITH TEST AFTER UNTIL WS-COUNT > 5.
TOTAL-ITEM-PARA.
    ADD ITEM-COUNT TO  ITEM-COUNT
    DISPLAY ‘TOTAL ITEM’ ITEM-COUNT.

PERFORM VARYING Phrase

This format is similar to the “FOR loop” of other programming languages. Like FOR loop it has a start and an end value. With each iteration, the value is incremented by a certain number until it reaches the end value. This process will continue until the condition is met and evaluated to be true. Once the condition evaluates to be true then the loop is braked and the next statement following PERFORM will be executed.

PERFORM procedure-name-1 [THRU/THROUGH procedure-name-2] [TESTBEFORE/TESTAFTER] 
        VARYING FROM BY UNTIL condition-1 
        [ AFTER FROM BY AFTER codition-2 ] 
        [ AFTER FROM BY AFTER codition-3 ] ... 

d - data-item 
I - index-name 
l - literal 

It is very similar to ( PERFORM… UNTIL ), except in this format it allows us to increase one or more data-item values automatically. 

Example: 
PERFORM TOTAL-ITEM-PARA VARIYING WS-I FROM 1 BY 2 
        UNTIL WS-I > 10 

In the above example, all statements in TOTAL-ITEM-PARA  are executed till the condition associated with UNTIL becomes true. For the first iteration, WS-I contains the value of 1 ( as it is specified after FROM ), for the second iteration WS-I value increase by 2 ( as it is specified after BY keyword) and the condition will be tested, if false statements in TOTAL-ITEM-PARA will get executed and control comes back to PERFORM, Now WS-I value increased again by 2 and condition will be tested, if it is false, statements in TOTAL-ITEM-PARA will get executed again. This loop continues till the condition becomes true.

PERFORM for Multi-dimensional arrays

This kind of PERFORM browses through each element of a multi-dimensional array.

Example:
PERFORM TOTAL-ITEM-PARA 
        VARIYING WS-I FROM 1 BY 2 
        UNTIL WS-I > 10 
        AFTER WS-J FROM 1 BY 3 
        UNTIL WS-J > 20 

In this example, in addition to WS-I, WS-J value also gets changed. For every valid value in WS-I, WS-J value start from 1 till WS-J > 20 becomes true. PERFORM statement execution ends only when WS-I > 10 becomes true. 

PERFORM TOTAL-INV-PARA THRU PARA-EXIT
        VARYING COUNT-I FROM 1 BY 1
        UNTIL COUNT-I > 3
        AFTER VARYING COUNT-J 1 BY 1
        UNTIL COUNT-J > 2.

TOTAL-INV-PARA.
    DISPLAY ‘COUNT: ’  ITEM-COUNT(COUNT-I)
    DISPLAY ‘AMOUNT: ’ ITEM-AMOUNT(COUNT-I, COUNT-J)

PARA-EXIT.
    EXIT.

COBOL Blog: Click Here IBM Reference: Click Here

Admin

Share
Published by
Admin

Recent Posts

Increase Transparency and Collaboration Product Backlog

A well-maintained product backlog is crucial for successful product development. It serves as a single…

4 days ago

Product Backlog – Incremental value to the customer

Incremental value to the customer refers to the gradual delivery of small, functional parts of…

7 days ago

Product Market, Customer’s Desire, Need, and Challenges

A Product Market refers to the group of potential customers who might be interested in…

1 week ago

PAL-EBM Professional Agile Leadership – EBM Certification

The Professional Agile Leadership - Evidence-Based Management (PAL-EBM) certification offered by Scrum.org is designed for…

3 months ago

PAL I Professional Agile Leadership Certification

The Professional Agile Leadership (PAL I) certification, offered by Scrum.org, is designed to equip leaders…

4 months ago

Scrum Master Certification: CSM, PSM, SSM

Choosing the right Scrum Master Certification depends on your current experience and career goals. If…

5 months ago