mercredi 31 janvier 2018

How do if/else work intermixed with switch/case interact?

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?

Aucun commentaire:

Enregistrer un commentaire