VB 循环
For Next、For Each、Do Loop、While,以及退出与嵌套。
For Next
Dim i, sum
sum = 0
For i = 1 To 10
sum = sum + i
Next
WScript.Echo "1..10 的和 = " & sum
For i = 10 To 1 Step -2
WScript.Write i & " "
Next
WScript.Echo ""1..10 的和 = 55
10 8 6 4 2Step may be negative (counting down) or fractional; it defaults to 1. For i = 1 To 10 runs ten times, inclusive at both ends — unlike C-style i < 10, so off-by-one mistakes are easy.
For Each
Dim langs, lang
langs = Array("VBScript", "JScript", "PowerShell")
For Each lang In langs
WScript.Echo lang
Next
' 遍历字典的值
Dim d
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Apple"
d.Add "b", "Banana"
Dim k
For Each k In d.Keys
WScript.Echo k & " = " & d(k)
NextVBScript
JScript
PowerShell
a = Apple
b = BananaFor Each only iterates and gives no index; use For i = 0 To UBound(arr) when you need one. Never add or remove items while enumerating — it upsets the enumerator.
Do Loop
' 先判断后执行
Dim n
n = 0
Do While n < 3
WScript.Echo "n = " & n
n = n + 1
Loop
' 先执行后判断
Dim m
m = 0
Do
WScript.Echo "m = " & m
m = m + 1
Loop Until m >= 3n = 0
n = 1
n = 2
m = 0
m = 1
m = 2Do While ... Loop tests first and may run zero times; Do ... Loop Until runs at least once. Use the latter for "must try once" cases such as reading a file or prompting.
While
Dim i
i = 1
While i <= 3
WScript.Echo i
i = i + 1
Wend1
2
3While ... Wend is the legacy form: test-first only, and no Exit. New code uses Do While ... Loop, which is equivalent but supports Exit Do.
退出循环
Dim i
For i = 1 To 100
If i * i > 50 Then
WScript.Echo "第一个平方超过 50 的数是 " & i
Exit For
End If
Next
Dim j
j = 0
Do
j = j + 1
If j = 5 Then Exit Do
Loop
WScript.Echo "j = " & j第一个平方超过 50 的数是 8
j = 5Exit For and Exit Do leave only the innermost loop. To break out of nested loops, set a flag and check it in the outer loop — VBScript has no labelled break.
跳过本次
Dim i
For i = 1 To 10
If i Mod 2 = 0 Then
' 没有 Continue For,只能把逻辑放进 If 里
Else
WScript.Echo i
End If
Next1
3
5
7
9VBScript has no Continue. Either wrap the body in an If or hand-control a Do ... Loop — the latter reads worse, so the If is usual.
嵌套与性能
Dim i, j, count
count = 0
For i = 1 To 100
For j = 1 To 100
count = count + 1
Next
Next
WScript.Echo "内层执行了 " & count & " 次"内层执行了 10000 次For Next
Dim i, sum
sum = 0
For i = 1 To 10
sum = sum + i
Next
WScript.Echo "1..10 的和 = " & sum
For i = 10 To 1 Step -2
WScript.Write i & " "
Next
WScript.Echo ""1..10 的和 = 55
10 8 6 4 2Step 可以是负数(倒序)或小数。省略时是 1。For i = 1 To 10 会执行 10 次,包含两端——和 C 系语言的 i < 10 不同,容易数错一次。
For Each
Dim langs, lang
langs = Array("VBScript", "JScript", "PowerShell")
For Each lang In langs
WScript.Echo lang
Next
' 遍历字典的值
Dim d
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Apple"
d.Add "b", "Banana"
Dim k
For Each k In d.Keys
WScript.Echo k & " = " & d(k)
NextVBScript
JScript
PowerShell
a = Apple
b = BananaFor Each 只能遍历,拿不到下标。需要下标就用 For i = 0 To UBound(arr)。遍历集合时不要增删元素——会打乱枚举器。
Do Loop
' 先判断后执行
Dim n
n = 0
Do While n < 3
WScript.Echo "n = " & n
n = n + 1
Loop
' 先执行后判断
Dim m
m = 0
Do
WScript.Echo "m = " & m
m = m + 1
Loop Until m >= 3n = 0
n = 1
n = 2
m = 0
m = 1
m = 2Do While ... Loop 先判断,可能一次都不执行;Do ... Loop Until 先执行,至少跑一次。读文件、弹对话框这类「必须先试一次」的场景用后者。
While
Dim i
i = 1
While i <= 3
WScript.Echo i
i = i + 1
Wend1
2
3While ... Wend 是老式写法,只能「先判断」,也不能 Exit。新代码统一用 Do While ... Loop——语义一样但更灵活,还能 Exit Do。
退出循环
Dim i
For i = 1 To 100
If i * i > 50 Then
WScript.Echo "第一个平方超过 50 的数是 " & i
Exit For
End If
Next
Dim j
j = 0
Do
j = j + 1
If j = 5 Then Exit Do
Loop
WScript.Echo "j = " & j第一个平方超过 50 的数是 8
j = 5Exit For 和 Exit Do 只能跳出当前一层。嵌套循环要跳两层,只能设一个标志变量在外层判断——VBScript 没有标签跳转。
跳过本次
Dim i
For i = 1 To 10
If i Mod 2 = 0 Then
' 没有 Continue For,只能把逻辑放进 If 里
Else
WScript.Echo i
End If
Next1
3
5
7
9VBScript 没有 Continue。想跳过,要么把逻辑包进 If,要么用 Do ... Loop 手工控制计数——后者可读性差,一般直接用 If。
嵌套与性能
Dim i, j, count
count = 0
For i = 1 To 100
For j = 1 To 100
count = count + 1
Next
Next
WScript.Echo "内层执行了 " & count & " 次"内层执行了 10000 次UBound、Len、对象属性访问)提出来,能快好几倍。