VFP Tutorial - Do ... While loop

Executes a section of code several times (zero, one or many times) whilst an expression remains true. This example will keep keep adding to a total until that total reaches 100;

lnLimit = 100
lnTotal = 0
Do While lnTotal < lnLimit
   *-- Keep adding until we hit the limit
   lnTotal = lnTotal + lnNextItem
   Print lnTotal

If the expression lnTotal < lnLimit evaluates as true (.T. in FoxPro) then the statements in the loop will be executed and the expression will be evaluated again. The loop will continue until the expression evaluates as false (.F. in FoxPro).

If the expression evaluates as false when FoxPro first reaches it then the statements inside the body of the loop will never execute.

The code in this example will keep adding new items until the total exceeds the limit.


The test condition must evaluate as .T. or .F. because the logical values in FoxPro are a separate data type and an integer result will not be interpreted as a logical value.

This is the only form of the while loop in Foxpro. There is not an alternative form with the expression being tested at the end of the loop and there is not an inverted form which runs until the expression evaluates as true.

Back to FoxPro program control.

Introduction | Environment | Project | Tables | Forms | Navigation | Executable