+ 3
Can (a<b)&&(b<c) be written as a<b<c.
6 Réponses
+ 11
The latter will result in syntax error, even though the logic is correct. Operator '<' isn't overloaded to evaluate stuff like that.
+ 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;
}
+ 3
thanks
+ 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...
+ 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...
+ 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.