Results 1 to 22 of 22

Thread: [RESOLVED] Exit one level up nested For...Next

  1. #1

    Thread Starter
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    Resolved [RESOLVED] Exit one level up nested For...Next

    Hi Guys!

    I am pretty sure it is impossible but I want to ask it, anyway:
    I have
    Code:
    For n = 0 To 5 'The values are not important.
    For m = 1 To 6 'The values are not important.
    .
    .'Do Something
    .
    Next m
    'Do some other stuff
    Next n
    I want to go to Next n if something happens in For..m..Next
    Code:
    For n = 0 To 5 'The values are not important.
    For m = 1 To 6 'The values are not important.
    .
    If something happens Then
    Next n 'Here is the impossible part.
    End If
    .
    Next m
    'Do some other stuff
    Next n
    My solution is
    Code:
    For n = 0 To 5 'The values are not important.
    For m = 1 To 6 'The values are not important.
    .
    If something happens Then
    GoTo Skip
    End If
    .
    Next m
    'Do some other stuff
    Skip:
    Next n
    But I am looking for something better because I am not sure if GoTo slows my code or not.

    Any idea?
    Thanks a lot!
    Last edited by Flashbond; Feb 22nd, 2013 at 09:31 PM.

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Exit one level up nested For...Next

    Exit For should do the trick.

    Code:
    For n = 0 To 5 'The values are not important.
        For m = 1 To 6 'The values are not important.
            .
            If something happens Then
                Exit For
            End If
    .
         Next m
         'Do some other stuff
    
    Next n

  3. #3

    Thread Starter
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    Re: Exit one level up nested For...Next

    Thanks DataMiser but you are missing the point. If I Exit For..m..Next only, it will do "'Do some other stuff", too. The main idea is to skip that line and directly go to Next n.

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Exit one level up nested For...Next

    In that case you may need another IF statement or restructure the code a bit, GoTo is rarely a good choice.

  5. #5
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: Exit one level up nested For...Next

    something like this

    Code:
    dim Skip as boolean = false ' control variable
    
    for n = 0 to 2
    
    for m = 0 to 2 if GoNextn then m = 2 skip = true 'set to skip code part later end if next m
    if skipcodepart = false then 'do code' end if skipcode = false 'reset to false for next loop next n
    something like that should work
    PS i didnt test this but give it a go it should work
    Last edited by GBeats; Feb 23rd, 2013 at 01:53 PM.
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


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

    Re: Exit one level up nested For...Next

    '
    Code:
            Dim somethinghappens As Boolean
            For n As Integer = 0 To 5 'The values are not important.
                For m As Integer = 1 To 6 'The values are not important.
                    '
                    If somethinghappens Then
                        Exit For
                    End If
                    '
                Next m
                If Not somethinghappens Then
                    'Do some other stuff
                End If
            Next n
            '
    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

  7. #7
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Exit one level up nested For...Next

    Wouldn't it be easier to have a While or Until as the inner loop?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  8. #8

    Thread Starter
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    Re: Exit one level up nested For...Next

    Afaik Do..Loop is slower than For..Next. I think a boolean controller will be slower than a GoTo command. Anway, I'll keep using it. Tanks for everyone!

  9. #9
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: [RESOLVED] Exit one level up nested For...Next

    I think GoTo is a bad idea, there are several ways to properly do what you seem to be trying but GoTo is not one of them. Being that you did not provide any info as to what you are actually doing in your loop it is hard to give good advice but I would seriously recommend that you not use a GoTo especially not to break out of a loop, a programming style like that will lead to bad things that are hard to debug.

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

    Re: Exit one level up nested For...Next

    Quote Originally Posted by Flashbond View Post
    Afaik Do..Loop is slower than For..Next. I think a boolean controller will be slower than a GoTo command. Anway, I'll keep using it. Tanks for everyone!
    I am a big proponent of code that is fast as possible. Without knowing all of those things you have deemed unimportant it is hard to argue with you, but the example you gave isn't an argument against a boolean check around 'Do some other stuff. The 'n' loop would have to be millions for it to matter.

    One other thing. If you have need of further assistance with this code you will spend some time explaining why you used GoTo before you even get answers to your new problem.
    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

  11. #11
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: [RESOLVED] Exit one level up nested For...Next

    Afaik Do..Loop is slower than For..Next.
    Not that I know of but, even if it is, it's unlikely to make any significant difference unless you're doing loops of millions of values! The logic will be a lot easier to follow if you're coming back to this code 6 months down the line than Exit For, that's for certain!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

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

    Re: [RESOLVED] Exit one level up nested For...Next

    Quote Originally Posted by dunfiddlin View Post
    Not that I know of but, even if it is, it's unlikely to make any significant difference unless you're doing loops of millions of values! The logic will be a lot easier to follow if you're coming back to this code 6 months down the line than Exit For, that's for certain!
    I am having a hard time visualizing what you are proposing.

    Knuth on GoTo lengthy but worth reading.
    Last edited by dbasnett; Feb 23rd, 2013 at 01:53 PM.
    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
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: [RESOLVED] Exit one level up nested For...Next

    Ok, so on the speed issue, you know I never like to simply take anyone's word for it. Here's the test ...

    vb.net Code:
    1. sw.Start()
    2.         For i = 0 To 99
    3.             For j = 0 To 99
    4.                 If i * j > 9500 Then Exit For
    5.                 Me.Text = j.ToString
    6.             Next
    7.         Next
    8.         sw.Stop()
    9.         Label1.Text = sw.Elapsed.ToString
    10.         sw.Reset()
    11.         sw.Start()
    12.         For i = 0 To 99
    13.             Dim j As Integer = 0
    14.             While j < 100 AndAlso i * j <= 9500
    15.                 Me.Text = j.ToString
    16.                 j += 1
    17.             End While
    18.         Next
    19.         sw.Stop()
    20.         Label2.Text = sw.Elapsed.ToString
    21.         sw.Reset()
    22.         sw.Start()
    23.         For i = 0 To 99
    24.             Dim j As Integer = 0
    25.             Do Until j > 99 Or i * j > 9500
    26.                 Me.Text = j.ToString
    27.                 j += 1
    28.             Loop
    29.         Next
    30.         sw.Stop()
    31.         Label3.Text = sw.Elapsed.ToString

    ... and here's the result (you won't like it!)

    For Next > 2.34 secs

    While > 2.20 secs

    Until > 2.16 secs

    You weren't expecting that now, were you?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  14. #14
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: [RESOLVED] Exit one level up nested For...Next

    Hi Flashbond,

    its up to you what you use, its not going to make alot of diference either way

    goto in simple routines isnt a problem, its used all the time. just leave a comment to remind yourself why its there. im always forgetting what my routines are doing(not because of goto just generally)

    using loops and for/next will make your routine easier to read and the diference in processor time, memory consumption are not even worth considering unless your planning on getting awarded for the fastest loop in the world. using 2 loops i imagine ur just checking tables or something if so then no worries.

    if its a problem then optimise afterwards.
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  15. #15
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: [RESOLVED] Exit one level up nested For...Next

    goto in simple routines isnt a problem, its used all the time.
    Wrong on both counts, I'm afraid!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  16. #16
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: [RESOLVED] Exit one level up nested For...Next

    goto in simple routines isnt a problem, its used all the time.
    By who???

    Accept for error handling in older Vb versions GoTo is rarely used at all. While there are times that it may make sense to use it those times are few and far between. I think in the last 10 years the only times I have used a GoTo in code was Vb6 and before error handling e.g. On Error GoTo ErrTrap

    Aside from that I don't think I have ever needed to use it and it is always better to properly structure your code with If blocks or loops rather than take the lazy way out with a GoTo.

    I have worked on a lot of code written by others in the professional world can't say I have ever encountered a GoTo other than for error handling. Online I see it used from time to time usually by beginners.

  17. #17

    Thread Starter
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    Re: [RESOLVED] Exit one level up nested For...Next

    Ok, I am not an expert. For now GoTo does my job and causes no errors. All that I can say is ignorance is happiness, after all.

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

    Re: [RESOLVED] Exit one level up nested For...Next

    I think Knuth made some strong arguments for where it might be used. It was funny that he admitted that the title of the article was to draw attention.


    The start
    Last edited by dbasnett; Feb 23rd, 2013 at 05:23 PM.
    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

  19. #19
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: [RESOLVED] Exit one level up nested For...Next

    it is too much an invitation to make a mess of one's program.
    That's all that needs to be said for me. What looks convenient in the early stages of development can all too quickly become an explosion at the spaghetti factory. I can say with complete certainty that I haven't used GoTo or any variant of it since I last programmed in actual Basic which would be 1990!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  20. #20
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: [RESOLVED] Exit one level up nested For...Next

    nobody uses it because of all the hysteria related to it, although its marked as 'Bad Programming' and it is using is constantly to redirect your code, the the actual fact is, it doesnt cause a problem whatsoever in a small routine. what possible problem could it cause it the routine above?
    if there was more things going on within the loop that required a loop to finish, or at least the code inside the loop, like say you wanted to update something and you misplaced the goto then its gonna leave you saying, "what the... now what"

    now dont get me wrong i usually use it for debugging errors with basic error traps and would usually use a control variable for controling loops.

    but at the end of the day it works

    if it aint broke dont fix it.
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  21. #21
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: [RESOLVED] Exit one level up nested For...Next

    [Edit: Removed - misread initial post.]
    [Edit 2: And in its place, I shall provide a link: https://blogs.msdn.com/b/ericlippert...edirected=true Basically mirroring my initial reaction at the end: what are you actually trying to do? Continuing an outer loop suggests you've got the semantics of the operation not quite nailed down.]
    Last edited by Evil_Giraffe; Feb 25th, 2013 at 07:53 AM.

  22. #22
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: [RESOLVED] Exit one level up nested For...Next

    I just read the article you posted evil_giraffe, i like how the first comment after it is asking about something the article just told you not to do basically lol.

    anyway, since im relatively new to vb.net, and looking at how syntax is structured in the professional world of programming (like creating classes, and various other examples i see on msdn) is the "Awsome" example done by using other routines to help it look tidy, or is that the actual 'Psuedo' code.

    i think the idea af just having a list of what your checking and looking for what you want is far better then looping through however many variables, but dont you have to loop through the "table" like structure to get the list in the first place.

    how is that working?
    im not putting it down im interested legitimately
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


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