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,278
  • 3
  • 18
  • 37
Vijay
  • 1,951
  • 4
  • 23
  • 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
  • 144,748
  • 64
  • 275
  • 293
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
  • 191,680
  • 20
  • 149
  • 239
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
  • 2,980
  • 2
  • 22
  • 45
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,258
  • 7
  • 28
  • 48