YouTip LogoYouTip

Vbscript Looping

--> * * * ## Loop Statements Loop statements are used to run the same block of code a specified number of times. In VBScript, we can use four loop statements: * **For...Next statement** - runs a block of code a specified number of times * **For Each...Next statement** - runs a block of code for each item in a collection or each element in an array * **Do...Loop statement** - runs a loop while a condition is true or until a condition becomes true * **While...Wend statement** - do not use this statement - please use the Do...Loop statement instead * * * ## For...Next Loop Use the **For...Next** statement to run a block of code a specified number of times. The **For** statement specifies the counter variable (**i**) and its initial and end values. The **Next** statement increments the variable (**i**) by a step value of 1. ## Example For i = 0 To 5 document.write("The number is " & i & "
") Next [Try it Β»](#) ### Step Keyword With the **Step** keyword, you can specify the increment value for the counter variable. In the following example, the counter variable (**i**) is incremented by a step value of 2 each time through the loop. For i=2 To 10 Step 2 some code Next To decrement the counter variable, you must use a negative **Step** value. And you must specify an end value that is less than the start value. In the following example, the counter variable (**i**) is decremented by a step value of 2 each time through the loop. For i=10 To 2 Step -2 some code Next ### Exit For...Next You can exit a For...Next statement with the Exit For keyword. For i=1 To 10 If i=5 Then Exit For some code Next * * * ## For Each...Next Loop The **For Each...Next** statement repeats a block of code for each item in a collection or each element in an array. ## Example Dim cars(2) cars(0)="Volvo" cars(1)="Saab" cars(2)="BMW" For Each x In cars document.write(x & "
") Next [Try it Β»](#) * * * ## Do...Loop If you don't know how many repetitions are needed, you can use a Do...Loop statement. The Do...Loop statement repeats a block of code until a condition is true or until a condition becomes true. ### Repeat Code Until Condition Is True You can use the While keyword to check the condition in a Do... Loop statement. Do While i>10 some code Loop If **i** equals 9, the code inside the above loop will stop executing. Do some code Loop While i>10 The code inside this loop will be executed at least once, even if **i** is less than 10. ### Repeat Code Until Condition Becomes True You can use the Until keyword to check the condition in a Do...Loop statement. Do Until i=10 some code Loop If **i** equals 10, the code inside the above loop will stop executing. Do some code Loop Until i=10 The code inside this loop will be executed at least once, even if **i** equals 10. ### Exit Do...Loop You can exit a Do...Loop statement with the Exit Do keyword. Do Until i=10 i=i-1 If i<10 Then Exit Do Loop The code inside this loop will be executed as long as **i** is not 10 and **i** is greater than 10. * * * ![Image 1: Examples](#) ## More Examples (IE only) (#) How to loop through the six headers in HTML. [Do...While loop](#) How to make a simple **Do...While** loop.
← Vbscript SummaryVbscript Conditionals β†’