|
PERFORM Statement
PERFORM Statement
Simple form of PERFORM acts very similar to GO TO statement, except that after execution
of paragraph control will return back to the next statement of PERFORM statement.
Format - 1
PERFORM procedure-name-1
Example 1 -
Below picture represents the difference between GO TO and PERFORM statements.
In above example, when we use PERFORM in the place of GO TO statement, after execution
of PARA-1, control return back to the next statement of PERFORM and continued the
execution.
Example 2 -
PERFORM PARA-1 THRU PARA-2
PARA-1 & PARA-2 are paragraph names. Execution of PERFORM statement causes control transfers
to the first statement of PARA-1. control executes all the statements from the first statement
of PARA-1 to last statement of PARA-2 and the control return to the next executable statement
of PERFORM statement. if there are any other paragraphs between PARA-1 and PARA-2, all those
statements are included for execution.
PERFORM with TIMES phrase
Format - 2
PERFORM procedure-name-1 data-item-1/literal-1 TIMES
This is very similar to what we discussed in 1st format of PERFORM, except that, here we can
specify , how many times we want to execute same set of commands.
Example - PERFORM PARA-1 THRU PARA-2 10 TIMES.
All the statements in the scope of PARA-1 thru PARA-2 are get executed 10 times. Control then
passes to the next executable statement following PERFORM statement
PERFORM with UNTIL phrase
Format - 3
PERFORM procedure-name-1
UNTIL condition-1
In this format all the statments come under scope of procedure-name-1 & procedure-name-2 are get
executed till condition-1 becomes true. It is very similar to WHILE statement in other programming
languages.
- TEST BEFORE is the default one. condition-1 will be tested before execution of statements.
equalent 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 equalent to DO UNTIL statement in other programming languages.
In either case, if condition-1 is true, control is transfered to next executable statement after
PERFORM statement.
PERFORM with VARYING phrase
Format - 4
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
This format is little complex to understand for new learners. Let us try to understand what exactly
means of this syntax. It is very similar to Format-3 ( PERFORM... UNTIL ), except in this format
it allows us to increase one or more data-item values automatically.
Example 1 - PERFORM PARA-1 VARIYING WS-I FROM 1 BY 2 UNTIL WS-I > 10
In above example , all statements in PARA-1 are executed till the codition associated with UNTIL
becomes true. For first iteration WS-I contains the value of 1 ( as it is specified after FROM ),
for second iteration WS-I value increase by 2 ( as it is specified after BY keyword) and condition
will be tested, if it false statements in PARA-1 will get executed and control come back to PERFORM,
Now WS-I value increased again by 2 and condition will be tested, if it is false, statements in
PARA-1 will get executed again. This loop continues till the condition becomes true.
PERFORM with VARYING , AFTER phrases
Example 2 - PERFORM PARA-1 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 get 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.
|
|