We help IT Professionals succeed at work.

Is there the equivalent of C's continue statement in VB?

desmondg
desmondg asked
on
Is there the equivalent of C's continue statement in VB?
How can I get to the top of a loop?

I've tried using code like the following:

     do while(condition1)
          statements...
          if condition2 then loop
          statements ...
     loop

However this doesn't compile.  The compiler tells me that the loop statement that is in the if statement does not have a corresponding do statement.  

Is there a workaround or some other statement that I may use?
Comment
Watch Question

Brendt HessSenior DBA
CERTIFIED EXPERT

Commented:
Use Exit Do:

do while(condition1)
         statements...
         if condition2 then Exit Do
         statements ...
    loop

Commented:
Hi guys... As I can remember, "exit do" takes me out of the cycle. A work around:

do while(condition1)
   statements...
   if not condition2 then
      statements ...
   end if
loop

Commented:
I usually do this using the GOTO statement, I know it's bad practice to use but sometimes it's needed. ;)

  do while(condition1)
    'statements...
    if condition2 then goto endofloop
    'statements ...
endofloop:
  loop

Another solution would be use NOT on condition2, something like this..

  do while(condition1)
    'statements...
    if (Not condition2) then
      'statements ...
    end if
  loop
I prefer the not logic to the goto statement.

Just an opinion.

dill
Mike McCrackenSenior Consultant
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2013

Commented:
Unfortunately the answer bhess1 gave will exit the loop not exit that iteration and allow it to continue.
What you really want is something more like


   do while(condition1)
      do while (TRUE)
        statements...
        if condition2 then Exit Do
        statements ...
        exit do
      loop
   loop

A little awkward but this will simulate the C continue statement.  the EXIT statements will exit the otherwise infinite inner loop and CONDITION1 is used to control actual execution of the loop.

good luck
mlmcc

Commented:
I usually do this using the GOTO statement, I know it's bad practice to use but sometimes it's needed. ;)

  do while(condition1)
    'statements...
    if condition2 then goto endofloop
    'statements ...
endofloop:
  loop

Another solution would be use NOT on condition2, something like this..

  do while(condition1)
    'statements...
    if (Not condition2) then
      'statements ...
    end if
  loop

Author

Commented:
Thanks.  

I wonder why something as obvious as the continue statement was left out of the language when so many other things are included!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.