Results 1 to 11 of 11

Thread: [RESOLVED] any difference between a Do...Loop and a While..End While loop?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2009
    Location
    Australia
    Posts
    130

    Resolved [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.

  2. #2
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    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

  3. #3
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: any difference between a Do...Loop and a While..End While loop?

    Quote Originally Posted by IanRyder View Post
    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
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  4. #4
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    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

  5. #5
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    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:
    1. '
    2.         While Not myValue = 1
    3.            
    4.         End While

    Is the same as this:-
    vbnet Code:
    1. '
    2.         Do While Not myValue = 1
    3.            
    4.         Loop
    Last edited by Niya; May 6th, 2013 at 06:49 AM.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  6. #6
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    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:-

    While Not myValue = 1
    and

    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?

    Cheers,

    Ian

  7. #7
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    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.

    Quote Originally Posted by IanRyder View Post
    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
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  8. #8
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    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

  9. #9
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: any difference between a Do...Loop and a While..End While loop?

    Quote Originally Posted by vbforever23 View Post
    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    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

  11. #11
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: any difference between a Do...Loop and a While..End While loop?

    Quote Originally Posted by IanRyder View Post
    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width