VFP Tutorial - Do While ... EndDo loop

The Do While ... EndDo loop in FoxPro executes a section of code several times whilst an expression remains true. The loop may execute zero, once or many times. 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 be executed.

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 are no alternative forms with the expression being tested at the end of the loop or with the logic inverted so that the loop runs until the expression evaluates as true.

Back to FoxPro program control.


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

MS Access technical tips

Visual FoxPro technical tips

General Tips

 

More about program control in VFP

Visual FoxPro Tutorial - The Goto statement

The truth about the Goto statement in Visual FoxPro. It doesn't do what you think it does.

Read More

Visual FoxPro Tutorial - If ... Else ... EndIf

Using If and EndIf to control program flow in Visual FoxPro

Read More

Visual FoxPro Tutorial - Navigation

Navigating between records in Visual FoxPro

Read More

Visual FoxPro Tutorial - Scan ... EndScan loop

Using a scan loop to control program flow in Visual FoxPro.

Read More

Visual FoxPro Tutorial - Variables

Using variables in Visual FoxPro.

Read More