DO WHILE ... ENDDO Command

Executes a set of commands within a conditional loop.

DO WHILE lExpression 
   Commands 
   [LOOP]
   [EXIT]
ENDDO

Parameters

  • lExpression
    Specifies a logical expression whose value determines whether the commands between DO WHILE and ENDDO are executed. As long as lExpression evaluates to true (.T.), the set of commands are executed.
  • Commands
    Specifies the set of Visual FoxPro commands to be executed as long as lExpression evaluates to true (.T.).
  • LOOP
    Returns program control directly back to DO WHILE. LOOP can be placed anywhere between DO WHILE and ENDDO.
  • EXIT
    Transfers program control from within the DO WHILE loop to the first command following ENDDO. EXIT can be placed anywhere between DO WHILE and ENDDO.

Remarks

Commands between DO WHILE and ENDDO are executed for as long as the logical expression lExpression remains true (.T.). Each DO WHILE statement must have a corresponding ENDDO statement.

Comments can be placed after DO WHILE and ENDDO on the same line. The comments are ignored during program compilation and execution.

Example

In the following example, the number of products in stock priced over $20 is totaled in the DO WHILE loop until the end of the file (EOF) is encountered. The DO WHILE loop is exited and the total is displayed.

CLOSE DATABASES
OPEN DATABASE (HOME(2) + 'Data\testdata')
USE products  && Opens Products table
SET TALK OFF
gnStockTot = 0

DO WHILE .T.  && Begins loop
   IF EOF( )
      EXIT
   ENDIF
   IF unit_price < 20
      SKIP
      LOOP
   ENDIF
   gnStockTot = gnStockTot + in_stock
   SKIP
ENDDO     && Ends loop

CLEAR
? 'Total items in stock valued over 20 dollars:'
?? gnStockTot

See Also

DO CASE ... ENDCASE | FOR EACH ... ENDFOR | FOR ... ENDFOR | IF ... ENDIF | IIF( ) | SCAN ... ENDSCAN