I've noticed an "awkward" issue in VB.... how to escape from things like nested loops or nested IFs, where the standard Exit Loop, Exit For, or Exit If simply starts a new iteration.

For example, assume we have a 3-dimensional array (TheArray) that we want to brute-force test to see if it contains a "true" value. The easiest way would probably be this:

For X = 0 To 50
            For Y = 0 To 50
                For Z = 0 To 50
                    If TheArray(X, Y, Z) = True Then GoTo ExitLoops
                Next
            Next
        Next
ExitLoops:
'Do something else here

But I've always been taught to avoid GOTO statements.... With that in mind, I could do this:

Dim Exiting As Boolean = False
        For X = 0 To 50
            For Y = 0 To 50
                For Z = 0 To 50
                    If TheArray(X, Y, Z) = True Then Exiting = True : Exit For
                Next
                If Exiting Then Exit For
            Next
            If Exiting Then Exit For
        Next
        'Do what you will here

but that makes the code longer, harder to read, and its a pain to have to explicitly exit all the loops, especially as the nest gets deeper...

So we could do this:

Do
            For X = 0 To 50
                For Y = 0 To 50
                    For Z = 0 To 50
                        If TheArray(X, Y, Z) = True Then Exit Do
                    Next
                Next
            Next
        Loop While 1 <> 1
        'Do something here

But that also gets confusing, as the Do really does nothing, not to mention it's not made to act like a container.

So shouldn't we have something like a "Section" block? Something with syntax like this:

Section <Name>
        'Statements go here
        End Section

and you can exit the section with

Exit <SectionName>

So, in our example, it then becomes:

Section LoopingPart
        For X = 0 To 50
            For Y = 0 To 50
                For Z = 0 To 50
                    If TheArray(X, Y, Z) = True Then Exit LoopingPart
                Next
            Next
        Next
        End Section
        'Do something here

Finally, a method that uses a proper block, is easy to read, and easy to code!

But as far as I know, nothing of the sort exists in VB. Am I right?

I like your first GoTo example.

I just did your second example not to long ago. When I come across it again I'm going to swap it out with the GoTo method.

I might also like this method

Try
    For i = 0 To 1000
       For j = 0 To 1000
          For k = 0 To 1000
             If 1 = 2 Then : Exit Try : End If
          Next
       Next
    Next

Finally
End Try

Edited 6 Years Ago by Unhnd_Exception: n/a

The Try and Do examples are interchangeable, as well as any other blocks that can be exited, such as a While.

Welp, Pick your poison.

You can treat a Try or Do as a section.

Theres really no good way. I still like the Go To method, although I've never had a go to in my code. But looks like a good situation for it.


Another thing would be to put the for statement in a function and exit function. But again may not be a good way due to performance loss.

Hello , You can simply use the folowing

While (True)

    For --------

        For ----------

           For ---------

               'your statments
               Exit While

           Next

       Next

    Next


 End While
This question has already been answered. Start a new discussion instead.