Re: How to use switch with ranges?

new topic     » goto parent     » topic index » view thread      » older message » newer message
DerekParnell said...
irv said...

Suppose I need to write something like:

switch age do 
     case 1..20 then msg = "No Beer!" 
     case 21..30 then msg = "check dress code" 
     case 31..50 then msg = "suggest they try the veal" 
     case 50.. 

You get the idea Is there a range or set or ? function to do this in eu 4.0?

There is a proposal to enhance the switch syntax to cater for some common shorthand sets but it is not likely to make the initial v4 release. However for now you can use the longhand way of doing this, but with the number of cases you are suggesting, it might be slower than a simple 'if' statement ...

I doubt that it would be slower (at least, probably not enough to notice). With integers, the switch is really fast, since it uses a jump table (there's a subtraction operation to adjust for the minimum value, too). The super optimized version will work for integers whose max and min are no more than 1024 apart. An optimized if statement, where most of the cases end up in the first clause may beat this, but again, probably not by a noticeable amount.

If you mix in doubles or sequences, or a wide range of integers, you basically are left with using find() to determine the correct jump offset. When translated, the optimized version uses a native C switch statement, and the non-optimized version uses a combination of find() plus a switch statement.

Matt

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu