vba “Do While”“Loop”和“While”“Wend”循环。有什么不同?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32728334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Do While" "Loop" and "While" "Wend" Loop. What's the difference?
提问by umLu
Reading some answers in stackoverflow I saw a whilewendloop. I'm used to the do whileloop, so I was wondering what would be the difference between this two loops.
在 stackoverflow 中阅读一些答案我看到了一个whilewend循环。我已经习惯了do whileloop,所以我想知道这两个循环之间有什么区别。
I did some testing (code below) and both seem to give me the same results.
我做了一些测试(下面的代码),两者似乎都给了我相同的结果。
Sub test_loop_1()
Dim i As Integer
i = 1
Do While i < 10
Cells(i, 1) = i
i = i + 1
Loop
End Sub
Sub test_loop_2()
Dim i As Integer
i = 1
While i < 10
Cells(i, 1) = i
i = i + 1
Wend
End Sub
回答by NeepNeepNeep
An answer I referred to is no longer visible, but this answer still holds true. While/Wend is a hangover from Basic and Do/Loop should be your preferred syntax because:
我提到的答案不再可见,但这个答案仍然适用。While/Wend 是 Basic 的后遗症,而 Do/Loop 应该是您的首选语法,因为:
- It supports checking the condition beforeentering the loop
Do While [condition] ... Loop(zero or more loop executions) - It supports checking the condition afterentering the loop
Do ... Loop While [condition](one or more loop executions) - It supports no specific condition
Do ...(some logic) (Exit Do) ... Loop(one or more loop executions, potentially infinite)
- 它支持在进入循环之前检查条件
Do While [condition] ... Loop(零个或多个循环执行) - 支持进入循环后检查条件
Do ... Loop While [condition](一个或多个循环执行) - 它不支持特定条件
Do ...(some logic) (Exit Do) ... Loop(一个或多个循环执行,可能是无限的)
回答by Vegard
I don't think there is much of a difference in their execution other than the syntactical options that While Wendis not capable of:
除了无法执行以下操作的语法选项外,我认为它们的执行没有太大区别While Wend:
Do
someCode
While (someCondition)
As for speed, I did a simple test:
至于速度,我做了一个简单的测试:
Sub whileLoopTest()
Dim i As Long, j As Long
Dim StartTime As Variant
i = 1
StartTime = Timer
While (i < 500000000)
j = i + 2
i = i + 1
Wend
Debug.Print "While execution time: " & Timer - StartTime
End Sub
Sub doWhileTest()
Dim i As Long, j As Long
Dim StartTime As Variant
i = 1
StartTime = Timer
Do While (i < 500000000)
j = i + 2
i = i + 1
Loop
Debug.Print "Do While execution time: " & Timer - StartTime
End Sub
Results:
结果:
While execution time: 6,429688
While execution time: 6,429688
While execution time: 6,441406
Do While execution time: 6,429688
Do While execution time: 6,449219
Do While execution time: 6,4375
回答by David BS
In fact, you don′t need "DO WHILE" since you may "DO-LOOP" without "While".
事实上,你不需要“DO WHILE”,因为你可以在没有“While”的情况下“DO-LOOP”。
I utilize "DO LOOP" if I need to perform an action at least one time (or several times) with no implicit condition, as the WHILE-WEND forces.
如果我需要在没有隐含条件的情况下至少执行一次(或多次)操作,我会使用“DO LOOP”,因为 WHILE-WEND 强制。
Just for an instance, a kind of alarm clock:
举个例子,一种闹钟:
Do
Display Time at screen
Sounds a buzz
if user confirm
exit do
end if
Loop

