Do While condition
Script block here
Loop
As long as the condition is satisfied ( or True ) the script block will be executed. Always at the starting the Condition is checked. Here is a simple script using Do While loop.
Dim my_num
my_num=1
Do While my_num <=10
Response.Write my_num & "<br>"
my_num = my_num +1
Loop
The above code will print 1 to 10 and it will fail to print 11 as the condition checking will return False. We can modify the Do While loop and keep the condition checking at the end of the Loop.
Do
Script block here
Loop While Condition
Now let us change our old example and try this way.
Dim my_num
my_num=11
Do
Response.Write my_num & "<br>"
my_num = my_num +1
Loop While my_num <=10
Here we can see there is a output of 11 as the inside the loop code is executed once even before the condition is checked and found False. So this way at least once the code inside the Loop will be executed. Test the above code by adding the While condition at the starting of the loop.
Dim my_num
my_num=1
Do Until my_num > 10
Response.Write my_num & "<br>"
my_num = my_num +1
Loop
VBScript also supports the older While ... Wend form, but Do While ... Loop is more flexible and is used throughout these tutorials.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.