Page 1 of 1

Extended Select

Posted: Fri Oct 16, 2020 7:45 pm
by Jac de Lad
Hi,
would it be possible and wanted to enhance Select a bit? Coming from XProfan I can use

Code: Select all

Select MyVar
Case 5,<0
Debug "MyVar is 5 or below 0"
Case >10
Debug "MyVar is bigger than 10"
Default
Debug "MyVar is something else"
EndSelect
to better use Select.

Re: Extended Select

Posted: Sat Oct 17, 2020 2:11 am
by BarryG
If you have a known min/max range, then you can do it with hard-coded values like below. Not exactly the formatting you want, but it works.

I take it you have some reason that you don't want to use If/ElseIf/EndIf to do what you're asking?

Code: Select all

Select MyVar
  Case 5, -999999 To -1
    Debug "MyVar is 5 or below 0"
  Case 11 To 999999
    Debug "MyVar is bigger than 10"
  Default
    Debug "MyVar is something else"
EndSelect

Re: Extended Select

Posted: Sat Oct 17, 2020 2:17 am
by Jac de Lad
Hi BarryG,
yeah that would work, if I know both boundaries. On the other hand I could set second value ridiculously high/low, this would work for most of the time too.
I want to avoid if, because I have some statements where I react to many, many values of a variable. Font know if if would fe faster though...

Re: Extended Select

Posted: Sat Oct 17, 2020 2:31 am
by BarryG
Jac de Lad wrote:Dont know if if would be faster though
Apparently it's the same speed when you check the compiled source -> viewtopic.php?p=28350#p28350

But Case has one additional assembler command ("PUSH [v_a]"), so technically Case is slower than If/ElseIf/EndIf.

Re: Extended Select

Posted: Sat Oct 17, 2020 2:42 am
by Jac de Lad
Ah, good to know. Thanks.

Re: Extended Select

Posted: Sat Oct 17, 2020 5:24 pm
by infratec
In such a case I use if

Code: Select all

If MyVar = 5 Or MyVar < 0
  Debug "MyVar is 5 or below 0"
ElseIf MyVar > 10
  Debug "MyVar is bigger than 10"
Else
  Debug "MyVar is something else"
EndIf

Re: Extended Select

Posted: Sat Oct 17, 2020 5:51 pm
by Jac de Lad
Yeah of course. I just wanted to make it a bit easier with Select.

Re: Extended Select

Posted: Sat Oct 17, 2020 5:58 pm
by mk-soft
Select is extended... :wink:

Code: Select all

For a = 0 To 10
  Select a
    Case 1, 3 To 7, 9
      Debug "Case 1, 3 To 7, 9 / a = " + a 
    Case 2, 8
      Debug "Case 2, 8 / a = " + a
    Default
      Debug "Default / a = " + a
  EndSelect
Next