|
-
Feb 22nd, 2013, 09:19 PM
#1
Thread Starter
Fanatic Member
[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.
-
Feb 22nd, 2013, 09:49 PM
#2
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
-
Feb 22nd, 2013, 10:02 PM
#3
Thread Starter
Fanatic Member
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.
-
Feb 22nd, 2013, 11:44 PM
#4
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.
-
Feb 23rd, 2013, 12:19 AM
#5
Fanatic Member
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

-
Feb 23rd, 2013, 11:12 AM
#6
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
'
-
Feb 23rd, 2013, 11:18 AM
#7
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!
-
Feb 23rd, 2013, 11:56 AM
#8
Thread Starter
Fanatic Member
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!
-
Feb 23rd, 2013, 12:14 PM
#9
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.
-
Feb 23rd, 2013, 12:30 PM
#10
Re: Exit one level up nested For...Next
 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.
-
Feb 23rd, 2013, 01:01 PM
#11
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!
-
Feb 23rd, 2013, 01:22 PM
#12
Re: [RESOLVED] Exit one level up nested For...Next
 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.
Last edited by dbasnett; Feb 23rd, 2013 at 01:53 PM.
-
Feb 23rd, 2013, 02:07 PM
#13
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?
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!
-
Feb 23rd, 2013, 02:13 PM
#14
Fanatic Member
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

-
Feb 23rd, 2013, 02:27 PM
#15
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!
-
Feb 23rd, 2013, 02:33 PM
#16
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.
-
Feb 23rd, 2013, 02:47 PM
#17
Thread Starter
Fanatic Member
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.
-
Feb 23rd, 2013, 05:15 PM
#18
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.
-
Feb 23rd, 2013, 07:21 PM
#19
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!
-
Feb 25th, 2013, 06:48 AM
#20
Fanatic Member
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

-
Feb 25th, 2013, 07:38 AM
#21
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.
-
Feb 28th, 2013, 12:43 PM
#22
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|