Link to home
Start Free TrialLog in
Avatar of desmondg
desmondg

asked on

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

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?
Avatar of Brendt Hess
Brendt Hess
Flag of United States of America image

Use Exit Do:

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

ASKER CERTIFIED SOLUTION
Avatar of nfernand
nfernand

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Vbmaster
Vbmaster

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
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
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
Avatar of desmondg

ASKER

Thanks.  

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