473,385 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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?

Nov 21 '05 #1
6 71897
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
Nov 21 '05 #2
"John Pass" <John Pa**@discussions.microsoft.com> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3
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?

"Sarika" wrote:
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

Nov 21 '05 #4
"John Pass" <Jo******@discussions.microsoft.com> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #5

"John Pass" <John Pa**@discussions.microsoft.com> wrote in message
news:7B**********************************@microsof t.com...
:
: 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
Nov 21 '05 #6
Thanks Ralf for the additional explanation; that waas very clear!

Regards,

John

"_AnonCoward" wrote:

"John Pass" <John Pa**@discussions.microsoft.com> wrote in message
news:7B**********************************@microsof t.com...
:
: 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

Nov 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sugapablo | last post by:
I have two ASP pages. They basically query a database, and spit out the information as plain text in CSV format. The first has the SQL query hardcoded into it. The second takes a SQL query...
32
by: Toby Newman | last post by:
At the page: http://www.strath.ac.uk/IT/Docs/Ccourse/subsection3_8_3.html#SECTION0008300000000000000 or http://tinyurl.com/4ptzs the author warns: "The for loop is frequently used, usually...
7
by: Jeff Lynn | last post by:
Help! I recently upgraded my VS V6 to VS 2005 and was unable to build projects that were perfectly ok under VS V6. Where VS 2005 fails was in the linker resolving external DLLs, which are Open...
0
by: U S Contractors Offering Service A Non-profit | last post by:
" Visionary Dreams " " Leaving New york City leaving to go " GOD noes were i Don't "
14
by: Jan Schmidt | last post by:
Hi, in a nested do-while-loop structure I would like to "continue" the outer loop. With goto this should be no problem in while-loops. However, for do-while I cannot get it to work (without a...
13
by: Sunbags | last post by:
Hello, I'm a 2nd year Computer Engineering student and I have a problem with my VB6 code. I've just started learning VB6 for a project in which we have to create a form which displays the...
2
by: deggler | last post by:
hi, i'd need to run a while ($row = mysql_fetch_array($result)) loop thru the whole table and then one more time. this last time the $row variable shouldn't get any values. i just need everything...
1
by: robotlizz | last post by:
Hello - I am a brand new at Java and I am having a hard time with a program I have to turn in tomorrow. I can not get the 'Q' option to work and the loop goes on forever. I've tried to go over the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.