Click to See Complete Forum and Search --> : Do you know if VB has a similiar "continue" statement like in C?


kevin shen
May 11th, 2001, 01:19 PM
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

Z LoveLife
May 11th, 2001, 02:13 PM
Yes it does

Exit Do

Johnny101
May 11th, 2001, 02:14 PM
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

Z LoveLife
May 11th, 2001, 02:16 PM
ooops sorry... that's what I get for reading too fast

Cimperiali
May 14th, 2001, 08:27 AM
...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.

Spectre
May 14th, 2001, 07:18 PM
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

shree
May 14th, 2001, 07:40 PM
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.

Spectre
May 15th, 2001, 06:03 AM
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