当前位置: 首页 > 网络学院 > 客户端脚本教程 > VBScript > VBScript 循环声明

VBScript
VBScript 介绍
如何使用 VBScript
VBScript 放置
VBScript 变量
VBScript 程序
VBScript 条件语句
VBScript 循环声明
VBScript 摘要
VBScript 实例
VBScript 函数
VBScript 关键字

VBScript 循环声明


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 593 ::
收藏到网摘: n/a

Examples
实例

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来执行简单的循环


Looping Statements
循环声明

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中有四种循环声明:

  • For...Next statement - runs statements a specified number of times.
    让声明运行指定次数
  • For Each...Next statement - runs statements for each item in a collection or each element of an array
    针对某数组内的每个元素反复运行声明
  • Do...Loop statement - loops while or until a condition is true
    在条件为真之前循环运行
  • While...Wend statement - Do not use it - use the Do...Loop statement instead
    别用这个声明,它完全可以用Do...Loop声明来取代

For...Next 循环

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
some code
Next

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递增一次

关键字 Step

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
some code
Next

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
some code
Next

离开 For...Next

You can exit a For...Next statement with the Exit For keyword.
你可以使用关键字Exit For来离开For....Next声明。


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.
当要对一个集合中的每个内容或是数组中的每个元素进行循环的话,可以使用For Each...Next循环

dim cars(2)
cars(0)="Volvo"

cars(1)="Saab"
cars(2)="BMW"

For Each x in cars
document.write(x & "<br />")
Next


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 is repeated while a condition is true or until a condition becomes true.
当你不知道需要重复执行多少次代码的时候你可以使用Do....Loop声明。当条件为真或是直到为真之前代码会一直反复执行。

Repeating Code While a Condition is True
当条件为真时反复执行代码

You use the While keyword to check a condition in a Do...Loop statement.
在Do...Loop声明中使用While关键字来检查条件

Do While i>10
some code
Loop

If i equals 9, the code inside the loop above will never be executed.
当I等于9的时候上面循环中的代码就不会被执行

Do
some code
Loop While i>10

The code inside this loop will be executed at least one time, even if i is less than 10.
上面的代码至少为执行一次,哪怕i小于10

Repeating Code Until a Condition Becomes True
代码一直重复,直到条件为真

You use the Until keyword to check a condition in a Do...Loop statement.
在Do...Loop声明中使用Until关键来检查条件

Do Until i=10
some code
Loop

If i equals 10, the code inside the loop will never be executed.
如果i等于10那么循环中的代码不会被执行

Do
some code
Loop Until i=10

The code inside this loop will be executed at least one time, even if i is equal to 10.
上面这段代码循环里的代码至少会执行依次,哪怕i等于10

离开 Do...Loop

You can exit a Do...Loop statement with the Exit Do keyword.
使用Exit Do关键字就可以离开Do...Loop声明

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

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的时候才会执行循环。

评论 (0) All

登陆 | 还没注册?