If you have a switch statement and want certain code to be run when the value is one value or another how do you do it? The following code always goes to the default case.
#include <iostream>
using namespace std;
int main()
{
int x = 5;
switch(x)
{
case 5 || 2:
cout << "here I am" << endl;
break;
default:
cout << "no go" << endl;
}
return 0;
}
Like this:
Known as “falling through”.
Just to point out that the reason the
defaultcase is executed in the posted code is that the result of5 || 2is1(true). If you setxto1in the posted code the5 || 2case would be executed (see http://ideone.com/zOI8Z).