Is it possible to use goto with switch?

Coder picture Coder · Nov 20, 2011 · Viewed 25.7k times · Source

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;
}

Answer

Steve Cox picture Steve Cox · Nov 21, 2013

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