Try/Finally the only way to Exit nested For loops?

  • Thread starter Thread starter Joe Duchtel
  • Start date Start date
J

Joe Duchtel

Hello -

I was wondering if it is the best way to exit nested For loops by
using an empty Try/Finally block? Is there any other "nicer" way of
doing this in Visual Basic?

Try
For i ...
For j ...
If lError Then Return Nothing ' This will also enter the
Finally block
For k ...
Exit Try

Finally
End Try

Thanks,
Joe
 
Hello -

I was wondering if it is the best way to exit nested For loops by
using an empty Try/Finally block?  Is there any other "nicer" way of
doing this in Visual Basic?

Try
  For i ...
    For j ...
      If lError Then Return Nothing   ' This will also enter the
Finally block
      For k ...
        Exit Try

Finally
End Try

Well, there is an Exit For but just like the C# break statement, it
can only exit the For loop it is in. If it a nested loop and you want
to exit out of all, you are going to have to add some exit code at the
end of each outer loop.
 
Joe said:
I was wondering if it is the best way to exit nested For loops by
using an empty Try/Finally block? Is there any other "nicer" way of
doing this in Visual Basic?

Try
For i ...
For j ...
If lError Then Return Nothing ' This will also enter the
Finally block
For k ...
Exit Try

Finally
End Try

You could set a flag and use Exit For:

Dim leave As Boolean = False
For i ...
For j ...
For k ...
If somethingScary Then
leave = True
Exit For
End If
Next
If leave Then Exit For
Next
If leave Then Exit For
Next

Andrew
 
You could set a flag and use Exit For:

Dim leave As Boolean = False
For i ...
    For j ...
       For k ...
        If somethingScary Then
         leave = True
         Exit For
        End If
     Next
     If leave Then Exit For
    Next
    If leave Then Exit For
Next

Andrew

Hello -

Yep ... the problem is that the Exit For only exits the current For.
There are other languages with named for loops but I guess not in
Visual Basic.

Is it stylistically a bad thing to use the Try/Finally for this?

Thanks,
Joe
 
VB.Net still has a GoTo. Much as I dislike GoTos I think that a GoTo might
be justified here.

Bob

You could set a flag and use Exit For:

Dim leave As Boolean = False
For i ...
For j ...
For k ...
If somethingScary Then
leave = True
Exit For
End If
Next
If leave Then Exit For
Next
If leave Then Exit For
Next

Andrew

Hello -

Yep ... the problem is that the Exit For only exits the current For.
There are other languages with named for loops but I guess not in
Visual Basic.

Is it stylistically a bad thing to use the Try/Finally for this?

Thanks,
Joe
 
Joe said:
On Feb 4, 8:36 am, "Andrew Morton" wrote:

Hello -

Yep ... the problem is that the Exit For only exits the current For.
There are other languages with named for loops but I guess not in
Visual Basic.

Did you notice I set a flag which will bubble the exits all the way to the
outside?
Is it stylistically a bad thing to use the Try/Finally for this?

It's intended for catching errors, not for flow-control.

However, I notice you were using "Return someValue" in your OP. If your
nested loops are in a function, then you can use Return someValue at any
point to leave the function.

Andrew
 
Perhaps you might give us a slightly more realistic example...

No offence meant, but with a little more detail as to the nature of your
situation, perhaps a better solution can be provided.

I'm thinking something involving ForEach / Procedures etc

Perhaps the need to break out of both loops can be avoided entirely by narrowing
the list of things you are looping through
 
Perhaps you might give us a slightly more realistic example...

No offence meant, but with a little more detail as to the nature of your
situation, perhaps a better solution can be provided.

I'm thinking something involving ForEach / Procedures etc

Perhaps the need to break out of both loops can be avoided entirely by narrowing
the list of things you are looping through

Hello -

I tried to find a better solution that all these nested for loops but
there is no other way. I know that the Return will also break out of
all loops but I only do that in case something went wrong (Return
Nothing).

Makes sense that Try/Finally should not be used for flow control so I
went ahead and did the "bubble" approach Andrew suggested.

Thanks!
Joe
 
Hello,

The context is a bit unclear.My understanding is that you are trying to use
a try/finally block as a way to control the code path which is IMO a very
bad thing.

If you want to exit on an arbitrary condition my personal preference would
be to use exit for or even just using a do loop rather than for (after all
if you don't want all those occurences to take place this is that going from
an initial value to a final value is not what you wanted to do).

Throwing an exception could be also another way but it is a bit unclear if
lError is just a state flag in your code or notes something that was really
wrong...

--
Patrice

"Joe Duchtel" <[email protected]> a écrit dans le message de groupe de
discussion :
(e-mail address removed)...
 
VB.Net still has a GoTo. Much as I dislike GoTos I think that a GoTo might
be justified here.

Bob

Agreed - GoTo is the devil most of the time, but there are times when it makes
sense. Until we get labled break/continue in C#/VB.NET - there isn't always a
cleaner alternative for nested loops.
 
Agreed - GoTo is the devil most of the time, but there are times when it makes
sense.  Until we get labled break/continue in C#/VB.NET - there isn't always a
cleaner alternative for nested loops.

Hello -

Sorry my example wasn't detailed enough. It is a lot more complicated
in real life and I was trying to simplify it for posting purposes. I
think I got my answer in that I should not use the Try/Finally for
control flow purposes. The "bubble" flag seems to be the most
appropriate solution for my particular situation ...

Yep ... GoTo is probably the worst way of doing this ... I mean I had
to use it on my Commodore 64 but don't know why they are keeping it
around ...

Thanks a lot!
Joe
 
Joe said:
-snip-

Yep ... GoTo is probably the worst way of doing this ... I mean I had
to use it on my Commodore 64 but don't know why they are keeping it
around ...

Actually he's saying that GOTO is the CLEANEST way of doing what you're
describing -- an exit condition that happens inside an inner loop that
doesn't exist the entire procedure.


Now, there are a couple of other alternatives (besides the flag that
Andrew mentioned).

You can change the outer loops from for loops to repeat or while loops,
you can fiddle with out loops control structure (i.e. if the outer most
loop was for i = 1 to x you can change either i to x or x to i).
Neither of these work if you're using a foreach loop. A 3rd
alternative is to extract the loops into a function of their own, where
you can exit the function whereever you want.

I'd go with either the GOTO or turning it into it's own function (with
more than a slight lean towards making it it's own function as that
gives you an isolated bit of code that easy to understand. But a GOTO
isn't bad in the described situation. While Andrew's solution will
certainly work, it is less clear than a single GOTO.
 
Actually he's saying that GOTO is the CLEANEST way of doing what you're
describing -- an exit condition that happens inside an inner loop that
doesn't exist the entire procedure.

Personally, I wish we had the labled breaks and continues that java has. It's
essentially a goto - but, at least it doesn't look like a goto :)

in VB it might look like:

For i As Integer = 0 To 100
For j As Integer = 0 To 100
If j = 10 Then Exit For To AllDone
Next
Next
AllDone:
Now, there are a couple of other alternatives (besides the flag that
Andrew mentioned).

You can change the outer loops from for loops to repeat or while loops,
you can fiddle with out loops control structure (i.e. if the outer most
loop was for i = 1 to x you can change either i to x or x to i).
Neither of these work if you're using a foreach loop. A 3rd
alternative is to extract the loops into a function of their own, where
you can exit the function whereever you want.

I'd go with either the GOTO or turning it into it's own function (with
more than a slight lean towards making it it's own function as that
gives you an isolated bit of code that easy to understand. But a GOTO

Using a function isn't a bad alternative - and it can make things a bit
cleaner. The only real downside is that you MIGHT take a small performance
hit because of the function call. I say might - because it's possilbe that
the JIT will inline the function call anyway :)
 
eBob.com said:
VB.Net still has a GoTo. Much as I dislike GoTos I think that a GoTo
might be justified here.

Bob

I agree with Bob here. If you want to avoid a GoTo in name only, wrap
the outer loop with a once only Do Loop.

Do
For i ...
For j ...
If lError Then Exit Do
For k ...
Next
Next
Next
Loop While False

Definitely better than an exception, or a Try Finally, but a GoTo with a
well named label is the simplest option here.
 
Back
Top