The Do...Loop Statement

A Do...Loop creates an iteration much like a While...End While loop. It offers the additional option of executing the statements in the loop at least one time.

Do [While|Until] condition

   ...statements

Loop

or

Do

   ...statements

Loop [While|Until] condition

In its Do While...Loop configuration the loop executes as long as a condition test at the beginning of the loop remains True. The condition may be False at the start of the loop, in which case the loop is exited before it begins. Somewhere inside the loop the condition becomes True for exiting the loop. As in the case with the While...End While loop, the condition test is for continuing the loop.

In its Do Until...Loop configuration the condition test is for ending the loop. Otherwise, the logic is the same as for the Do While...Loop.

If there is a situation in which the statements in the loop will be executed at least one time, then the Do...While Loop or Do...Until Loop configuration can be used. The condition test is not made until the end of the loop, after the statements have been executed the first time.

The Do...Loop presents special condition-test options that don't normally appear in routine data processing. For the most part, For...Next and While...End While statements can handle the bulk of processing. As an illustration, though, of the Do...Until Loop option, the following script loads the StatesArray and CodesArray arrays that are used in a previous example of the While...End While loop. It reads records from an external text file to populate the arrays, then writes the information to this page.

<SCRIPT runat="server">

Dim StatesArray(0) As String
Dim CodesArray(0) As String

Sub Page_Load

  Dim FileReader As StreamReader
  Dim LineIn As String
  Dim FieldArray() As String

  FileReader = File.OpenText("e:\WebSite\tutorials\vbnet\vbnet04\StateCodes.txt")
  LineIn = FileReader.ReadLine()	
  Do
    FieldArray = Split(LineIn, ",")
    StatesArray(UBound(StatesArray)) = FieldArray(0)
    CodesArray(UBound(CodesArray)) = FieldArray(1)
    LineIn = FileReader.ReadLine()
    If LineIn <> ""
      ReDim Preserve StatesArray(StatesArray.Length)
      ReDim Preserve CodesArray(CodesArray.Length)
    End If
  Loop Until LineIn = ""
  FileReader.Close()

  Dim i As Integer
  For i = 0 To UBound(StatesArray)
    CodesOut.Text &= StatesArray(i) & ", " & CodesArray(i) & "<br/>"
  Next

End Sub

</SCRIPT>

<div style="border:solid 1px; padding:10px; width:150px">
<asp:Label id="CodesOut" EnableViewState="False" runat="server"/>
</div>
Alabama, AL
Alaska, AK
Arizona, AZ
Arkansas, AR
California, CA
Colorado, CO
Connecticut, CN
Deleware, DE
Florida, FL
Georgia, GA
Hawaii, HI
Idaho, ID
Illinois, IL
Indiana, IN
Iowa, IA
Kansas, KS
Kentucky, KY
Louisiana, LA
Maine, ME
Maryland, MD
Massachusetts, MA
Michigan, MI
Minnesota, MN
Mississippi, MS
Missouri, MO
Montana, MT
Nebraska, NE
Nevada, NV
New Hampshire, NH
New Jersey, NJ
New Mexico, NM
New York, NY
North Carolina, NC
North Dakota, ND
Ohio, OH
Oklahoma, OK
Oregon, OR
Pennsylvania, PA
Rhode Island, RI
South Carolina, SC
South Dakota, SD
Tennessee, TN
Texas, TX
Utah, UT
Vermont, VT
Virginia, VA
Washington, WA
West Virginia, WV
Wisconsin, WI
Wyoming, WY

This version of the script makes the assumption that there is at least one record in the external file from which the arrays are built. This is a proper assumption in this particular example, but should not be the premise for most external data stores. A better solution is to use the Do While...Loop construct, making the condition test prior to any processing. Yet, the While...End While serves this exact purpose.

As a general rule, use the While...End While statement for loops for which an explicit number of iterations is not known. Use the Do...Loop Until version if you are a balding 60-year-old renegade programmer with a pony tail.