Previous Topic: DEFINE Control StatementNext Topic: ELSE Control Statement


DO Control Statement

The DO control statement repeats a given set of statements a specified number of times, until a certain condition is met or while a certain condition is met. The set of statements following the DO control statement and preceding the corresponding END control statement constitute a DO group. This DO group continues to execute as long as all necessary conditions are met. As a point of reference, the function and evaluation of the DO control statement itself is referred to as DO operation.

Syntax

DO	field = expression-1 TO expression-2 BY expression-3
	FOREVER UNTIL(condition) WHILE(condition)

where:

field

Specifies the name of the defined field (DEFINE control statement) that is set and incremented during the DO operation.

expression-1

Specifies an expression that defines the initial value for the DO operation.

expression-2

Specifies an expression that defines the final value or limit for the DO operation.

When the final value is exceeded, execution of the DO group is ended.

expression-3

Specifies an expression that defines the incrementing value for the DO operation.

If BY increment-expression is not specified, a value of +1 is used for an ascending range (expression-1 is less than or equal to expression-2), and a value of -1 is used for a descending range (expression-1 is greater than expression-2).

FOREVER

Specifies continual execution of the DO group.

The FOREVER keyword is mutually exclusive with the field = expression-1 TO expression-2 BY expression-3 specification.

UNTIL (condition)

Specifies that the execution of the DO group is to continue until the given condition is true.

WHILE (condition)

Specifies that the execution of the DO group is to continue while the specified condition is true.

All operands for the DO control statement are optional. If no operands are specified, or only field = expression-1 is specified, the DO group is executed one time only. The DO group continues to execute until one of the following conditions is met:

The execution of the DO group can also be interrupted by a BREAK control statement, by a STOP control statement, or by an end-of-data condition retrieving database records or sort records.

DO control statements can be nested within IF control statements or other DO control statements to any level. Avoid an infinite loop when coding the UNTIL or WHILE conditions.

For sample DO control statement, see RELEASE control statement.