Go to: Articles List

Looping Logic Part 2 : Do...Loop Statements
Part 1 | Part 2 | Part 3 | Part 4

     The Do loops are a little different from the For loop. The For loop did a specific action a certain number of times. The Do loop continues to do a specific action until the supplied statement is found to be true.

     The snytax for the Do loop follows:

Do {While Condition | Until Condition}
'do this...
Loop

'Or you can organize the statements like this:
Do
'do this...
Loop {While Condition | Until Condition}

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

     The difference between the two is where the condition is located. The first one allows you to test the condition first and then proceed. The second Do loop allows you to proceed through the code once and then check the conditional statement to see if continue processing is in order. The first type of Do loop is used most often for the simple reason that usually you do not want to go in to an action at all unless a basic cindition is met.

     Below is an example of a Do Loop:

i = 0
Do While i < 5
     Response.Write("i=" & i & "<br>")
     i = i + 1
Loop

Result:
i=0
i=1
i=2
i=3
i=4

     The Do loop repeats a specific condition until the Condition is met with true. The above example printed out the value of i until i was no longer less than 5.