Bug? Multiple Exits don't seem to work

Jim Brown

Active Member
Licensed User
Longtime User
I have a case where I need to break out of several loops. I thought calling Exit more than once would work but it seems not.

Is it possible to break out of multiple loops?
If not, would you consider something like Exit n (where n is the number of loops to break out of)?

B4X:
Sub Process_Globals
    Dim a,b As Int
End Sub

Sub AppStart (Args() As String)
    Do While a<5
        Log("inner loop 1")
        Do While b<5
            Log("inner loop 2")
            b=b+1
            If b=3 Then
                Log("I want to exit BOTH loops")
                Exit : Exit
            End If
        Loop
        a=a+1
    Loop
    Log("end")
End Sub
 

stevel05

Expert
Licensed User
Longtime User
Processing of the inner loop will stop at the first Exit, so the second will never be called anyway. You'll need to perform the test again in the higher loops and call exit from there.

B4X:
Sub AppStart (Args() As String)
    Do While a<5
        Log("inner loop 1")
        Do While b<5
            Log("inner loop 2")
            b=b+1
            If b=3 Then
                Log("I want to exit BOTH loops")
                Exit
            End If
        Loop
        If b=3 Then Exit
        a=a+1
    Loop
    Log("end")
End Sub
 
Last edited:

Jim Brown

Active Member
Licensed User
Longtime User
EDIT

Thanks Steve.

Erel, Is it possible to add a parameter to determine the number of loops to exit from?

B4X:
Exit 3 ' exit 3 of the inner loops in total
 
Last edited:
Top