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?
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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
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
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
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!
I wonder why something as obvious as the continue statement was left out of the language when so many other things are included!
do while(condition1)
statements...
if condition2 then Exit Do
statements ...
loop