Is it possible to use goto with switch?

后端 未结 2 357
孤街浪徒
孤街浪徒 2020-12-29 02:31

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 ba

相关标签:
2条回答
  • 2020-12-29 03:02

    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

    0 讨论(0)
  • 2020-12-29 03:21

    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;
    }
    
    0 讨论(0)
提交回复
热议问题