0

I think

if( newItem.ReceiptNo != 0 && newItem.ReceiptYear != 0 && newItem.SR != 0)

and

if( ! ( newItem.ReceiptNo == 0 && newItem.ReceiptYear == 0 && newItem.SR == 0))

Should be same. But I am not getting same result on android

Sachin
  • 3,424
  • 3
  • 21
  • 42
Vijay
  • 2,021
  • 4
  • 24
  • 33

4 Answers4

1

Logic doesn't work that way.

if( newItem.ReceiptNo != 0 && newItem.ReceiptYear != 0 && newItem.SR != 0)

is the same as

if( ! ( newItem.ReceiptNo == 0 || newItem.ReceiptYear == 0 || newItem.SR == 0))

Notice the || instead of &&.

See also: De Morgan's Laws

laalto
  • 150,114
  • 66
  • 286
  • 303
0

You would be wrong, carry out the negation on your second if

if (newItem.ReceiptNo != 0 || newItem.ReceiptYear != 0 || newItem.SR != 0) 

Remember the negation of and is or.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

Statement 1

if(newItem.ReceiptNo != 0 && newItem.ReceiptYear != 0 && newItem.SR != 0)

This means that all 3 should not be zero

Statement 2

if( !(newItem.ReceiptNo == 0 && newItem.ReceiptYear == 0 && newItem.SR == 0))

first
newItem.ReceiptNo == 0 && newItem.ReceiptYear == 0 && newItem.SR == 0 means all 3 should be zero and not of this means any of the 3 should not be zero

to this to be same change this with

if( !(newItem.ReceiptNo == 0 || newItem.ReceiptYear == 0 || newItem.SR == 0))

for eg

a != b is equal to !(a=b)
(a != b && a != c) is equal to !(a=b || a=c)
(a != b || a != c) is equal to !(a=b && a=c)

whenever !operator comes out and applied on result of some operations then it will revert all opertion like && will convert into || and || will convert into &&.

Ashish Aggarwal
  • 3,018
  • 2
  • 23
  • 46
0

Even if there was noticeable difference, I think compilers are smart enough to care for such things. So my advice is to use what makes the code easier to understand, and leave micro-optimizations to the compiler.

See : Is the inequality operator faster than the equality operator?

Community
  • 1
  • 1
rachana
  • 3,344
  • 7
  • 30
  • 49