¿Es posible usar goto con switch?


Parece que es posible con C#, pero lo necesito con C++ y preferiblemente multiplataforma.

Básicamente, tengo un interruptor que ordena las cosas en un solo criterio, y vuelve al procesamiento predeterminado en todo lo demás.

Di:

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;
}
Author: ArtB, 2011-11-20

2 answers

No del todo, pero puedes hacer esto:

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

O podrías hacer esto:

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;
}
 24
Author: Ahmed Masud,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-11-20 14:35:28

La respuesta de Ahmed es buena, pero también hay:

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

La gente tiende a olvidar lo poderosos que son los interruptores

 27
Author: Steve Cox,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-06-09 14:00:56