1

In a question about switch and goto, this answer used this code to avoid using a goto altogether:

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

How does this code work? I understand that cases automatically fall through, so I don't need it explained when color is YELLOW, but what about when color is GREEN? How does C++ handle else when it never encountered if. Would this code even compile, would it be a runtime error, or would it just ignore the else?

Jarod42
  • 203,559
  • 14
  • 181
  • 302
Johnathan Gross
  • 678
  • 6
  • 19

0 Answers0