+ 3

Can (a<b)&&(b<c) be written as a<b<c.

15th Aug 2017, 5:45 AM
Rohan Singh Aswal
Rohan Singh Aswal - avatar
6 ответов
+ 11
The latter will result in syntax error, even though the logic is correct. Operator '<' isn't overloaded to evaluate stuff like that.
15th Aug 2017, 5:58 AM
Hatsy Rei
Hatsy Rei - avatar
+ 6
...I think it works if b and c are Booleans #include <iostream> using namespace std; int main() { int a = 15; bool b = 1; bool c = 0; if (a>b>c){ cout<<a<<" "<<b<<" "<<c<<" is possible"; } else{ cout<<"Not possible"; } return 0; }
15th Aug 2017, 7:31 AM
Nomeh Uchenna Gabriel
Nomeh Uchenna Gabriel - avatar
+ 3
thanks
15th Aug 2017, 6:02 AM
Rohan Singh Aswal
Rohan Singh Aswal - avatar
+ 1
The problem is that types cannot be compared... You cannot compare int with bool... In the following operation : int a,b,c; if(a>b>c){...} (a>b)>c or a>(b>c) translates to a comparision between an int and a bool variable... This becomes invalid to perform, as the new bool data type cannot be implicitly casted to int for this to work. Even if this was done, it would be a 1 or 0, and the number may be smaller than the other operands but greater than 1 or 0...
15th Aug 2017, 3:49 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
The problem is that types cannot be compared... You cannot compare int with bool... In the following operation : int a,b,c; if(a>b>c){...} (a>b)>c or a>(b>c) translates to a comparision between an int and a bool variable... This becomes invalid to perform, as the new bool data type cannot be implicitly casted to int for this to work. Even if this was done, it would be a 1 or 0, and the number may be smaller than the other operands but greater than 1 or 0...
15th Aug 2017, 3:49 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
It works (to be sure, tested with diff. Compilers), but it most probably does not what you intended. 1st runs correctly, but 2nd compares a<b (left associativity) with result of a bool value (true or false). Bool value is casted to an integer value (0 or 1) and then compared with c.
9th Mar 2018, 11:09 PM
K Locher
K Locher - avatar