Latest web development tutorials

VBScript loop

Loops

Run the same number of code blocks for the specified loop. Looping statements are used to run the same block of code a specified number of times.

In VBScript, we can use four loop:

  • For ... Next statement - thefrequency and run for a period specified by the code
  • For Each ... Next statement - for each item in the collection or array each element of a piece of code to run
  • Do ... Loop statement - theoperating cycle, when the condition is true or until a condition is true
  • While ... Wend statement - Do not use this phrase - Use Do ... Loop statement instead it

For ... Next loop

Please use theFor ... Next statements to run a block of code a specified number of times.

For statement specifies the counter variable (i)as well as its initial and end values.Next statement in step 1 as the value increments the variable (i).

Examples

<html>
<body>

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

</body>
</html>

try it"

Step Keywords

Step by keyword, you can define the counter variable increment or decrement step value.

In the following example, the counter variable(i) is incremented for each cycle step value 2.

For i=2 To 10 Step 2
some code
Next

If you want to count down variable, you must use a negativeStep value.And it must be less than the predetermined start value end value.

In the following example, the counter variable(i) is decremented each cycle of the step value 2.

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

Quit For ... Next

You can exit by Exit For keyword For ... Next statement.

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 array each element of a piece of code to run repeatedly.

Examples

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


Do ... Loop

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

Do ... Loop statement to repeat a section of code until a condition is true or condition becomes true.

Repeat code execution until the condition is true

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

Do While i>10
some code
Loop

Ifi is equal to 9, the code inside the loop above will be terminated.

Do
some code
Loop While i>10

The code within the loop will be executed at least once, even ifi is less than 10.

Repeat code execution until the condition becomes true

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

Do Until i=10
some code
Loop

Ifi is equal to 10, above the code within the loop will be terminated.

Do
some code
Loop Until i=10

The code within the loop will be executed at least once, even ifi is equal to10.

Exit Do ... Loop

You can exit by Exit Do keyword Do ... Loop statement.

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

This code is inside the loop, it will be executed as long asi do not i isgreater than 10 and 10.


Examples s

Further examples (only for IE)

Loop through title
Html How to loop through the six headings.

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