Let's understand Mainframe
Home Tutorials Interview Q&A Quiz Mainframe Memes Contact us About us

MODULE 7: COBOL Control statements


PERFORM VARYING

  • This format is similar to “FOR loop” of other programming language
  • Syntax:-

    PERFORM [para-1 [{THRU/THROUGH} para-n]]       VARYING id-1 FROM id-2       BY id-3 UNTIL condition-1       [statement-block  END-PERFORM]

    Where,
    • para-1, para-n are paragraph names which we want to call
    • id-1 can be numeric data item or index data item
    • id-2 can be numeric data item or index data item or literal
    • id-3 can be numeric data item or literal
    • condition-1 is any conditional-expression which can return either true or false
    • [statement-block END-PERFORM] is used for Inline PERFORM.
  • The PERFORM in this format work like this:-
    • First initializes id-1 with id-2. The condition is checked.
    • If the condition is false, execute the para-1 through para-n and then increment the id-1 by id-3 and again test the condition(s)
    • If the condition evaluates to false again, then again perform para-1 though para-n.
    • Repeat this process until the condition is met and evaluates to true. Once condition evaluates to true then loop is braked and the next statement following PERFORM will be executed
  • Example:-
    In PROCUEDURE DIVISION,

    PARA-0. DISPLAY 'IN PARA-0'. DISPLAY 'WS-INDEX: ' WS-INDEX. PERFORM PARA-1 THRU PARA-1-EXIT VARYING WS-INDEX FROM 1 BY 1 UNTIL WS-INDEX = 3. DISPLAY 'IN PARA-0 AGAIN' STOP RUN. PARA-1. DISPLAY 'IN PARA-1 WS-INDEX:' WS-INDEX PARA-1-EXIT. EXIT.

    SYSOUT display (output):-

    IN PARA-0 WS-INDEX: 10 IN PARA-1 WS-INDEX: 1 IN PARA-1 WS-INDEX: 2 IN PARA-0 AGAIN

  • In above example, if you observe, before PERFORM statement is executed, WS-INDEX was having value 10 but when PERFORM statement is executed for the first time it initialized WS-INDEX to 1 and since condition evaluates to false, it performed PARA-1. In second time it incremented WS-INDEX by 1 and then again called PARA-1 because condition ‘WS-INDEX =3’ evaluates to false. After that, again WS-INDEX incremented by 1 but this time condition is met and thus PARA-1 is not called and control is transferred to next statement following PERFORM



Advertisements






© copyright mainframebug.com
Privacy Policy