While Vs. Do While

snarfblam

Ultimate Contributor
Joined
Jun 10, 2003
Messages
2,097
Location
USA
This is something ive been wondering a very long time (perhaps years) but have never investigated.

Is there ANY difference programatically, in compilation, or any other kind of difference between a Do While...Loop and a While...While End?
 
Hummm I think it's kept for compatibility issues.

I know their is a difference between Do... While and While... Do (am I right ? huhu).

But ... I think they kept that for compatibility with VB6 language. I recommend using While While End.

Have a nice day!
 
Do While ... Loop and While ... End While are the same thing. I'd assume that they compile to the same assembly instructions.
The While ... End While is sort of like a special version of the Do While ... Loop, since other languages like C have while loops and this would be easily distinguishable as the VB equivalent, but other than that, I use the Do Loop all of the time...
Because you can also have the While condition at the bottom (Do ... Loop While). And then, you can have a Do Until ... Loop which is the opposite of the While loop, and you can also move the Until to the bottom
(Do ... Loop Until) :).
 
Iceplug said:
Because you can also have the While condition at the bottom (Do ... Loop While). And then, you can have a Do Until ... Loop which is the opposite of the While loop, and you can also move the Until to the bottom
(Do ... Loop Until) :).

That is the difference. One loop checks for a condition at the "top" of the loop and the other checks it at the "bottom" of the loop. I can't remember which one does which exactly though...late at night, not enough caffeen...powers...weakening...
;)
 
Come on guys. I asked if there was a difference between a Do While...Loop and a While...While End, not Do Loops and While Loops in general.

All this i know: You can have a Do While... Loop, a Do Until...Loop, Do...Loop While and Do...Loop Until. While... End While appears to be identical to Do While... Loop, which is why I question it's being.

What I dont know: Is there ANY difference programatically, in compilation, or any other kind of difference between a Do While...Loop and a While...While End?
 
Did a quick test:

Visual Basic:
Public Sub test1()
        Dim i As Integer = 3
        While i < 100
            i += 1
        End While
    End Sub

    Public Sub test2()
        Dim i As Integer = 3
        Do While i < 100
            i += 1
        Loop
    End Sub
both produced the same MSIL
Code:
{
  // Code size       14 (0xe)
  .maxstack  2
  .locals init (int32 V_0)
  IL_0000:  ldc.i4.3
  IL_0001:  stloc.0
  IL_0002:  br.s       IL_0008
  IL_0004:  ldloc.0
  IL_0005:  ldc.i4.1
  IL_0006:  add.ovf
  IL_0007:  stloc.0
  IL_0008:  ldloc.0
  IL_0009:  ldc.i4.s   100
  IL_000b:  blt.s      IL_0004
  IL_000d:  ret
}

no difference between them.
 
Wow. A very straightforward and helpful answer. I'm surprised. Thank you very much, PlausiblyDamp.
 
Back
Top