Quote:
|
Originally Posted by arizona99
Code:
int a = 0;
if ( 5 < a < 10 )
std::cout << "Yep";
It would seem like C++ should evaluate the condition as 5 < a AND a < 10. Why doesn't it work that way? And is there a term for this situation?
-thanks
|
Specifically, you have to remember that the statements are evaluated individually. Any compound boolean statement has to be broken down into individual statements, which are then evaluated and chained together as appropriate. The statement 5 < a < 10 is the same as ( 5 < a ) < 10.
In this case, 5 < a < 10, the first thing to be evaluated is '5 < a'
This is false. False values are evaluated as 0, while true ones (from boolean operators, anyway), are evaluated as 1. So we swap out '5 < a' for '0'.
This leaves us with '0 < 10', which is true, so the code executes. While tekno's right on about how it should be implemented, it's actually not improper syntax, per se. It's just not syntax that's doing what you want.