View contents of this chapter

The Do Loop

VB provides a very flexible general purpose loop known as a Do loop.

Do While

Do While condition
  Statements
Loop

Using the While keyword and an expression tells VB to execute the loop while the expression is True.

Do Until

Do Until condition
  Statements
Loop

The Until keyword instructs the loop to execute until a condition is True.

Loop While

Do
  Statements
Loop While Condition

To make sure that the loop executes at least once, place the exit condition at the Loop statement, rather than at the Do statement.

Loop Until

Do
  Statements
Loop Until Condition

The Until keyword instructs the loop to execute until a condition is True. This loop executes at least once.

Consider the following code segment:

Do While counter < 10
  lblOut.Caption = counter
  counter = counter + 1
Loop

Play Sound You must change the loop testing condition inside the loop's body; otherwise, the loop never ends.

Loop Description
Do While condition Loop

Enters loop if condition is True

Do Until condition Loop

Enters loop if condition is False

Do Loop While condition Always executes loop once. Loops if condition is True.
Do Loop Until condition Always executes loop once. Loops if condition is False.

 



© Universal Teacher Publications        INDEX Previous Screen Next Screen