You can use the fall-through property of switches:
Code:
switch (x) {
case 0:
case 1:
cout << "X is either 0 or 1" << endl;
break;
From there on, since 1 has already been handled, in the next hypothetical case "1 or 2" the value would never be 1 - it can only be 2.
However, there are certainly possibilities to write the above program without a switch or an if-else chain (keeping the messages somewhat broken as in the original code).
Code:
int main()
{
std::string messages[] = {"X is either 0 or 1" , "X is either 0 or 1" , "X is either 1 or 2", "X is either 2 or 3"};
int x = 1;
//a range check should go here: the default case
std::cout << messages[x] << std::endl;
}
Polymorphism is another means of replacing switches.