Exit Select block?
BlitzMax Forums/BlitzMax Programming/Exit Select block?
| ||
| Today i've come to need, but didn't find a way to exit out of a select block explicitly. Looks like some Basic dialects use "Exit Select" for this, while C has its "break". Of course, there's always the workaround of enveloping the Select block with and If block and using Exit, but i'd prefer a cleaner way. |
| ||
| Doesn't 'Exit' work? If not post your code. |
| ||
Are you trying to say Blitzmax will continue checking though all of the Cases?Global b:Int Global a:Int=1 Select a Case 1 b=1 a=2 Case 2 b=2 a=3 Case 3 b=3 a=4 Case 4 b=4 a=5 Case 5 b=5 End Select Print b Looks to me once it finds a "case" it doesn't check anymore so it auto exits. :) |
| ||
a = 3 b = 2 Select a Case 3 Print "before" If b = 2 Exit Print "after" End Select doesn't compile. |
| ||
| From the Docs: Exit causes program flow to exit the enclosing While, Repeat or For loop. What you need is this:
a = 3
b = 2
Select a
Case 3
Print "before"
If b <> 2 then
Print "after"
EndIf
End Select
Or if the code is in a small function you could perhaps say If b=2 then Return which will exit the whole function. |
| ||
| I knew there were workarounds, but seeing how other languages do have a keyword for this, I'd find it appropriate for BMax to have on too. |
| ||
| If you have your Select block in a function, and there's nothing else in the function after the Select block, just stick a 'Return' in. |
| ||
| I said that ;-) |
| ||
Or you could try:Global b:Int Global a:Int=1 Select a Case 1 b=1 aTMP=2 a=999 'Soft Exit-code Case 2 b=2 aTMP=3 a=999 ... ... Case 999 a=aTMP End Select Print b But maybe this is not a clean way? Oh, or this: Global b:Int Global a:Int=1 Local aTMP:Int = a Select aTMP Case 1 b=1 a=2 Case 2 b=2 a=3 ... ... End Select Print b Looks pretty clean to me. |
| ||
a=999 'Soft Exit-code Select in BlitzMax is *not* like switch in C/C++/Java/etc. Once you are in a Case, it won't fall through to any others. Once the end of Case is reached, code drops down to the statement after End Select. |
| ||
| Ah, good. Im used to EasyTrieve (Cobol variant), it falls through there :( |
| ||
Here's an ugly way of doing it. :)a = 3 b = 2 Select a Case 3 Repeat 'Create a loop to exit from Print "before" If b = 2 Exit Print "after" Until 1 'Always exit the loop after one time through End Select |
| ||
| I don't get why you need to exit out of a case block. A case block will end when it reaches the next case, default or end select. i.e
superstrict
local a : int = 2
local b : int
select a
case 1
' blah
case 2
' only the code here
' upto here will be executed
' even having the following line
a = 1
b = 2
' will not stay in the case structure as it does not loop
'
case 3
' blah
end select
' code will resume here
print b
It is not like a loop where you may need to 'exit' out prematurely and I'm sure blitzmax will use short circuit evaluation so only upto a match does it evaluate the testing expression. So you don't need to exit from a case at all. Cheers. |
| ||
| I don't get why you need to exit out of a case block. If you want to exit a Case before the end of it. Suppose you have a character that can walk left, walk right, climb up a ladder, or slide down a chute. While he is walking, he could be on a platform or a treadmill. Do a select for which type of movement he is doing. If he is walking, then move the character left or right, unless he's on a treadmill, then any movement will cause him to stand still. psudocode:
Select State
Case WalkingLeft
If OnTreadmill 'character on treadmill?
If TreadmillLeft
CharacterX :- 1 'Move the character faster if same direction as treadmill
Else
Exit 'exit from this case with no movement
End If
End If
CharacterX :- 1
Case WalkingRight
If OnTreadmill 'Character on treadmill?
If TreadmillRight
CharacterX :+ 1 'Move the character faster if same direction
else
Exit 'exit from this case with no movement
End If
End If
Character :+ 1
Case Climbing
CharacterY :- 1
Case Sliding
CharacterY :+ 1
Case Standing
End Select
I know there are other ways to code that without needing an Exit, but sometimes it is just simpler to use. |
| ||
| I know there are other ways to code that without needing an Exit, but sometimes it is just simpler to use. Yes, in your example not to have the else statement ;) |
| ||
| I dunno about all this.. I've the impression that if you need such a construction your code structure is bad. It's a bit like going to a shoe factory and require shoes for your hands, only because you think you should walk on your hands instead of your feet.. :P TomToad's example is quite a bit oldskool.. ^_^ |