+ 7

Logical not operation make me confused

1st case int a = 100; void main(){ if(! a>500){ a = 200; } printf("%d", a); } O/p----> 100 2nd case: int a = 100; void main(){ if(! 500<a){ a = 200; } printf("%d", a); } O/p----> 200 Both the condition are same but why output are not please explain it. I want to know.

28th Aug 2019, 9:14 AM
Krishnanshu Dey
Krishnanshu Dey - avatar
6 odpowiedzi
+ 6
It's because it has higher precedence than comparison. !a →a is true, so it will be false (0) 0 > 500 is false, so the code doesn't execute !500 → 500 is true, so it will be false (0) 0 < 100 is true, so a will be 200
28th Aug 2019, 9:21 AM
Airree
Airree - avatar
+ 3
I have an explanation, but I'm not sure AT ALL it is the good one. In first case : !a = 0 0 > 500 //false In second case !500 = 0 0 < a //true
28th Aug 2019, 9:23 AM
Théophile
Théophile - avatar
+ 3
Great it's a little bit tricky. Thanks to all to clear my confusion
28th Aug 2019, 9:25 AM
Krishnanshu Dey
Krishnanshu Dey - avatar
+ 2
You are confusing Boolean and numeric operators 🤗
29th Aug 2019, 3:23 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 1
If your trying to 'Not' the whole expression then put brackets round it. e.g. if(! (a > 500)) { blah blah blah; }
29th Aug 2019, 9:34 PM
rodwynnejones
rodwynnejones - avatar
0
Yes, rowdynnejones adding brackets will help clear the confusion caused by operator precedence to get a definite result.
30th Aug 2019, 7:18 AM
Arnold D'Souza
Arnold D'Souza - avatar