Do...Loop Statement


Do...Loop Statement

Syntax

     Do [{While | Until} condition]        [statements]        [Exit Do]        [statements]        [Continue Do]        [statements]     Loop 

or:

     Do        [statements]        [Exit Do]        [statements]        [Continue Do]        [statements]     Loop [{While | Until} condition] 


condition (optional; Boolean)

An expression that is reevaluated each pass through the loop


statements (optional)

Program statements to execute while (or until) condition is true

Description

The Do...Loop statement repeatedly executes program code while (or until) a given condition remains TRue. When used with the While clause, the loop block is executed each time condition evaluates to true; when used with the Until clause, the loop block is executed until condition evaluates to true. Once the sustaining condition is no longer met, the entire loop is exited. The Exit Do statement can be used at any time to exit the Do loop early.

If the condition appears at the top of the loop construct, the code within the loop might execute zero or more times, depending on the evaluation result of condition. When the condition appears at the bottom of the loop construct, the code within the loop always executes at least once.

New in 2005. The Continue Do statement can be used at any time to immediately jump back to the top of the loop and attempt to process the next iteration. The condition is reevaluated immediately upon reaching the top of the loop (or the bottom, if the condition is located there).

Usage at a Glance

  • You can create an infinite loop by leaving out the While and Until clauses completely.

         Do        [statements]     Loop 

    Such loops should generally include Exit statements so that the loop can eventually be exited.

  • A condition of Nothing is treated as False.

  • You can nest Do...Loop statements within each other.

Version Differences

Visual Basic 2005 includes the Continue Do statement.

See Also

While...End While Statement




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net