Case with a range
BlitzMax Forums/BlitzMax Beginners Area/Case with a range
| ||
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 |
| ||
Should do the trick! |
| ||
Oh how i wish for a ".." range specifier. too bad its already taken :( |
| ||
It could be reused, as the two contexts of use are different. |
| ||
Hehe, then maybe mark could fix the comment trouble with extending lines while he was at it. |
| ||
if a < 20 elseif a < 80 else endif Much cleaner than aboves attempt of a select ... |
| ||
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. |
| ||
I thought that Blitz just converted the select..case into the equivalent if..then sequence at compile anyway? |
| ||
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. |
| ||
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. |
| ||
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. |
| ||
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" |
| ||
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. |
| ||
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. |
| ||
The following is a C program fragmentswitch (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. |