VB.Net 101: Difference "While" and "Do While/Loop"

G

Guest

What is the difference between a While and Do While/Loop repetition structure.
If they is no difference (as it seems) why do both exist?
 
G

Guest

John,
The Do While/ Loop is the same as While/End While. However note that the
alternative syntax of the Do/ Loop While serves a different purpose. See
below:

///
'Condition is checked and only if the condition is satisfied loop is executed
While <condition>
'Do stuff
End While
\\\

///
'Condition is checked after 1st pass through the loop
Do
'Do stuff
Loop While <condition>
\\\

HTH
 
H

Herfried K. Wagner [MVP]

John Pass said:
What is the difference between a While and Do While/Loop repetition
structure.
If they is no difference (as it seems) why do both exist?

There is no difference. However, note that 'Do...Loop' is much more
flexible than 'While'. In VB6 it was not possible to exit a 'While' loop
using 'Exit While', and thus using 'While' loops made sense because it made
the code more self-documenting. In VB.NET, I never use the 'While' loop
because it's a redundant language feature that doesn't add a value.
 
G

Guest

Sarika,
Thanks for you response. I was familiar with the difference between Do
While/Loop and Do Loop/While. My question referred to the differende between
the:
While structure and the
Do While/Loop structure
According to the text I am reading, they are the same.
But if that is really (100%) so, why do they both exist?
 
H

Herfried K. Wagner [MVP]

John Pass said:
Thanks for you response. I was familiar with the difference between Do
While/Loop and Do Loop/While. My question referred to the differende
between
the:
While structure and the
Do While/Loop structure
According to the text I am reading, they are the same.
But if that is really (100%) so, why do they both exist?

They exist because BASIC had two types of loops: 'Do...Loop' loops which
allowed 'Until' and 'While' end coditions in the head and the tail of the
block and a way to exit the loop and the 'While...Wend' loop which supported
a 'While' condition in its head only and didn't provide a command to exit
the loop. In VB.NET, 'Wend' has been renamed to 'End While' and 'Exit
While' has been added.
 
A

_AnonCoward

:
: What is the difference between a While and Do While/Loop repetition
: structure. If they is no difference (as it seems) why do both exist?


The two constructs are very similar and often achieve the same results:

Dim i As Integer = 3
While i > 0
i = i - 1
Console.WriteLine("In the While loop")
End WHile


However, you have more flexibility with the Do/Loop structure. You can
use a Do While or a Do Until statement that may be clearer. The code is
essentially the same but one may be more appropriate than the other (it
is often a question of personal style more than anything else).

Do While i > 0
i = i - 1
Console.WriteLine("In the Do While loop")
Loop

Do Until i <= 0
i = i - 1
Console.WriteLine("In the Do Until loop")
Loop


However, the real benefit of a Do / Loop is that you can delay the
conditional until the Loop runs at least once. In the examples above, if
i was set to an initial value of 0, you'd never hit the
Console.WriteLine statement. In the following example however, you will
always process that line at least once:


Do
i = i - 1
Console.WriteLine("In the Do loop")
Loop Until i < 1


Ralf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top