Whats the difference between do while and while in VB.NET?

jaffa picture jaffa · Apr 17, 2013 · Viewed 17.4k times · Source

What's the difference between Do While where the statement is the first line in the loop block and just the single While in VB.NET?

They don't seem to offer any difference in behavior.

Answer

dbasnett picture dbasnett · Apr 17, 2013

In Visual Basic these are identical:

    Dim foo As Boolean = True

    While Not foo
        Debug.WriteLine("!")
    End While

    Do While Not foo
        Debug.WriteLine("*")
    Loop

These are not; the do executes once:

    Dim foo As Boolean = True

    While Not foo
        Debug.WriteLine("!")
    End While

    Do
        Debug.WriteLine("*")
    Loop While Not foo