Reading some answers in stackoverflow I saw a while wend loop. I\'m used to the do while loop, so I was wondering what wo
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:
Do While [condition] ... Loop (zero or more loop executions)
Do ... Loop While [condition] (one or more loop executions)
Do ...(some logic) (Exit Do) ... Loop (one or more loop executions, potentially infinite)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
I don't think there is much of a difference in their execution other than the syntactical options that While Wendis not capable of:
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