Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

VBScript loop statement


May 13, 2021 VBScript


Table of contents


VBScript loop

The VBScript loop is used to repeat a set of statements, and this section describes four loop statements.

Loop statement

Loop statements are used to run the same block of code the 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 looping statements:

  • For... Next Statement - The number of times a piece of code has been run
  • For Each... Next statement - Run a piece of code for each item in the collection or for each element in the array
  • Do... Loop statement - Runs a loop when the condition is true or until the condition is true
  • While... W end statement - Don't use this statement - Use Do... Loop statement instead of it

For... Next loop

Please use For... T he number of times the Next statement runs a piece of code.

The For statement specifies the count variable (i) and its initial and end values. The Next statement increments the variable (i) with 1 as the step value.

<html>
<body>

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

</body>
</html>

Try it out . . .

Step keywords

With Step keywords, you can specify the step value of the count variable increment or decrease.

In the following example, the count variable (i) has an incremental step value of 2 per loop.

For i=2 To 10 Step 2
some code
Next

If you want to decrease the count 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 count variable (i) has a decreasing step value of 2 per loop.

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

Exit For... Next

You can opt out of For... Next statement.

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

For Each... Next loop

For Each... Next repeats a piece of code for each item in the collection or for each element in the array.

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


Do... Loop

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

Do... T he Loop statement repeats a piece of code until the condition is true or the condition becomes true.

Repeat the code until the condition is true

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

Do While i>10
some code
Loop

If i is equal to 9, the code in the loop above 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.

Repeat the code until the condition becomes true

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

Do Until i=10
some code
Loop

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

Do
some code
Loop Until i=10

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

Exit Do... Loop

You can opt out of Do... L oop statement.

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

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


VBScript loop statement

More instances (IE only)

Loop through the title
How to loop through the six titles in html.

Do... While loop
How to do a simple Do... W hile loop.

That's the whole story of the VBScript loop statement, and now you can familiarize yourself with the use of the statement through your code!