+ 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.
6 Respostas
+ 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
+ 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
+ 3
Great it's a little bit tricky. Thanks to all to clear my confusion
+ 2
You are confusing Boolean and numeric operators 🤗
+ 1
If your trying to 'Not' the whole expression then put brackets round it. e.g.
if(! (a > 500)) {
blah blah blah;
}
0
Yes, rowdynnejones adding brackets will help clear the confusion caused by operator precedence to get a definite result.