perform command

PERFORM

Command:

PERFORM

Syntax:

Variant 1, Standard PERFORM:

PERFORM <module-name>.

Variant 2, PERFORM .. UNTIL:

PERFORM <module-name> UNTIL <condition>.

Variant 3, Inline PERFORM:

PERFORM UNTIL <condition>

:

:

END-PERFORM

Description:

The basic PERFORM statement has three variants in CobolScript:

Variant 1, Standard PERFORM:

The standard PERFORM passes program control to a program module module-name a single time, and then returns control to the statement following the PERFORM. When the PERFORM is encountered during program execution, control passes immediately to the first line of code within module-name. The code within module-name then executes; after the last statement in module-name has been processed, control is returned to the line immediately following the PERFORM statement, and program execution continues normally.

Variant 2, PERFORM .. UNTIL:

PERFORM .. UNTIL is used to pass program control to a program module module-name multiple times, until condition is satisfied. Execution of the code within module-name is similar to the standard PERFORM.

When a PERFORM .. UNTIL statement is encountered during program execution, condition is immediately evaluated; if it evaluates to FALSE, module-name is executed, and control returns to the beginning of the PERFORM .. UNTIL statement, so that condition can be evaluated again. If condition evaluates to TRUE, module-name is not executed, and control passes to the statement following the PERFORM .. UNTIL.

There are two important points to keep in mind when using PERFORM .. UNTIL:

· First, remember that condition must evaluate to TRUE in order for control to be passed to the statement following the PERFORM .. UNTIL; if condition always evaluates to FALSE, the program will be caught in an endless loop, repeatedly performing the code in module-name. To avoid this, some of the code within module-name must change some component of condition, so that condition will eventually be TRUE.

· Second, remember that condition is always evaluated prior to the execution of module-name. Therefore, if condition evaluates to TRUE the first time that the PERFORM .. UNTIL is encountered, the code in module-name will never be performed.

More information on conditions, condition evaluation, and permitted condition syntax is available in the Command Reference entry for IF, and in the Expressions and Conditions section in Chapter 3, CobolScript Language Constructs.

Variant 3, Inline PERFORM:

The Inline PERFORM is simply a variation of PERFORM .. UNTIL. Instead of performing a separate module, however, it executes the code that is between the PERFORM and END-PERFORM statements multiple times, until condition is satisfied.


Example Usage:

Variant 1:

PERFORM INIT.

Variant 2:

PERFORM PROCESSING UNTIL counter = 5.

Variant 3:

PERFORM UNTIL counter = 5

ADD 1 TO counter

DISPLAY `counter = ` & counter

END-PERFORM

See Also:

IF (for explanation of condition evaluation, PERFORM .. VARYING

Sample Program:

PERFORM.CBL

PERFORM .. VARYING

Command:

PERFORM .. VARYING

Syntax:

Variant 1, Standard PERFORM .. VARYING:

PERFORM <module-name> VARYING <varying-variable>

FROM <from-amount> BY <increment-amount> UNTIL <condition>.

Variant 2, Inline PERFORM VARYING:

PERFORM VARYING <varying-variable>

FROM <from-amount> BY <increment-amount> UNTIL <condition>

:

:

END-PERFORM

Description:

PERFORM .. VARYING has two variants in CobolScript:

Variant 1, Standard PERFORM .. VARYING:

The standard PERFORM .. VARYING is used to pass program control to a program module module-name multiple times, until condition is satisfied, while also incrementing varying-variable with each call to module-name. Condition evaluation, and the execution of module-name, are handled in the same way as PERFORM .. UNTIL; see Variant 2 in the PERFORM command description above for details.

In a PERFORM .. VARYING, the varying-variable is initialized on the first loop pass, or incremented for every pass other than the first, then condition is evaluated, then module-name is performed, in that order. This happens as follows:

· On the first pass through the PERFORM .. VARYING statement, the varying-variable is first initialized to from-amount; then, if condition evaluates to FALSE, the code in module-name is executed, and control returns to the beginning of the PERFORM .. VARYING. If condition evaluates to TRUE on the first pass, module-name is not performed.

· From the second pass through the PERFORM .. VARYING and all subsequent passes, varying-variable is first incremented by increment-amount; if condition evaluates to FALSE, module-name is performed, and control returns to the beginning of the PERFORM .. VARYING. If condition evaluates to TRUE, control passes to the statement following the PERFORM .. VARYING.

Increment-amount can be any nonzero number or numeric variable; to decrement the varying-variable rather than increment it, use a negative value for increment-amount.

More information on conditions, condition evaluation, and permitted condition syntax is available in the Command Reference entry for IF, and in the Expressions and Conditions section in Chapter 3, CobolScript Language Constructs.

Variant 2, Inline PERFORM VARYING:

The Inline PERFORM VARYING is a variation of PERFORM .. VARYING. Instead of performing a separate module, however, it executes the code that is between the PERFORM and END-PERFORM statements multiple times, until condition is satisfied.

Example Usage:

Variant 1:

PERFORM PROCESSING

VARYING varying_nbr

FROM 5 BY –1

UNTIL varying_nbr = 0.

Variant 2:

PERFORM VARYING varying_nbr

FROM 10 BY 2 UNTIL SQRT(varying_nbr)>=4

DISPLAY `varying_nbr = ` & varying_nbr

END-PERFORM


Command:

PERFORM .. VARYING

See Also:

IF (for explanation of condition evaluation), PERFORM.

Sample Program:

PERFORM.CBL