Why exactly can't we use relational expressions in a switch statement?

716 views Asked by At

Since, there is no true false boolean type in C, what does the expression x == y evaluate to, when it's true and when it's false?

If it evaluates to 1 and 0 respectively, which are integers, why can't we use such expressions as cases for a switch statement?

Is the correct reason behind this:

  1. case allows only integer expressions or expressions that evaluate to integers and x == y wouldn't evaluate to an integer (which I don't see how)? or
  2. if switch allowed such expressions as cases there is a good chance that mutiple cases will end up having the same value, i.e., whenever x==y, we'll get a 1, so multiple such cases will evaluate to 1, or 0; which can't be allowed in switch statements.
1

There are 1 answers

3
MSalters On BEST ANSWER

The reason is not the type of x==y, but the fact that case takes a constant. x==y is usually not a constant.