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
Printable View
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
VB Code:
public sub Something() if x <> something then exit sub else do this end if end sub
do you mean like this?
[/Highlight]
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
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.
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
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???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
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:
To be honest I still don't understand.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
You caught me... :p 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
As far as I can see the "Goto" statement is the cleanest way to go.