Do... Loop Statement

Top  Previous  Next

 

Function:

Repeats a block of statements while a condition is True or until a condition becomes True.

Syntax:

 

do [ while | until ] expression

       statement1

       statement2

       

       [exit do]

       ...

       statementN

loop

 

or:

 

do

       statement1

       statement2

       

       [exit do]

       ...

       statementN

loop [ while | until ] expression

Scope:

Local and HTML

See Also:

For... Next Statement, While-Wend Statement, Exit Statement

 


Part

Description

expression

A logical expression which is evaluated either before or after the first time the statements are executed.

statement[1, 2...]

Lines of code to be executed.

Details

The do-loop statement repeats a block of code. If the condition (while or until) is included at the end of the loop (after the loop keyword), the block of code is executed at least once; If the condition is included at the beginning of the loop (after the do keyword), the condition must evaluate to true for the code to execute even once.

Any number of exit do statements may be placed anywhere in the do... loop as an alternate way to exit the loop. These can be used as an alternate way to exit the loop, such as after evaluating a condition mid-loop using an if... then statement. Exit do statements used within nested do-loop statements will transfer control to the loop which is one nested level above the loop in which the exit do occurs.

Examples

 

dim i as integer

 

' example of the first syntax:

i = 0

do        

       i = i + 1

loop until i = 10

 

' example of the second syntax:

i = 0

do until i = 10        

       i = i + 1

loop