Exit in Select-End Select IN For..Next ?

exjey

Member
Licensed User
Longtime User
I was shooting myself why the subroutine going totally wrong until i figured out that EXIT in Select..End Select and all this inside For..Next loop not working or at least tries to "exit" the Select block which is meaningless. Please forgive me if i am wrong.

For X = 0 To some_var

...blah blah

If some_condition=True Then Exit 'that works and exits For..Next


Select some_other_var
Case 5,7
Do_Some_Stuff (call other sub actually)
Exit ' i was instructing to Exit For..Next but that was wrong causing me headaches

End Select

other_commands

Next

End Sub

So in Select block if the some_other_var equals 5 or 7 does not exit For and continues to other_commands. This is known behavior, am i missing something? Or there is something else in my huge program?

I've replaced Exit with Return cause no need to stay in Sub after the condition in Select block is true, and all returned to normal.
 

mc73

Well-Known Member
Licensed User
Longtime User
As a workaround, till this will be fixed, you can use an exitLoop flag and set it to true when your requirements are met inside the select. Once select is finished, you can check for this flag and exit for.
 
Upvote 0

francoisg

Active Member
Licensed User
Longtime User
Any news??? - version 4.3 still does this! Bit me on the !@#$ a few times now!!!!!!

try the following:

' ** You would expect the following code to log 0-4 and then stop
' ** The bug causes it to log 0-10
for i=0 to 10
select i
case 5
exit​
end select
log(i)​
next​
 
Last edited:
Upvote 0

francoisg

Active Member
Licensed User
Longtime User
Ok, the following works:

Dim done As Boolean = False
For i = 0 To 10
Select i
Case 5
done = True​
End Select
Log(i)
If done Then Exit​
Next
If it is something that you cannot fix, please fix the documentation to state this fact then. This bug caused me hours of misery trying to figure out where my code went wrong!

I see the generated java code uses a "break" statement, which is also used to "break" out of the "switch" statement - hence the problem ...
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Took me a while to find that there is an extra non-visible character in the code you posted which breaks the parser.

I will update the documentation. Note that in the new IDE the Exit inside the Select block will not be highlighted:

SS-2015-05-06_09.52.50.png
 
Upvote 0

francoisg

Active Member
Licensed User
Longtime User
Sorry about the non-visible character in the posted code! Thank you, I think that will help a lot ...
 
Upvote 0
Top