当前位置: 首页 > 网络学院 > 客户端脚本教程 > VBScript > VBScript 循环声明
For...next loop
This example demonstrates how to make a simple For....Next loop.
怎样使用For...Next来执行简单的循环
Looping through headers
This example demonstrates how you can loop through the 6 headers in html.
怎样在html中将6种规格的标题循环显示一遍
For...each loop
This example demonstrates how to make a simple For.....Each loop.
怎样使用For...Each来执行简单的循环
Do...While loop
This example demonstrates how to make a simple Do...While loop.
使用Do...While来执行简单的循环
Very often when you write code, you want to 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:
在VBScript中有四种循环声明:
You can use a For...Next statement to run a block of code, when you know how many repetitions you want.
当你确定自己要重复运行多少次代码的时候可以使用For...Next声明。
You can use a counter variable that increases or decreases with each repetition of the loop, like this:
你可以通过每循环一次便递增或递减某一变量的数值来达到这种效果,像这样:
For i=1 to 10 |
The For statement specifies the counter variable (i) and its start and end values. The Next statement increases the counter variable (i) by one.
For声明已经指定变量i为计数用的变量并设置了它的初始值和最终循环结束时的值。Next声明可以让计数变量i递增一次
Using the Step keyword, you can increase or decrease the counter variable by the value you specify.
当使用到Step关键字你就可以指定计数变量每次改变是递增还是递减,并指定它每次增减的大小。
In the example below, the counter variable (i) is increased by two each time the loop repeats.
下面的实例中,计数变量i是以每次循环增加2的速度进行递增的。
For i=2 To 10 Step 2 |
To decrease the counter variable, you must use a negative Step value. You must specify an end value that is less than the start value.
要使计数变量产生递减的效果就得让Step值变为负数。但同时你得保证指定的初始值得比最后的值要大。
In the example below, the counter variable (i) is decreased by two each time the loop repeats.
下面的举例中计数变量i每次循环后就会递减2。
For i=10 To 2 Step -2 |
You can exit a For...Next statement with the Exit For keyword.
你可以使用关键字Exit For来离开For....Next声明。
A For Each...Next loop repeats a block of code for each item in a collection, or for each element of an array.
当要对一个集合中的每个内容或是数组中的每个元素进行循环的话,可以使用For Each...Next循环
dim cars(2) |
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 is repeated while a condition is true or until a condition becomes true.
当你不知道需要重复执行多少次代码的时候你可以使用Do....Loop声明。当条件为真或是直到为真之前代码会一直反复执行。
You use the While keyword to check a condition in a Do...Loop statement.
在Do...Loop声明中使用While关键字来检查条件
Do While i>10 |
If i equals 9, the code inside the loop above will never be executed.
当I等于9的时候上面循环中的代码就不会被执行
Do |
The code inside this loop will be executed at least one time, even if i is less than 10.
上面的代码至少为执行一次,哪怕i小于10
You use the Until keyword to check a condition in a Do...Loop statement.
在Do...Loop声明中使用Until关键来检查条件
Do Until i=10 |
If i equals 10, the code inside the loop will never be executed.
如果i等于10那么循环中的代码不会被执行
Do |
The code inside this loop will be executed at least one time, even if i is equal to 10.
上面这段代码循环里的代码至少会执行依次,哪怕i等于10
You can exit a Do...Loop statement with the Exit Do keyword.
使用Exit Do关键字就可以离开Do...Loop声明
Do Until i=10 |
The code inside this loop will be executed as long as i is different from 10, and as long as i is greater than 10.
上面的这串代码仅当i大于10的时候才会执行循环。