Show/Hide Toolbars

XSharp

Purpose

Execute a loop while a condition is TRUE.

Syntax

[DO] WHILE <lCondition>

 <Statements>...

[EXIT]

 <Statements>...

[LOOP]

 <Statements>...

END[DO]

Arguments

<lCondition>The logical control expression for the DO WHILE 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

When the condition evaluates to TRUE, control passes into the structure and proceeds until an EXIT, LOOP, or ENDDO is encountered.  ENDDO returns control to the DO WHILE statement and the process repeats itself.  If the condition evaluates to FALSE, the DO WHILE construct terminates and control passes to the statement immediately following the ENDDO.

 

Use EXIT to terminate a DO WHILE structure based on a condition other than the DO WHILE condition.  LOOP, by contrast, prevents execution of statements within a DO WHILE based on an intermediate condition, and returns to the most recent DO WHILE 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, EXIT, LOOP