Case, Select

Started by Baggey, October 24, 2021, 20:41:44

Previous topic - Next topic

Baggey

In a Select case senario. Im trying to use a Case 16 To 23

I want to use the To definition, but it dosent allow. for Example

Code (Blitzmax) Select

Case 16 to 23 ' NOT ALLOWED!

' Rather than

Case 16,17,18,19,20,21,22,23 ' LONG WINDED!


Is there a short cut? If not why not?  :-\

Maybe a future implementation to Blitzmax NG :-X

Kind Regards Baggey
Running a PC that just Aint fast enough!? i7 Quad core 16GB ram 1TB SSD and NVIDIA Quadro K620 . DID Technology stop! Or have we been assimulated!

ZX Spectrum 48k, C64, ORIC Atmos 48K, Enterprise 128K, The SID chip. Im Misunderstood!

Derron

There is no shortcut  ... not really but you could create interesting select case stuff:

Code (Blitzmax) Select

SuperStrict
Framework Brl.StandardIO

Local x:Int = 10
Select True
Case x >= 10 And x <= 20
Print "10 <= x <= 20"
Case x = 21
Print "21"
Default
Print x
End Select


dunno if that is slower or faster than if-then-else... doubt it


bye
Ron

Pebender

Hi,

can you take it into a Loop??

maybe:

for i=16 to 23

case(i)

next i

i´m not sure..

TomToad

There are two forms of Select/Case.  First form is
Select expression
        Case constant list
                code
        Case constant list
                code
        Default
                code
End Select

Where expression must evaluate to a single value, and then matched to each Case.  Constant list is one or more constants, separated by a comma, which the expression is matched against.  Default is what gets executed when no Case matches.
The second form is
Select True
        Case expression
                code
        Case expression
                code
        Default
                code
End Select

In this form, each expression needs to evaluate to True or False.  The Case are evaluated one by one until one is True.  Default is executed when all Case statements evaluate to False.

Note that only the first matching Case statement is executed in both forms, all others are skipped whether they match or not.

So to answer your question, to use form one, you would use
Select x
    Case 16,17,18,19,20,21,22,23
        DoCode()
End Select


for form two, you would use
Select True
    Case x >= 16 and x <= 23
        DoCode()
End Select


I'd say the second form is better.
------------------------------------------------
8 rabbits equals 1 rabbyte.