Show/Hide Toolbars

XSharp

Purpose

Execute a loop until a condition is TRUE.

Syntax

REPEAT
 <Statements>...
[EXIT]
 <Statements>...
[LOOP]
 <Statements>...
UNTIL <lCondition>

Arguments

<lCondition>The logical control expression for the REPEAT UNTIL loop.

 

EXITUnconditionally branches control from within a FOR, FOREACH , REPEAT or DO WHILE statement to the statement immediately following the corresponding ENDDO or NEXT statement.

 

LOOPBranches control to the most recently executed FOR, FOREACH , REPEAT or DO WHILE statement.

Description

As long as the condition evaluates to FALSE then the loop will continuet. When the condition evaluates to false, the REPEAT construct terminates and control passes to the statement immediately following the UNTIL.

 

Use EXIT to terminate a REPEAT UNTIL structure based on a condition other than the REPEAT UNTIL condition.  LOOP, by contrast, prevents execution of statements within a REPEAT UNTIL based on an intermediate condition, and returns to the most recent REPEAT UNTIL statement.

 

Control structures can be nested to any depth.  The only requirement is that each control structure be properly nested.

Examples

This example demonstrates a typical control structure for a simple grouped report:

 

LOCAL cOldSalesman, nTotalAmount
USE sales INDEX salesman NEW
DO WHILE .NOT. EOF()
 cOldSalesman := Sales->Salesman
 nTotalAmount := 0
 DO WHILE cOldSalesman = Sales->Salesman ;
                 .AND. (.NOT. EOF())
         ? Sales->Salesman, Sales->Amount
         nTotalAmount := nTotalAmount + Sales->Amount
         SKIP
 ENDDO
 ? "Total:  ", nTotalAmount, "for", cOldSalesman
ENDDO
CLOSE sales
 

This code fragment demonstrates how LOOP can be used to provide an intermediate processing condition:

 

DO WHILE <lCondition>
 <Initial Processing>...
 IF <Intermediate Condition>
         LOOP
 ENDIF
 <Continued Processing>...
ENDDO

The next example demonstrates the use of DO WHILE to emulate a "repeat until looping" construct:

 

LOCAL lMore := TRUE
DO WHILE lMore
 <Statements>...
 lMore := <lCondition>
ENDDO

 

Here, a DO WHILE loop moves sequentially through a database file:

 

DO WHILE .NOT. EOF()
 <Statements>...
 SKIP
ENDDO

See Also

BEGIN SEQUENCE, DBEval(), DO CASE, FOR, IF, RETURN, DO WHILE, EXIT, LOOP