Open Directory SiteVBScript Tutorials

ASP | XML | VBScript | JavaScript | ADO | CSS | XMLDOM | PHP | Operating Systems

Home >>  VBScript >> VBScript Looping Statements

VBScript Looping Statements

 

Looping Statements

Very often when you write code, you want allow the same block of code to run a number of times. You can use looping statements in your code to do this.

In VBScript we have four looping statements:

· Do...Loop statement - loops while or until a condition is true

· While...Wend statement - use Do...Loop instead

· For...Next statement - run statements a specified number of times.

· For Each...Next statement - run statements for each item in a collection or each element of an array

Do...Loop

You can use Do...Loop statements to run a block of code when you do not know how many repetitions you want. The block of code are repeated while a condition is true or until a condition becomes true.

Repeating Code While a Condition is True

You use the While keyword to check a condition in a Do...Loop statement.

Do While i > 10

some code

Loop

Notice that if i is for example 9, the code inside the loop will never be executed.

Do

some code

Loop While i > 10

Notice that in this example the code inside this loop will be executed at least one time, even if i is less than 10.

Repeating Code Until a Condition Becomes True

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

Do Until i = 10

some code

Loop

Notice that if i is equal to 10, the code inside the loop will never be executed.

Do

some code

Loop Until i = 10

Notice that in this example the code inside this loop will be executed at least one time, even if i is equal to 10.

Exiting a Do...Loop

You can exit a Do...Loop statement with the Exit Do keyword.

Do Until i = 10

i = i - 1

If i < 10 Then Exit Do

Loop

Notice that the code inside this loop will be executed as long i is different from 10, and as long as i is greater than 10.

For...Next

You can use For...Next statements to run a block of code when you know how many repetitions you want.

You can use a counter variable that increases or decreases with each repetition of the loop, like this:

For i = 1 to 10

some code

Next

The For statement specifies the counter variable i and its start and end values. The Next statement increases i by 1.

Using the Step keyword, you can increase or decrease the counter variable by the value you specify.

For i = 2 To 10 Step 2

some code

Next

In the example above, i is increased by 2 each time the loop repeats. When the loop is finished, total is the sum of 2, 4, 6, 8, and 10.

To decrease the counter variable, you use a negative Step value. You must specify an end value that is less than the start value.

For i = 10 To 2 Step -2

some code

Next

In the example above, i is decreased by 2 each time the loop repeats. When the loop is finished, total is the sum of 10, 8, 6, 4, and 2.

Exiting a For...Next

You can exit a For...Next statement with the Exit For keyword.

For Each...Next

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

The For Each...Next statement looks almost identical to the For...Next statement. The difference is that you do not have to specify the number of items you want to loop through.

dim names(3)

names(0) = "Tove"

names(1) = "Jani"

names(2) = "Hege"

For Each item in names

some code

Next

Examples

1. This example demonstrates how to make a simple For.....Next loop.

Coding

<html>

<body>

<script language="VBScript">

for i = 0 to 5

document.write("The number is " & i)

document.write("<br>")

next

</script>

</body>

</html>

Output

The number is 0

The number is 1

The number is 2

The number is 3

The number is 4

The number is 5

2. This example demonstrates how you can loop through the 6 headers in html.

Coding

<html>

<body>

<script language="VBScript">

for i = 1 to 6

document.write("<h" & i & ">This is header " & i & "</h" & i & ">")

next

</script>

</body>

</html>

Output

This is header 1

This is header 2

This is header 3

This is header 4

This is header 5

This is header 6

Cheap Web Hosting Articles - Web Site Design & Web Hosting Tutorials - Domain Hosting