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
EndDo

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.

Notes

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

More tips from Alvechurch Data

More tips from Alvechurch Data

FoxPro 101 - For ... Next loop

Visual FoxPro Tutorial - Using a For ... Next loop.

Read More

FoxPro 101 - Scan ... EndScan loop

Visual FoxPro Tutorial - Using a scan loop to control program flow.

Read More

FoxPro 101 - Program control

Visual FoxPro Tutorial - program control structures.

Read More