MODULE 7: COBOL Control statements
PERFORM WITH TEST BEFORE/AFTER
- Syntax:-
PERFORM [para-1 [{THRU/THROUGH} para-n]] [WITH TEST {BEFORE/AFTER}] UNTIL condition-1 [statement-block END-PERFORM]Where,
- para-1, para-n are paragraph names which we want to call
- condition-1 is any conditional-expression which can return either true or false
- [statement-block END-PERFORM] is used for Inline PERFORM.
- When WITH TEST BEFORE is coded,
- Condition is checked first, if it evaluates to false the paragraphs are executed and then repeats process until condition is met (evaluates to true).
- This is default option.
- It is similar to WHILE…DO of the other language
- When WITH TEST AFTER is coded,
- It first executes paragraphs, then evaluates the conditional expression. If this condition evaluates to false, the paragraphs will be executed again. Otherwise, it terminates and control is returned to next statement after PERFORM
- Similar to REPEAT… UNTIL
- Below flow diagram will help you understand the difference between “WITH TEST BEFORE” and “WITH TEST AFTER”
- Example:-
In PROCUEDURE DIVISION,PARA-0. DISPLAY ‘IN PARA-0’. MOVE 1 TO WS-COUNT. PERFORM PARA-1 THRU PARA-1-EXIT WITH TEST AFTER UNTIL WS-INDEX > 3. DISPLAY ‘IN PARA-0 AGAIN’ STOP RUN. PARA-1. DISPLAY ‘IN PARA-1 WS-COUNT:’ WS-COUNT PARA-1-EXIT. EXIT.SYSOUT display (output):-IN PARA-0 IN PARA-1 WS-COUNT: 1 IN PARA-1 WS-COUNT: 2 IN PARA-1 WS-COUNT: 3 IN PARA-0 AGAIN
- In above example, if we had used WITH TEST BEFORE the output would have been (observe ‘IN PARA-1 WS-COUNT: 3’ is not displayed below):-
IN PARA-0 IN PARA-1 WS-COUNT: 1 IN PARA-1 WS-COUNT: 2 IN PARA-0 AGAIN