[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.
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
Re: any difference between a Do...Loop and a While..End While loop?
Quote:
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
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
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
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:-
Quote:
While Not myValue = 1
and
Quote:
Do While Not myValue = 1
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? :D
Cheers,
Ian
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:-
Quote:
...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.
Quote:
Originally Posted by
IanRyder
It seems like that Martian head is getting the best of you? :D
Cheers,
Ian
Do you honestly believe that two minds in one body wouldn't have glitches ;)
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 :rolleyes::D
Re: any difference between a Do...Loop and a While..End While loop?
Quote:
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
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.
Re: any difference between a Do...Loop and a While..End While loop?
Quote:
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 :rolleyes::D
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 :sick:.
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.