472,791 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,791 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 71734
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.