October 25, 2021, 17:20:04

Author Topic: Case, Select  (Read 111 times)

Online Baggey

  • Full Member
  • ***
  • Posts: 197
Case, Select
« on: October 24, 2021, 20:41:44 »
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
  1. Case 16 to 23 ' NOT ALLOWED!
  2.  
  3. ' Rather than
  4.  
  5. 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
Currently Running a PC that just Aint fast enough!?
ZX Spectrum 48k, NEXT, C64, ORIC Atmos 48K, Enterprise 128K, The SID chip.

Jesus was only famous because of his DAD.    Resistance Is Futile! The code will be assimulated!

Offline Derron

  • Hero Member
  • *****
  • Posts: 3674
Re: Case, Select
« Reply #1 on: October 24, 2021, 21:07:18 »
There is no shortcut  ... not really but you could create interesting select case stuff:

Code: BlitzMax
  1. SuperStrict
  2. Framework Brl.StandardIO
  3.  
  4. Local x:Int = 10
  5. Select True
  6.         Case x >= 10 And x <= 20
  7.                 Print "10 <= x <= 20"
  8.         Case x = 21
  9.                 Print "21"
  10.         Default
  11.                 Print x
  12. End Select
  13.  

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


bye
Ron

Offline Pebender

  • Jr. Member
  • **
  • Posts: 9
Re: Case, Select
« Reply #2 on: October 24, 2021, 21:09:59 »
Hi,

can you take it into a Loop??

maybe:

for i=16 to 23

case(i)

next i

i´m not sure..

Offline TomToad

  • Hero Member
  • *****
  • Posts: 557
Re: Case, Select
« Reply #3 on: Today at 10:24:14 »
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
Code: [Select]
Select x
    Case 16,17,18,19,20,21,22,23
        DoCode()
End Select

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

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

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal