473,714 Members | 2,623 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 71975
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**@discussion s.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******@discu ssions.microsof t.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**@discussion s.microsoft.com> wrote in message
news:7B******** *************** ***********@mic rosoft.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.WriteLi ne("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.WriteLi ne("In the Do While loop")
Loop

Do Until i <= 0
i = i - 1
Console.WriteLi ne("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.WriteLi ne statement. In the following example however, you will
always process that line at least once:
Do
i = i - 1
Console.WriteLi ne("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

"_AnonCowar d" wrote:

"John Pass" <John Pa**@discussion s.microsoft.com> wrote in message
news:7B******** *************** ***********@mic rosoft.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.WriteLi ne("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.WriteLi ne("In the Do While loop")
Loop

Do Until i <= 0
i = i - 1
Console.WriteLi ne("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.WriteLi ne statement. In the following example however, you will
always process that line at least once:
Do
i = i - 1
Console.WriteLi ne("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
2041
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 from a webform. Now, the hardcoded results, I can click "Save Page As" when the results come up and save it as a text file. The other, while it seems to be the same results exactly, gives me an
32
4644
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 where the loop will be traversed a fixed number of times. It is very flexible, and novice programmers should take care not to abuse the power it offers."
7
1939
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 Source DLLs like "log4Cxx" and "Xerces C++" DLL libraries. VS 2005 converted my projects without any problem. Even the proper lib includes and lib path were captured. But its linker just unable to resolve the C++ function references.
0
3007
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
19412
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 strange workaround construct): -- do { // ...
13
17950
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 knight-rider light sequence. Obviously, this means using a loop and our lecturer has told us to use timers to control how long the sequence lasts for. I don't want to use timers ((this is not due to laziness, I would have been finished long ago had I used...
2
2992
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 in the loop be done one more time. Thank u very much for your help deggler
1
4588
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 tutorials but when I change it from here I then get compiling issues... Can someone explain what I've done wrong? import javax.swing.JOptionPane; public class LloydPage_Assignment5 { public static void main (String args) { // Enter Y to...
0
8801
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8707
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9314
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9174
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9015
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6634
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4464
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3158
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.