VBScript Loops


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- Run a block of code a specified number of times
  • For Each...Next Statement- Run a block of code for each item in a collection or each element in an array
  • Do...Loop Statement- Run a loop while the condition is true or until the condition is true
  • While...Wend Statement- Do not use this statement - use the Do...Loop statement instead

For...Next Loop

Use theFor...Nextstatement to run a block of code a specified number of times.

ForThe statement specifies the counter variable (i) and its starting and ending values.NextThe statement increments the variable by 1 as the step value (i)。

Example

<html>
<body>

<script type="text/vbscript">
For i = 0 To 5
document.write("The number is " & i & "<br />")
Next
</script>

</body>
</html>

Try it Yourself »

Step Keyword

With the Stepkeyword, you can specify the step value by which the counter variable increases or decreases.

In the example below, the counter variable (i) is incremented by 2 each loop iteration.

For i=2 To 10 Step 2
some code
Next

If you want to decrement the counter variable, you must use a negativeStepvalue. And you must specify an ending value less than the starting value.

In the example below, the counter variable (i) is decremented by 2 each loop iteration.

For i=10 To 2 Step -2
some code
Next

Exit For...Next

You can exit the For...Next statement using the Exit For keyword.

For i=1 To 10
If i=5 Then Exit For
some code
Next

For Each...Next Loop

For Each...NextRepeatedly run a block of code for each item in a collection or each element in an array.

Example

<html>
<body>

<script type="text/vbscript">
Dim cars(2)
cars(0)="Volvo"
cars(1)="Saab"
cars(2)="BMW"

For Each x In cars
document.write(x & "<br />")
Next
</script>

</body>
</html>

Try it Yourself »


Do...Loop

If you do not know how many times to repeat, you can use the Do...Loop statement.

The Do...Loop statement repeatedly executes a block of code while the condition is true or until the condition becomes true.

Repeat code execution until the condition is true

You can use the While keyword to check the condition of the Do...Loop statement.

Do While i>10
some code
Loop

If iequals 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 iis less than 10.

Repeat code execution until the condition becomes true

You can use the Until keyword to check the condition of the Do...Loop statement.

Do Until i=10
some code
Loop

If iequals 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 iequals 10.

Exit Do...Loop

You can exit the Do...Loop statement using 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 asiis not 10 andiis greater than 10.


实例s

More Examples (only for IE)

Loop through headings
How to loop through the six headings in HTML.

Do...While loop
How to make a simpleDo...Whileloop.


Other Extensions