Easy Tutorial
❮ Vbscript Variables Func Loadpicture ❯

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 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) along with its start and end values. The Next statement increments the variable (i) by 1.

Example

Step Keyword

With the Step keyword, you can specify the increment or decrement value of the counter variable.

In the following example, the counter variable (i) increments by 2 each loop.

To decrement the counter variable, you must use a negative Step value and specify an end value less than the start value.

In the following example, the counter variable (i) decrements by 2 each loop.

Exiting For...Next

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


For Each...Next Loop

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

Example


Do...Loop

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

The Do...Loop statement repeats a block of code until a condition is true or until a condition becomes true.

Repeating Code Until a Condition is True

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

If i equals 9, the code inside the loop will stop executing.

The code inside this loop will execute at least once, even if i is less than 10.

Repeating Code Until a Condition Becomes True

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

If i equals 10, the code inside the loop will stop executing.

The code inside this loop will execute at least once, even if i equals 10.

Exiting Do...Loop

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

The code inside this loop will execute as long as i is not 10 and i is greater than 10.


More Examples (IE Only)

Loop Through Headers

Do...While Loop Do...While Loop.

❮ Vbscript Variables Func Loadpicture ❯