Results 1 to 14 of 14

Thread: Number of Loops

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    4

    Number of Loops

    Silly question perhaps, but I am in a debate with a professor to actually how many different loops there are in Visual Basic.

    He is saying there are only 2.

    Do Loops
    For Next Loops

    I am making a case since a While..End While Loop is unique since it can be written without any indication of the DO statement. This makes it a unique loop because of its unique syntax.

    What is the opinion of the users here ?

  2. #2
    Fanatic Member FireXtol's Avatar
    Join Date
    Apr 2010
    Posts
    874

    Re: Number of Loops

    While is deprecated, as it doesn't sport any loop exit statement, and only supports a condition in the first While statement. There's Exit For and Exit Do, but no Exit While.

    There's also no 'End While', the term is Wend.

    While/Wend is merely supported for backwards compatibility, DO/LOOP is the suggested replacement.

    There's, I suppose, technically a forth syntax and method for looping. Using the For Each statement.

    Then, of course, you can do it 'manually' with GoTo.

    Perhaps he means there are two proper ways to loop (assuming For/Next encompasses For Each(they both make use of Exit For, and Next)).

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    4

    Re: Number of Loops

    According the the microsoft web site, there is a end statement for While.


    http://msdn.microsoft.com/en-us/library/zh1f56zs.aspx

  4. #4
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Number of Loops

    Quote Originally Posted by nofrills View Post
    According the the microsoft web site, there is a end statement for While.


    http://msdn.microsoft.com/en-us/library/zh1f56zs.aspx
    Judging by the example code I'd say that is for .Net, because

    counter += 1

    is not valid in VB Classic.

    Also, initializing a variable like: Dim counter As Integer = 0 is not correct, plus you close a While loop with Wend, not End While.

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    4

    Re: Number of Loops

    I appreciate these responses. I am not trying to disprove anyone here, but a smarty Professor and his snippy comments to the students.

    I am using Visual Studios 2008 designing a Visual Basic Form. I used the code example from above and it worked just fine. I have to assume that when I wrote this code it was for visual basic and not .net

    Am I wrong?
    Last edited by nofrills; May 14th, 2010 at 08:12 PM.

  6. #6
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Number of Loops

    If you are using Visual Studio 2002 or newer (which you are) then you are definitelly working with VB.Net so the sample on the website you have posted applies and works for you. And, if that is the case, you posted the question on the wrong forum since this is for VB Classic (that's why we were telling you that you were wrong, since that doesn't work for older VB6 ) I'll notifiy the mods to move the thread to the .Net section so you don't have to repost.

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Number of Loops

    Thread moved from 'VB6 and Earlier' forum to 'VB.Net' (VB2002 and later) forum

    (thanks as always baja_yu )

  8. #8
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Number of Loops

    For VB.NET, the loops are:

    For...Next
    Do...Loop
    While...End While
    For Each...Next

    And there are Exit statements for all of them, and none of them are deprecated, no matter how many people say it.

  9. #9

    Thread Starter
    New Member
    Join Date
    May 2010
    Posts
    4

    Re: Number of Loops

    In my visual basic class, I am still getting smacked around by the professor and some students telling me there are only two loops.

    I am holding fast to my inital answer of 3, For Each making it 4 , regardless how many points I loss in the class for it. Truth is truth, and they are all wrong.

  10. #10
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Number of Loops

    You're losing points for it??? Ok, just give a code example using While to everyone.

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

    Re: Number of Loops

    Aren't we just picking nits?

    Are these the same or different loops

    do while true
    loop

    do
    loop while true

    And isn't
    For Each just a form of For value = initialcount to stopcount?
    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

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

    Re: Number of Loops

    Fundamentally there are two (three with goto). There are many variations of those two, but I would suspect that if you looked at the IL for two examples, they would be close.
    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

  13. #13
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Number of Loops

    For Each will generate completely different IL to use the IEnumerator interface, so I classify it as another type of loop. While is the same as Do when the condition for the Do is at the start of the loop. So there are 3 and a half.

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

    Re: Number of Loops

    Quote Originally Posted by minitech View Post
    For Each will generate completely different IL to use the IEnumerator interface, so I classify it as another type of loop. While is the same as Do when the condition for the Do is at the start of the loop. So there are 3 and a half.
    That is a surprise.

    I knew the difference in the do loops.
    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

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