Re: Exit Do

Tech-Archive recommends: Fix windows errors by optimizing your registry



Robbie Flower schrieb:

I've been instructed by a dot net / asp programmer that Exit Do is not a great way to end a loop.

Ask him/her to define "great way".

Can someone explain to me if this is so, or does this only apply to higher level programming languages ?

Code in *all* languages should be easy to understand and not relying
on tricks/surprises.

I am using nested loops and need to exit out of one loop into another for something to apply. Any other functional ways of doing this ?

A loop should terminate on a clearly defined condition:

Do Until oTS.AtEndOfStream
sLine = oTS.ReadLine
... process sLine ...
Loop
oTS.Close

To handle special a special case by "Exit Do"

Do Until oTS.AtEndOfStream
sLine = oTS.ReadLine
... process sLine ...
If "no more interesting stuff" = sLine
Exit Do
End If
Loop
oTS.Close

looks ok to me, but you can use an extra variable instead:

bAbort = False
Do Until oTS.AtEndOfStream Or bAbort
...
If "no more interesting stuff" = sLine
bAbort = True
End If
Loop
oTS.Close

I think that this is more complicated (3 places to think about
bAbort and 1 Or to take into account), but YMMV.

If you use Exit Do/For/Sub/Function, you should be aware that
'jumping to the right place' in deeply nested constructs may
not be possible (no labels in VBScript).
.