Select the case

Blitz3D Forums/Blitz3D Beginners Area/Select the case

Tibit(Posted 2004) [#1]
Why doesn't this work:

Select Map%

Case < 0

Case > 5 and < 10

End Select

I guess there is som "behind the scenes" logic that I don't get with select..

When on the subject, what can one do with select really?
This is the 2 I heard about:
Case 1,3,5,2,2 ;pick several
Case 1 To 5 ;Get intervall

There is probably more secrets hiding in select~

Anyone who knows?
//Wave~


SoggyP(Posted 2004) [#2]
Hi Folks,

I think you need to write instead

Select True
  case Map<0
  case Map>5 and Map<10
end select


I might be wrong, it wouldn't be the first time ;0)
Later,

Jes


JazzieB(Posted 2004) [#3]
As SoggyP says, use Select True and then have each Case as you would an If statement.

You can't use To with Case, but you can have the 'pick several' option that you mentioned.


Mustang(Posted 2004) [#4]
Manual is your friend:

http://www.blitzbasic.co.nz/b3ddocs/command.php?name=Case&ref=2d_cat

; Advanced SELECT/CASE/DEFAULT/END SELECT Example
; Assign a random number 1-10
mission=Rnd(1,10)

; Start the selection process based on the value of 'mission' variable
Select True

; Is mission = 1?
Case mission=1
Print "Your mission is to get the plutonium and get out alive!"

; Is mission = 2?
Case mission=2
Print "Your mission is to destroy all enemies!"

; Is mission = 3 to 5
Case mission>=3 And mission<=5
Print "Your mission is to steal the enemy building plans!"

; What do do if none of the cases match the value of mission
Default 
Print "Missions 6-10 are not available yet!"

; End the selection process
End Select 



tonyg(Posted 2004) [#5]
When would you use...
Select True
and when...
Select <variable>
??
The manual entry for Select gives the second example and
for Case the first example.
Thanks


Tibit(Posted 2004) [#6]
I guess select isn't that useful after all. I'll rather use "if" instead of "select true". Anyway thanks for clearing things up,
//Wave~


_PJ_(Posted 2004) [#7]
I was curious as to why use Select when If will do (Cant find the thread now), and was enlightened that it can be quicker than sifting through a whole load of If's when the actual valid case has already been determined.


tonyg(Posted 2004) [#8]
I remember a thread in BlitzCoder suggesting that Select is quicker at Machine Code level as it only loaded the value into a register once while IF did it each test. However, this was right after a thread which stated BB converts Select to IF/ELSE statements at compile time so the only difference is 'readability'.
I might have also found an answer to me own question earlier that 'Select True' takes the first true case statement. Not sure I'd really want to use it that way.
How does it know which variables you want to Select against
(or am I being dense?)


_PJ_(Posted 2004) [#9]

I remember a thread in BlitzCoder suggesting that Select is quicker at Machine Code level as it only loaded the value into a register once while IF did it each test. However, this was right after a thread which stated BB converts Select to IF/ELSE statements at compile time so the only difference is 'readability'.

hmmm ... never knew that.
I wish I could find my previous thread because there was some timescale testing and comparisons between IF/THEN and Select done by some people.


I might have also found an answer to me own question earlier that 'Select True' takes the first true case statement. Not sure I'd really want to use it that way.
How does it know which variables you want to Select against


That's what the variable argument in the Case statement. is for...


tonyg(Posted 2004) [#10]
Hi Malice,
Yep, it was me being dense.
Obviously with Select <variable> you can say Case 1 while,
with Select True you would say Case <variable> = 1
Cheers


_PJ_(Posted 2004) [#11]
This has got me thinking now along the lines of what can be used in the Case argument.

In which order are 'And/Or' statements iterated...

i.e.
Case Apple=1 And Banana=1 Or Fruit>2 And Veg<10 Or CanBeBothered>0


is this sort of thing valid? Unless I had a huge list of Cases, it probably would be better to use If's for such complicated choices, but this was just a thought.

------------------------------------------

EDIT!!!

Found it!!!


JazzieB(Posted 2004) [#12]
Basically, if you're only comparing one variable use...

Select <variable>
  Case blah
    ...
  Case blah 2
    ...
  Default
    ...
End Select


When you need to compare ranges of a variable or multiple variables use...

If <conidtion(s)> Then
  ...
ElseIf <condition(s) 2> Then
  ...
Else
  ...  ; <<< if nothing above is met
EndIf


Basically the If..Then..Else approach is what the Select True construct is converted to into machine code. You could still use Select True, but it's down to personal preference.

The argument about the first, normal, use of Select is that the value of the variable being compared need only be loaded into a register once (in machine code). The compare statements then only need to load the values that this first one is being compared to. Make sense? Basically, the theory is that it made the Select statement just that little bit faster then the If..Then version. I remember the thread, but can't remember the outcome.

Basically just use whichever you find more readable, as the speed gain, if any, is probably so small it doesn't matter.


Ice9(Posted 2004) [#13]
Well if you have 30 comparisons to make to find the
one that is true and you leave out one Endif or you
need to add more comparisons then its easier to use
a select true. Also with a Select true you can order
the selects so the most likely to be true is near the
top. Basicly its easier to read and cleaner to code
a Select True than an If ElseIf


JazzieB(Posted 2004) [#14]
I don't see how this differs from If..ElseIf..Else..EndIf! You can add extra comparisons easily and you can order them. In fact, you're supposed to order your If's in the order of likelihood anyway, for the sake of speed.

As for readability, maybe you have a point, but it's not that significant in my opinion.

If a>1 and a<10 Then
    ...
Else If a>9 Then
    ...
Else
    ...
EndIf

versus

Select True
    Case a>1 And a<10
        ....
    Case a>9
        ....
    Default
        ....
End Select

Each to their own, I guess.