Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to do an OR in a case statement?

I want to do something like:

case someenumvalue || someotherenumvalue:
    // do some stuff
    break;

Is this legal in C?

The variable that I am doing a switch on is an enumerated list data struct.

like image 758
Falcata Avatar asked Dec 02 '22 21:12

Falcata


1 Answers

You can rely on the fact that case statements will fall-through without a break:

case SOME_ENUM_VALUE:  // fall-through
case SOME_OTHER_ENUM_VALUE:
    doSomeStuff();
    break;

You can also use this in a more complicated case, where both values require shared work, but one (or more) of them additionally requires specific work:

case SOME_ENUM_VALUE:
    doSpecificStuff();
    // fall-through to shared code
case SOME_OTHER_ENUM_VALUE:
    doStuffForBothValues();
    break;
like image 145
pb2q Avatar answered Dec 12 '22 15:12

pb2q