|
-
May 6th, 2013, 04:41 AM
#1
Thread Starter
Addicted Member
[RESOLVED] any difference between a Do...Loop and a While..End While loop?
The way that my reference book is describing these two loops, they seem to two different ways to do the same thing?
Thanks in advance.
-
May 6th, 2013, 05:03 AM
#2
Re: any difference between a Do...Loop and a While..End While loop?
Hi,
The difference between a While/End While Loop and a Do/Loop Until Loop is that the code within a While Loop may never be executed since the Condition controlling the execution of the Loop is Tested at the Start of the Loop but in the case of the Do Loop the Loop will ALWAYS be executed at least once since the Condition controlling the Loop is tested after the first execution of the code within the Loop.
To demonstrate, have a play with this quick example:-
Code:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim myValue As Integer = 1
Do
MsgBox("Do Loop Always Occurs At Least Once Since the Condition is Tested After The Code in the Loop Has Been Executed!")
Loop Until myValue = 1
While Not myValue = 1
MsgBox("The Code In a While Loop May Never be Executed Since the Condition is Tested Before Entry into the Loop!")
End While
End Sub
Hope that helps.
Cheers,
Ian
-
May 6th, 2013, 05:33 AM
#3
Re: any difference between a Do...Loop and a While..End While loop?
 Originally Posted by IanRyder
Hi,
The difference between a While/End While Loop and a Do/Loop Until Loop is that the code within a While Loop may never be executed since the Condition controlling the execution of the Loop is Tested at the Start of the Loop but in the case of the Do Loop the Loop will ALWAYS be executed at least once since the Condition controlling the Loop is tested after the first execution of the code within the Loop.
Actually this is misleading. While your code is not wrong, what you should really be telling the OP is that a Do...Loop is more flexible in that you can choose to evaluate the condition at the start or the end. A While...End While loop doesn't have that flexibility, you can only evaluate at the start. Its also more flexible in that you can choose to iterate a Do...Loop while a condition is True or until it becomes True.
Simply stated, a Do...Loop can emulate any While...End While loop while the reverse is not true.
The Do...Loop is clearly superior and there really isn't a need for a While...End While loop. While...End While is simply a terse version of Do While...Loop
-
May 6th, 2013, 05:41 AM
#4
Re: any difference between a Do...Loop and a While..End While loop?
Hi Niya,
Actually, I would disagree with you on that point but I will not hijack this thread to debate the point.
Cheers,
Ian
-
May 6th, 2013, 06:43 AM
#5
Re: any difference between a Do...Loop and a While..End While loop?
No, I'm not saying you were wrong...just incomplete.
Do...Loop is more flexible. This:-
vbnet Code:
' While Not myValue = 1 End While
Is the same as this:-
vbnet Code:
' Do While Not myValue = 1 Loop
Last edited by Niya; May 6th, 2013 at 06:49 AM.
-
May 6th, 2013, 07:36 AM
#6
Re: any difference between a Do...Loop and a While..End While loop?
Hi Niya,
In fear of Hijacking this thread, (OP, please forgive me and just Shout if this is starting to confuse you or is getting in the way of answering your question), but:-
and
Is exactly the same thing, in that, they may NEVER be executed dependant on the value of the variable myValue whereas a Do/Loop Until type of Loop is ALWAYS executed once and is then tested for additional continuations of the Loop at the end of the code execution.
It seems like that Martian head is getting the best of you? 
Cheers,
Ian
-
May 6th, 2013, 07:42 AM
#7
Re: any difference between a Do...Loop and a While..End While loop?
Well I just wanted to clear things up because you said this:-
...but in the case of the Do Loop the Loop will ALWAYS be executed at least once since the Condition controlling the Loop is tested after the first execution of the code within the Loop.
This implies that Do...Loops can only evaluate their conditions after one iteration which is not true.
 Originally Posted by IanRyder
It seems like that Martian head is getting the best of you?
Cheers,
Ian
Do you honestly believe that two minds in one body wouldn't have glitches
-
May 6th, 2013, 07:59 AM
#8
Re: any difference between a Do...Loop and a While..End While loop?
Hi Niya,
Ahhhh, I now see your point since I made a typo in my original post by saying "Do Loop" instead of "Do/Loop Until", so, valid point made.
However, that said, as a human programmer you knew what I was talking about since I made several references to Do/Loop Until?, but as a Martian, you seem to enjoy "splitting hairs" on our minor human failings?
vbforever23, the Martian in our midst is technically correct, but i hope this discussion has been beneficial to your understanding of the While and Do Loops.
Kind regards to all,
Ian 
-
May 6th, 2013, 08:13 AM
#9
Re: any difference between a Do...Loop and a While..End While loop?
 Originally Posted by vbforever23
The way that my reference book is describing these two loops, they seem to two different ways to do the same thing?
Thanks in advance.
If the book says that this
Code:
Dim i As Integer
i = 0
While i < 2
i = i + 1
End While
and this
Code:
Dim i As Integer
i = 0
Do While i < 2
i = i + 1
Loop
are the same, then they are correct. As has been discussed the Do version provides many more alternatives that might prove useful.
The documentation is always worth looking at.
While...End While
Do...Loop
-
May 6th, 2013, 10:35 AM
#10
Re: any difference between a Do...Loop and a While..End While loop?
In any case: The two will be effectively the same as far as performance is concerned. The differences, while there are some, are pretty minor and don't have any ramification on performance.
My usual boring signature: Nothing
 
-
May 6th, 2013, 06:55 PM
#11
Re: any difference between a Do...Loop and a While..End While loop?
 Originally Posted by IanRyder
Hi Niya,
Ahhhh, I now see your point since I made a typo in my original post by saying "Do Loop" instead of "Do/Loop Until", so, valid point made.
However, that said, as a human programmer you knew what I was talking about since I made several references to Do/Loop Until?, but as a Martian, you seem to enjoy "splitting hairs" on our minor human failings?
vbforever23, the Martian in our midst is technically correct, but i hope this discussion has been beneficial to your understanding of the While and Do Loops.
Kind regards to all,
Ian  
When instructing your primitive species, I like to be as thorough as possible. We all know how you humans like to fill knowledge gaps with strange gods .
Seriously though, if you had made such an error answering a question for say jmcilhinney or Shaggy, I would have left it as is because I'd expect them to know what you meant but you have to be weary of taking too many liberties when explaining something to a relative beginner. In this case, you could have ended up creating an experienced programmer years down the line who never knew that Do...Loops could specify their condition at the beginning.
Last edited by Niya; May 6th, 2013 at 07:00 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|