structuring a loop

  • Thread starter Pablo Cardellino
  • Start date
P

Pablo Cardellino

Hi,
I need to structure a loop this way:

Do
.....
If .... then Loop
.....
Loop

So, the loop will perform a series of actions, and there is a condition in a
certain point that may trigger the next iteration, ignoring the rest of the
current one. Is it possible? Should I use another loop instruction?

Thanks in advance,

Pablo Cardellino
Florianópolis, SC
Brazil
 
J

Jay Freedman

Pablo said:
Hi,
I need to structure a loop this way:

Do
.....
If .... then Loop
.....
Loop

So, the loop will perform a series of actions, and there is a
condition in a certain point that may trigger the next iteration,
ignoring the rest of the current one. Is it possible? Should I use
another loop instruction?
Thanks in advance,

Pablo Cardellino
Florianópolis, SC
Brazil

Rewriting your structure to label the .... parts:
Do
A
If (condition) then Loop
B
Loop

What you need is to write it this way:

Do
A
If Not (condition) then
B
End If
Loop

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
P

Pablo Cardellino

Hi, Jay,
Rewriting your structure to label the .... parts:


What you need is to write it this way:

Do
A
If Not (condition) then
B
End If
Loop

Well, I've simplified the structure a lot for the problem explanation. I
can't do that, because the B statemens couldn't have to be run if the
condition is true. Think of this:

Do
A
If condA then
yadayada
If condB then Loop
End If
B
Loop

In this case, I could structure the code this way:

Do
A
If condA And Not condB then
B
Else If condA
yadayada
End If
Loop

But A, B and yadayada are not simple statements as well, and include several
processes and conditions that make the structured code not simple, and
unordered for reading and debugging. I'd like to keep the order of A,
yadayada and B statments blocks as they are, because they are ordered in a
more understandable way (for me, of course). If there is no way for escaping
to the next loop, I think I could use some GoTo statements. The code would
result less structured but at tthe same time more understandable.


Pablo Cardellino
Florianópolis, SC
Brazil
 
J

Jay Freedman

Hi, Jay,


Well, I've simplified the structure a lot for the problem explanation. I
can't do that, because the B statemens couldn't have to be run if the
condition is true. Think of this:

Do
A
If condA then
yadayada
If condB then Loop
End If
B
Loop

In this case, I could structure the code this way:

Do
A
If condA And Not condB then
B
Else If condA
yadayada
End If
Loop

But A, B and yadayada are not simple statements as well, and include several
processes and conditions that make the structured code not simple, and
unordered for reading and debugging. I'd like to keep the order of A,
yadayada and B statments blocks as they are, because they are ordered in a
more understandable way (for me, of course). If there is no way for escaping
to the next loop, I think I could use some GoTo statements. The code would
result less structured but at tthe same time more understandable.


Pablo Cardellino
Florianópolis, SC
Brazil

Hi Pablo,

I think you will have to use GoTo statements, with labels placed just before the
Loop statements.

There are Exit statements (Exit Do, Exit For, etc.) that terminate loops, but
VBA doesn't have the equivalent of the Continue statement that some languages
use to skip to the end of the loop and then start another iteration.

Assuming that you have one GoTo statement and one label per loop, the code won't
be much less structured than if the Continue statement was available.

You may get a chuckle from the discussion at
http://en.wikipedia.org/wiki/Considered_harmful.
 
T

Tony Jollans

I think you will have to use GoTo statements, with labels placed just
before the
Loop statements.

An alternative technique - which has much the same effect, whilst appearing
more structured - is along these lines:

Do: For Dummy = 0 To 1 Step 0

Some code
If SomeCondition Then
Some more code
If AnotherCondition Then Exit For
Still more code
End If
Yet more code
If ConditionX Then
CodeX
Exit For
End If
Final section of code

Exit For: Next Dummy: Loop

The nested infinite, but always escaped, For .. Next block provides a
mechanism of implementing an Iterate statement by coding Exit For. It's not
perfect and there can be complications if you have other For ... Next blocks
within the Loop, but that is an issue that exists with nested For ... Next
blocks anyway.
 
P

Pablo Cardellino

Hi, Tony,
The nested infinite, but always escaped, For .. Next block provides a
mechanism of implementing an Iterate statement by coding Exit For. It's
not perfect and there can be complications if you have other For ... Next
blocks within the Loop, but that is an issue that exists with nested For
... Next blocks anyway.

it seems to be a useful technique. I will study it and if I don't find any
problem with it I'll use it.

Thanks to both, Tony and Jay

Pablo Cardellino
Florianópolis, SC
Brazil
 
A

alborg

Hi Pablo:

You should check out Word VBA's on-line help example-

'-------------------------------------------
Dim Check, Counter
Check = True: Counter = 0 ' Initialize variables.
Do ' Outer loop.
Do While Counter < 20 ' Inner loop.
Counter = Counter + 1 ' Increment Counter.
If Counter = 10 Then ' If condition is True.
Check = False ' Set value of flag to False.
Exit Do ' Exit inner loop.
End If
Loop
Loop Until Check = False ' Exit outer loop immediately.
'------------------------------------------------------
Like it's been mentioned, you can do a GoTo statement too...

Cheers,
Al
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top