“Do While” “Loop” and “While” “Wend” Loop. What's the difference?

前端 未结 3 1630
無奈伤痛
無奈伤痛 2020-12-08 20:07

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

3条回答
  •  不思量自难忘°
    2020-12-08 21:03

    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
    

提交回复
热议问题