Visual Basic and Visual FoxPro loops

VBA

Do While, Do Until, Exit Do, Loop
While, Wend

VFP

Do While, Loop, Exit, EndDo

Visual Basic Syntax Notes

Visual Basic has four related types of loop with similar syntax. These run While or Until the test condition is true and allow the test condition to be at the beginning or the end of the loop. A couple of examples:

x = 1
Do While x < 100
  x = x * 2
  ? x
Loop

or

x = 1
Do
  x = x * 2
  ? x
Loop Until x >= 100

Use the Loop keyword to skip the rest of the code in the body of the loop and return immediately to the top. Use the Exit Do commands to break out of the loop and execute the statement following the end of the structure.

Visual Basic also has the separate While...Wend structure which has been retained for backwards compatibility with GW-BASIC. This structure has the test at the start of the loop.

Visual FoxPro Syntax Notes

FoxPro just has the loop with While at the beginning so it is not possible to initialise the test condition inside the body of the loop. The test variable must exist and have a value before the loop is entered:

x = 1
Do While x < 100
  x = x * 2
  ? x
EndDo

Use the Loop keyword to skip the rest of the code in the loop and return immediately to the test in the While statement. Use the Exit command to break out of the loop and execute the statement following EndDo.

FoxPro does have another specialist type of loop command. The scan command is specifically designed for processing data in tables.

Case  |  Language index  |  For