"Do While" "Loop" and "While" "Wend" Loop. What's the difference?
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:
- It supports checking the condition before entering the loop
Do While [condition] ... Loop
(zero or more loop executions) - It supports checking the condition after entering 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)
I don't think there is much of a difference in their execution other than the syntactical options that While Wend
is not capable of:
Do someCodeWhile (someCondition)
As for speed, I did a simple test:
Sub whileLoopTest()Dim i As Long, j As LongDim StartTime As Varianti = 1StartTime = TimerWhile (i < 500000000) j = i + 2 i = i + 1WendDebug.Print "While execution time: " & Timer - StartTimeEnd SubSub doWhileTest()Dim i As Long, j As LongDim StartTime As Varianti = 1StartTime = TimerDo While (i < 500000000) j = i + 2 i = i + 1LoopDebug.Print "Do While execution time: " & Timer - StartTimeEnd Sub
Results:
While execution time: 6,429688 While execution time: 6,429688While execution time: 6,441406Do While execution time: 6,429688Do While execution time: 6,449219Do While execution time: 6,4375
In fact, you donĀ“t need "DO WHILE" since you may "DO-LOOP" without "While".
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.
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