Why is x != 0 || x=0 an error? java programming?

Home » Programming & Design » Why is x != 0 || x=0 an error? java programming?
Programming & Design No Comments

Other answer:

Richard:
Ignoring the possible = versus == argument. I have used languages where a single = can be used as a comparison operator. A preceding 'if' tells the compiler that the = is a comparison in those languages.

x != 0 || x = 0
or
x != 0 || x == 0

has the logical fault. One comparison will be true and the other will be false, and ORing these two results will always be true. You could simply replace it with 0 == 0 and get exactly the same result only more efficiently.

I hope this helps.

EddieJ:
In Java, and many other programming languages, you need 2 equal signs to test for equality.

x != 0 || x==0

Chris P:
Because x=0 means: assign the value zero to the variable x.
You need x==0, which means: return whether the variable x currently has the value zero.

In other words:
= is assignment
== is equality

Electric:
you forgot that an comparison operator needs ==

LEAVE A COMMENT