Do you know if VB has a similiar "continue" statement like in C?
Do you know if VB has a similiar "continue" statement? I want the statement to exit the current loop(i) then go into the next loop(i+1).
Thanks!
Best Regards,
Kevin Shen
Re: Do you know if VB has a similiar "continue" statement like in C?
Re: Do you know if VB has a similiar "continue" statement like in C?
The only way that i know of is to set up a label, just before the end of the loop and when a certain condition is met, jump to that label:
for i = 1 to 20
if i mod 7 = 0 then
Goto NextOne
else
DoSomethingElse
end if
NextOne:
next i
something like that should work.
john
John Pirkey
MCSD
http://www.ShallowWaterSystems.com
http://www.stlvbug.org
Re: Do you know if VB has a similiar "continue" statement like in C?
ooops sorry... that's what I get for reading too fast
Re: Do you know if VB has a similiar "continue" statement like in C?
...but you could do the contrary:
do something only if it matches your criteria,
else... do nothing and go on counting!
ie:
for i = 1 to 20
'if matched, do something
if i mod 7 <> 0 then
DoSomethingElse
end if
'if not matches, do nothing and just
'add 1 to i
next i
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood Bruno Paris and all the other wonderful people who made and make Codeguru a great place. Come back soon, you Gurus.
Re: Do you know if VB has a similiar "continue" statement like in C?
How about...
for i = 1 to 20
'Check for skip condition
If i = 4 then
i = i + 1
else 'Do work
Tmp = Tmp + i
End If
next i
The loop will add i to Tmp until i = 4, this iteration is skipped, then the loop continues with 5 to add i to Tmp. Is this more what you meant?
Hope this helps.
Spectre
Re: Do you know if VB has a similiar "continue" statement like in C?
It is a bad practice to change the loop index inside a loop. It can lead to errors that are difficult to debug. You could do with a Do...Loop statement instead, if the index needs to be changed.
Re: Do you know if VB has a similiar "continue" statement like in C?
I've been using this technique for almost 10 years and have yet for it to cause a problem - let alone an error that is hard to find, however, if you are uncomfortable with it don't do it.
Hope this helps.
Spectre