|
-
Apr 1st, 2004, 02:34 PM
#1
Thread Starter
Frenzied Member
Continue with next iteration
In C or C++ you can exit the body of a loop and just move on to the next iteration without finishing, using the "continue" statement...
Is there a way to do this in VB.Net?
Squirrelly1
Now happily married and still crankin' away at the keyboard.  Life is grand for a coder, no?
-
Apr 1st, 2004, 02:50 PM
#2
Frenzied Member
VB Code:
public sub Something()
if x <> something then
exit sub
else
do this
end if
end sub
do you mean like this?
[/Highlight]
Last edited by Andy; Apr 1st, 2004 at 03:13 PM.
-
Apr 2nd, 2004, 10:17 AM
#3
Thread Starter
Frenzied Member
Well, It could be done like that, but the idea is that it would not have to run through the rest of the lines of code, thus making the whole thing faster...
I guess I'm stuck w/ the long way.
Thanks for ur input,
squirrelly1
Now happily married and still crankin' away at the keyboard.  Life is grand for a coder, no?
-
Apr 2nd, 2004, 10:31 AM
#4
Addicted Member
Are you using a "For Each Next" or a "For Next" loop,
If so just use "Exit For", this will get you out of the loop and just continue on with the rest of the code in the sub.
-
Apr 2nd, 2004, 10:50 AM
#5
Thread Starter
Frenzied Member
The thing is... I don't want to get out of the loop. I want to stay in the loop, but not have to finish the rest of the code that is defined within the loop. Like this...
VB Code:
Dim i as int32
For i = 0 to 9
i = i + 1
if i = 5 then
'<<whatever the code would be>>
end if
i = i - 1
Next i
With the ability to do what I'm thinking of, this code would eventually stop at the <<whatever the code would be>> part and move on to the next itteration where i would = 6 and it would just continue the loop as normal...
Understand?
squirrelly1
Now happily married and still crankin' away at the keyboard.  Life is grand for a coder, no?
-
Apr 2nd, 2004, 11:04 AM
#6
Addicted Member
Code:
Dim i as int32
For i = 0 to 9
i = i + 1
if i = 5 then
'<<whatever the code would be>>
end if
i = i - 1
Next i
First off if I'm seeing this right, it's an infinite loop! i starts off 0 then is increased by 1, but then later decraesed by 1???
The value i would never get higher than 1.
Just let the For statement increase i.
if you want to exit a loop a specific value then just use:
Code:
Dim i as int32
For i = 0 to 9
if i = 5 then
'<<whatever the code would be>>
i='Any Value you want to jump to
end if
Next i
To be honest I still don't understand.
-
Apr 2nd, 2004, 11:12 AM
#7
Thread Starter
Frenzied Member
You caught me... I wasn't paying too much attention to that code. But the idea is still the same. I just don't want to have to move through the rest of the code before going on to the next itteration of the loop. For instance...
If I have some 30 lines of code within my for loop and after the first 5 i do some sort of check and realize that I don't need to go through the other 25 lines of code for that itteration, is there any way to get of of doing those other 25 lines?
Just setting i = to whatever won't work b/c I'm still required to move through the rest of the lines before getting to the "next i" statement. I could use a goto statement and put a label just above the "next i" statement, but that just looks sloppy.
Thanks,
Squirrelly1
Now happily married and still crankin' away at the keyboard.  Life is grand for a coder, no?
-
Apr 2nd, 2004, 11:47 AM
#8
Addicted Member
As far as I can see the "Goto" statement is the cleanest way to go.
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
|