Go to: Articles List

Looping Logic Part 3 : While...WEnd Statements
Part 1 | Part 2 | Part 3 | Part 4

     The While...WEnd loop is very similar to the Do loop. This loop performs the requested action until a condition is met. The only difference is a small amount of syntax.

     The snytax for the While loop follows:

While {Condition}
'Perform this...
WEnd

NOTE: Leave off the { } brackets in the real code.

The While loop is commonly used to loop through a recordset that has been returned from a database. The following code demonstrates this:

While NOT RS.EOF
Response.Write("Name = " & RS("lname") & ", " & RS("lname") & "<br>")
RS.MoveNext
WEnd

In the above code we construct the While statement to loop through the RS object, pulling out the "lname" and "fname" elements from each record. When the condition of the RS reaching the end of the file (RS.EOF, after the last record), the statement exits.