+ 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????

13th Oct 2016, 1:44 PM
Soutik
Soutik - avatar
3 Antworten
+ 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.
13th Oct 2016, 2:59 PM
Sagy Zino
+ 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.
13th Oct 2016, 2:13 PM
Schindlabua
Schindlabua - avatar
0
SAGY Zino you are right
14th Oct 2016, 1:34 PM
Mehak
Mehak - avatar