Re: Falling through on switch-cases

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




"Jon Skeet [C# MVP]" <skeet@xxxxxxxxx> wrote in message news:MPG.224b45d96c3561ebb8e@xxxxxxxxxxxxxxxxxxxxxxx
Ben Voigt [C++ MVP] <rbv@xxxxxxxxxxxxx> wrote:
Jon Skeet [C# MVP] wrote:
> Rudy Velthuis <newsgroups@xxxxxxxxxxxx> wrote:
>> Jon Skeet [C# MVP] wrote:
>>
>>> Ranges are a possibility, so long as you can always check that a
>>> value falls in only one range.
>>
>> The compiler should simply generate a compile error if the ranges
>> overlap, like it does in Pascal. Then, only one range can apply. If
>> you'd allow overlapping ranges, the first match could win.
>
> Yes - as I say, it's easy enough to tell for the built-in numeric
> types. There may be other types where it's not so obvious though.

switch is only allowed on built in integral types and string anyway

Well yes, at the moment - but if we're considering possible changes, we
should consider the possibility of a more flexible switch.

It really goes back to what the point of switch is. If it's to use a
lookup to avoid a series of if/else statements for performance reasons,
I'm not sure its place is really deserved in C#.

If its purpose is to find one matching piece of code from a number of
options, there are ways it could be significantly more flexible.

--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


As a newbie/amateur, I was disappointed with both case statements and enums in C#; both are weaker than what I expected. (Almost everything else was way better than I expected!).

As I understand it, I have to write

..
..
..
thisday = (int) dayofweek.Sunday
..
..
..

switch (thisday);
{case (int) dayofweek.Sunday: dostuff();break;
case (int) dayofweek.Monday: dostuff(); break;
..
..}


What I was hoping for was:

thisday=Sunday;

switch (thisday);
{case Sunday: dostuff(); break;
case Monday: dostuff(); break;}

Or even:

switch(thisday);
{case Sunday: dostuff(); break;
case Monday..Friday: dostuff(); break;
case Saturday: dostuff(); break;}

The continual need to cast back to integers reduces the readability of the code.

Also, as someone else pointed out, the ++ operator for enums should loop. If you have a statement:

thisday++;

and thisday was (int)dayofweek.Saturday, it returns what in reality is an out-of-range enum (as there is no day of the week after Saturday).

What I think would have been more useful is thisday++ being equivalent to

thisday++; if (thisday>(int)dayofweek.Saturday) thisday=(int) dayofweek.Sunday;

or even better

thisday++; if (thisday>Saturday) thisday=Sunday;

Its probably too late to change this and keep backwards compatibility, so I think C# is stuck with ++ working for enums as it does for integers.

As somebody else pointed out, Pascal (now 30 years old) had user defined types which work a little like enums, but in a more readable, maintainable and intuitive form.

.



Relevant Pages

  • Re: Why doesnt "switch" statement have "break" as default?
    ... The 'C' switch statement is in no way syntactic sugar for a cascade of if-else ... I do realise that the compiler and/or JIT is allowed to implement any ... As to the proposal to add lists and ranges, a couple of comments, not entirely ... and those limits would translate into apparently arbitrary (and very ...
    (comp.lang.java.programmer)
  • PLL ratios (was Re: Dual-frequency quartz oscillator with a FPGA ?)
    ... Now if you have a smart idea about how to switch between them ... I'll have to divide this by 149 or 160 to obtain the right frequency. ... The Actel PLL has only 7-bit ratios, ... and the PLL's VCO ranges from 24 to 350MHz:-/ ...
    (comp.arch.fpga)
  • Re: [PATCH] ASoC: Add new TI TLV320AIC3204 CODEC driver
    ... I can't help but think that this'd be more legible with switch ... statement is going to be much clearer. ... I define my ranges so that I start from the very ... A number of pages have a similar register pattern, ...
    (Linux-Kernel)
  • Re: Auto populate field with 100 different number range
    ... of the underlying business purpose or need can help clarify what needs to ... ranges"), but left out the WHAT or WHY. ... possible ranges under this switch. ... But this TCIC is divided into ranges of 24 (24 TCICs ...
    (microsoft.public.access.forms)
  • Re: Switch for floating !!
    ... int main ... Is it so difficult for C library to support the floating point ... Switch is designed to work with integer values only. ... ranges, and not distinct values. ...
    (comp.lang.c)