36

It seems it's possible with C#, but I need that with C++ and preferably cross platform.

Basically, I have a switch that sorts stuff on single criteria, and falls back to default processing on everything else.

Say:

switch(color)
{
case GREEN:
case RED:
case BLUE:
    Paint();
    break;
case YELLOW:
    if(AlsoHasCriteriaX)
        Paint();
    else
        goto default;
    break;
default:
    Print("Ugly color, no paint.")
    break;
}
Sled
  • 18,541
  • 27
  • 119
  • 168
Coder
  • 3,695
  • 7
  • 27
  • 42
  • 1
    Have you tried it? I think it might be possible... – Merlyn Morgan-Graham Nov 20 '11 at 14:37
  • Why not use standard programming practice and have the `else` and the `default` call a function (inline or otherwise) as they are intended to perform the same action. – Jim H. Nov 20 '11 at 14:42
  • 2
    @MerlynMorgan-Graham you cannot use goto default; default is a keyword and cannot be used as a label. the fact that C# is doing it is screwy :-) hehe ... – Ahmed Masud Nov 20 '11 at 14:48
  • It would actually make as much sense to have default as just another label that is the default if non match. That is what the assembly does. If the value isn't in the jump table it will jump to the default location. So I would not say that allowing one to jump to default: is screwy. The screwy part is that some languages treat switch like an if/else-if with booleans. – Beached Nov 20 '11 at 17:18
  • Beached 1) i was talking about parse phase of C; C keywords are NOT allowed to be used as labels; 2) you are presuming that the generated assembly WILL use a label/jump ... What if the target CPU uses some completely different branching mechanism? – Ahmed Masud Nov 26 '11 at 08:03

2 Answers2

47

Ahmed's answer is good, but there's also:

switch(color)
case YELLOW:
    if(AlsoHasCriteriaX)
case GREEN:
case RED:
case BLUE:
        Paint();
    else
default:
        Print("Ugly color, no paint.");

people tend to forget how powerful switches are

Steve Cox
  • 1,947
  • 13
  • 13
  • 20
    @Matt "Many people (even bwk?) have said that the worst feature of C is that switches don't break automatically before each case label. This code forms some sort of argument in that debate, but I'm not sure whether it's for or against." – Steve Cox Jul 18 '14 at 19:32
  • 6
    It's not about the lack of breaking, it's about putting case labels in between the components of an if-block. – Matt Jul 25 '14 at 14:13
  • 2
    I'm wondering, if `color` is `BLUE` and `AlsoHasCriteriaX` is `false`, would that call both `Paint()` and `Print()`? Or: how is the `else` statement evaluated if you jump right into the `if` body? – Torben L. Dec 15 '14 at 15:25
  • 1
    Automatic breaks in C would introduce a level of complexity that's not desired in C. C is supposed to be very very close to the machine. If you put automatic breaks then you introduce implicit local jumps in the code which is something that C avoids, everything in C strives to explicitlybe mapped to Assemblers. – Ahmed Masud May 30 '15 at 16:23
  • @AhmedMasud well brian kernighan, and tom duff think it's a lot less black and white. – Steve Cox Jun 01 '15 at 14:03
  • 3
    The real problem with this code is when you try to change/add something. – Piotr Siupa Jan 21 '18 at 15:37
  • 5
    Forget all the criticism. This snippet is awesome and real programmers must be able to cope with this kind of things. Thumbs (and vote) up! – linuxfan says Reinstate Monica Jun 18 '18 at 14:22
  • 1
    points for creativity, but I hope to not encounter this outside an academic exercise :} – namezero Dec 04 '19 at 12:25
  • @LShadow77 if you swap if for a while you basically have a duff's device – Steve Cox Mar 30 '21 at 19:19
38

Not quite but you can do this:

switch(color)
{
case GREEN:
case RED:
case BLUE:
     Paint();
     break;
case YELLOW:
     if(AlsoHasCriteriaX) {
         Paint();
         break; /* notice break here */
     }
default:
     Print("Ugly color, no paint.")
     break;
}

OR you could do this:

switch(color)
{
case GREEN:
case RED:
case BLUE:
     Paint();
     break;
case YELLOW:
     if(AlsoHasCriteriaX) {
         Paint();
         break; /* notice break here */
     }
     goto explicit_label;

case FUCHSIA:
     PokeEyesOut();
     break;

default:
explicit_label:
     Print("Ugly color, no paint.")
     break;
}
Ahmed Masud
  • 21,655
  • 3
  • 33
  • 58