Case with a range

BlitzMax Forums/BlitzMax Beginners Area/Case with a range

LeeFbx(Posted 2008) [#1]
Is there some way to select a range of values with "Case"
like Select
case 20 through 80

I know I could use 20,21,22,.....80
but thats a lot of commas.

Thanks,
-Lee


AdsAdamJ(Posted 2008) [#2]


Should do the trick!


grable(Posted 2008) [#3]
Oh how i wish for a ".." range specifier. too bad its already taken :(


Czar Flavius(Posted 2008) [#4]
It could be reused, as the two contexts of use are different.


grable(Posted 2008) [#5]
Hehe, then maybe mark could fix the comment trouble with extending lines while he was at it.


Dreamora(Posted 2008) [#6]
if a < 20

elseif a < 80

else

endif


Much cleaner than aboves attempt of a select ...


Ked(Posted 2008) [#7]
Much cleaner than aboves attempt of a select ...

But If...Then...EndIf is slower than Select...Case...(Default)...EndSelect and he is wanting to use the Select Method.


Czar Flavius(Posted 2008) [#8]
I thought that Blitz just converted the select..case into the equivalent if..then sequence at compile anyway?


Gabriel(Posted 2008) [#9]
But If...Then...EndIf is slower than Select...Case...(Default)...EndSelect

What's your source for this statement? I would have supposed the same as Czar Flavius.


Czar Flavius(Posted 2008) [#10]
To be fair in other languages such as C, select..case (well it's called switch..case) compiles to different code than an if statement. I guess he assumed the same for Blitz.


Ked(Posted 2008) [#11]
I thought that Blitz just converted the select..case into the equivalent if..then sequence at compile anyway?

Oh. Well then nevermind. I could have sworn I heard somewhere on here that If...EndIf was slower than Select...EndSelect. I guess I'm wrong.


Dreamora(Posted 2008) [#12]
it at least does not generate a jump table for the case selection.
And due to above features I would guess it definitely goes down to IFs as "range" checks can definitely not be shortened out into a "single jump"


Emmett(Posted 2008) [#13]
Dreamora stated:
it at least does not generate a jump table for the case selection.
And due to above features I would guess it definitely goes down to IFs as "range" checks can definitely not be shortened out into a "single jump"

Would someone mind expanding on this information. I would really like to know more about "jump table" and "single jump" - what are you talking about.
So, is either (Select - End Select) or (If - End If) better or simply a matter of programmers likes.


grable(Posted 2008) [#14]
Would someone mind expanding on this information. I would really like to know more about "jump table" and "single jump" - what are you talking about.


A jump table is a table of JMP (or goto if you will) instructions, going to the various parts of a Select statement.
So instead of comparing each Case with the current value, you can use indexing to find the correct spot.

So, is either (Select - End Select) or (If - End If) better or simply a matter of programmers likes.

Yes, and no ;) it all depends on what assembler the compiler generates, but in this case it seems BlitzMax doesnt care.
They have different purposes in my view, feks large if-then-elseif blocks can be confusing to read, a select-case would probably fit better.


Czar Flavius(Posted 2008) [#15]
The following is a C program fragment

switch (number)
{
 case 1: dosomething; break;
 case 2: dosomething; break;
 case 3: dosomething; break;
 case ......
 default: whatever;
}


Switch is basically select renamed, and a different syntax because it's a different language of course.

The counterpart in Blitz is
Select number
Case 1 something
Case 2 something
Case 3 something
Case ....
Default somethingelse
End Select


In Blitz the select..case is convered into the equivalent if..else chain. Ie
If number = 1 Then something ElseIf number = 2 Then something ... Else somethingelse EndIf

So the difference is purely "cosmetic"

There are ifs and elses in C of course, but the switch..case compiles into DIFFERENT code than the equivalent if..else would do.

In an if-chain (if..elses in C, either in Blitz) the check has to start at the first check, then the second, then the third... all checks up to the one that is true are made. In a true switch..case the computer jumps *straight* to the number required, so the number of cases is irrelevant. But there are a few limitations. If I remember correctly, a switch..case can only check constant numbers, not variables or "complex" checks that an if can.

This means that cases which occur lower down in a select block will be very slightly slower to access in Blitz, but on any kind of modern computer the time is negligiable, unless you're making some kind of super number-cruncher program, so don't worry too much about it.