loop Statement

The loop statement repetitively executes (loops) until an ending condition is met. The first set of statements, if present, is executed at least once.

Syntax

loop {statement.block1} [until|while] logical.exp {do} {statement.block2} repeat

loop

{statement.block1}

[until|while] logical.exp {do}

{statement.block2}

repeat

loop {statement.block} repeat

Description

The loop...until and loop...while forms are repetitive loop functions used to repeat a sequence of statements conditionally. The do word is optional.

If the until clause is used, looping continues as long as the expression following it evaluates to false. If the while clause is used, looping continues as long as the expression following it evaluates to true.

The statements in the do clause are executed as long as the loop is executed. If while or until is specified, the do is required.

The repeat statement defines the end of the loop.

CAUTION

Loops with no breaks in processing (such as looping waiting for input through system(14)), will place the system into a constant CPU consumption state until the loop is exited, leading to poor system performance.

If neither a while nor until clause is used, the loop never finishes. The use of goto or exit is required to exit the loop in this construct.

If the until expression is initially true or the while expression is initially false, the statements specified in statement.block1 are executed at least once and those specified in statement.block2 are not executed.

Example(s)

This loop terminates when the calculated value of x is under 100. Each time through the loop, c is incremented by one.

c = 1

loop

x = c*2

while x < 100 do

c = c + 1

repeat

This processes an active list. When the last item-ID is read, the exit clause passes control to the next executable statement after the repeat statement. Notice the select list is the default active select list.

execute "select customers"

loop

readnext item-ID else exit

print item-ID

repeat

See Also

Active List, Boolean Evaluation, Branching, continue Statement, do Clause, exit Statement, for...next Statement, readnext Statement, Relational Operators, repeat Statement, Statements and Functions, until Clause, while Clause