DO

Purpose

Begins a sequence of statements to be executed as a group, possibly to be executed iteratively.

Syntax

DO {while-until-spec | [index = iteration-spec[,iteration-spec]…]
       |[index = start {UPTHRU|DOWNTHRU} finish][1] [repeat-spec][while-until-spec]
       | FOREVER | LOOP};

Parameters

while-until-spec
{WHILE | UNTIL}(test-expression) 
iteration-spec
start[to-by-spec|repeat-spec][while-until-spec] 
to-by-spec
{TO finish[BY increment] | BY increment[TO finish]}
repeat-spec
REPEAT next
test-expression
Any expression that yields a scalar bit string value of length >= 1. If any bit of the value is 1, the test-expression is true; otherwise, it is false. test-expression must be enclosed in parentheses.
index
A reference to a scalar variable.
start, next, finish, and increment    
Expressions valid for assignment to index.

Description

The DO statement begins a sequence of statements to be executed in a group. The group ends with the non-executable statement END.

A DO-group is executed a variable number of times under the control of its DO statement.

A DO statement cannot be used as an ON-unit, but can appear anywhere within a procedure or BEGIN block, including a THEN or ELSE clause of an IF statement or as an object of a WHEN or OTHERWISE clause.

DO FOREVER | DO LOOP
DO FOREVER;
...
END;

specifies an infinite loop. The GOTO or LEAVE statement, or terminating the program, is the only means of exiting the loop.

DO LOOP;
...
END;

is the equivalent.

Example

DCL I FIXED BIN (15);

ON CONVERSION BEGIN;
     PUT SKIP LIST ('Conversion triggered');
     STOP;
END;

DO FOREVER;
    GET LIST (I) ;
    PUT LIST (I) ;
    PUT SKIP;
    IF I = 0 THEN
        LEAVE;
END;

Types of DO Statements

The following topics explain each DO statement. In all of these discussions, you can assume that the DO-group neither transfers control out of the group nor skips statements within the group when the statements of the group are executed once, twice, n times, and so on. However, any DO-group can contain IF statements, other DO-groups, RETURN, or GOTO statements that alter the order of execution.

Control cannot be transferred initially into a non-simple DO-group except by executing that DO-group's DO statement. Also, if control is transferred out of a non-simple DO-group (other than by a procedure or function call), control cannot generally be transferred back into the group. Violation of these rules causes unpredictable results.