Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
754 views
in Technique[技术] by (71.8m points)

vba - "Do While" "Loop" and "While" "Wend" Loop. What's the difference?

Reading some answers in stackoverflow I saw a while wend loop. I'm used to the do while loop, so I was wondering what would be the difference between this two loops.

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

  1. It supports checking the condition before entering the loop Do While [condition] ... Loop (zero or more loop executions)
  2. It supports checking the condition after entering the loop Do ... Loop While [condition] (one or more loop executions)
  3. It supports no specific condition Do ...(some logic) (Exit Do) ... Loop (one or more loop executions, potentially infinite)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...