for 循环

在经典 ASP 中,我们可以使用 for 关键字指定 for 循环。使用 for 语句,我们需要下一个语句来增加计数器。

For i = 0 To 10
    Response.Write("Index: " & i)
Next

关键字可以用来改变该怎么下一个声明将修改计数器。

For i = 10 To 1 Step -1
    'VBS Comment
Next

要退出 for 循环,请使用 Exit For 语句

For i = 0 To 10
    Response.Write("Index: " & i)
    If i=7 Then Exit For 'Exit loop after we write index 7
Next

我们还可以使用 For...Each 循环来执行集合中一系列已定义元素的循环。例如:

Dim farm, animal
farm = New Array("Dog", "Cat", "Horse", "Cow")
Response.Write("Old MacDonald had a Farm, ")
For Each animal In farm
    Response.Write("and on that farm he had a " & animal & ".<br />")
Next