[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!
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
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.
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.
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
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
'
Re: Exit one level up nested For...Next
Wouldn't it be easier to have a While or Until as the inner loop?
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!
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.
Re: Exit one level up nested For...Next
Quote:
Originally Posted by
Flashbond
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. ;)
Re: [RESOLVED] Exit one level up nested For...Next
Quote:
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!
Re: [RESOLVED] Exit one level up nested For...Next
Quote:
Originally Posted by
dunfiddlin
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.
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:
sw.Start()
For i = 0 To 99
For j = 0 To 99
If i * j > 9500 Then Exit For
Me.Text = j.ToString
Next
Next
sw.Stop()
Label1.Text = sw.Elapsed.ToString
sw.Reset()
sw.Start()
For i = 0 To 99
Dim j As Integer = 0
While j < 100 AndAlso i * j <= 9500
Me.Text = j.ToString
j += 1
End While
Next
sw.Stop()
Label2.Text = sw.Elapsed.ToString
sw.Reset()
sw.Start()
For i = 0 To 99
Dim j As Integer = 0
Do Until j > 99 Or i * j > 9500
Me.Text = j.ToString
j += 1
Loop
Next
sw.Stop()
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?
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.
Re: [RESOLVED] Exit one level up nested For...Next
Quote:
goto in simple routines isnt a problem, its used all the time.
Wrong on both counts, I'm afraid!
Re: [RESOLVED] Exit one level up nested For...Next
Quote:
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.
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.
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
Re: [RESOLVED] Exit one level up nested For...Next
Quote:
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!
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.
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.]
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