473,289 Members | 1,940 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,289 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 71861
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.