+ 2
when I wrote int x; cin>>x; if(10<x>15) it is always false 10<x<15 always true 10>x<15 always true 10>x>15 always false 3<x<0 always false 3>x>0 true for x less than 3 can anyone explain why this is happening????
3 Respuestas
+ 2
Look at the first expression, for example, when you write 10<x>15, first, 10<x is evaluated and a boolean variable is returned, which is either true or false (depends on x). this boolean is then casted to an integer, which results in 0 or 1, and tested whether is greater than 15, which is always false.
see the answer above for testing if a variable is in some range.
+ 1
Yeah that's not how '<' and '>' work! If you want to check whether x is both larger than 10 and smaller than 15, you need to use
if(x > 10 && x < 15)
or
if(x > 10){
if(x < 15){
...
instead.
0
SAGY Zino you are right