VB loop



Loop statement

Loop statement is used to run the same block of code a specified number of times. Looping statements are used to run the same block of code a specified number of times.

In VBScript, we can use four kinds of looping statements:

  • For...Next Statement- Runs a block of code a specified number of times

  • For Each...Next Statement- For each item in the collection or Run a certain piece of code for each element in the array

  • Do...Loop statement- Run a loop when the condition is true or until the condition is true

  • While...Wend statement - Do not use this statement - use Do...Loop statement instead


For...Next Loop

Please use the For...Next statement to run a section of code a specified number of times. The

For statement specifies the count variable (i) and its initial and end values. The Next statement increments the variable by 1 (i).

Instance

<html>
<body>

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

</body>
</html>

Run instance»

Click the "Run instance" button to view the online instance

Step keyword

Through the Step keyword, you can specify the step value for the count variable to increase or decrease.

In the following example, the count variable (i) increments by 2 each time it loops.

For i=2 To 10 Step 2
Some code
Next

If you want to decrement a counting variable, you must use a negative Step value. And an end value smaller than the start value must be specified.

In the following example, the decrement step value of the count variable (i) is 2 for each loop.

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

Exit For...Next

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

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

For Each...Next loop

For Each...Next for each item in the collection Or run a certain piece of code repeatedly for each element in an array.

Instance

<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>

Run instance »

Click the "Run instance" button to view the online instance



Do...Loop

If you don’t know how many times to repeat, you can use the Do...Loop statement.

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

Repeatedly execute the code 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 i is equal to 9, the code within the above loop will terminate execution.

Do
some code
Loop While i>10

The code within this loop will be executed at least once, even if i is less than 10.

Repeatedly execute code until the condition becomes true

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

Do Until i=10
some code
Loop

If i is equal to 10, the code within the above loop will terminate execution.

Do
some code
Loop Until i=10

The code within this loop will be executed at least once, even if i is equal to 10.

Exit Do...Loop

You can exit the Do...Loop statement through the Exit Do keyword.

Do Until i=10
i=i-1
If i<10 Then Exit Do
Loop

The code in this loop will be executed as long as i is not 10 and i is greater than 10 be executed.

tryitimg.gif

More examples (for IE only)

Looping through headers
How to loop through six headers in html.

Do...While loop
How to make a simple Do...While loop.