x = 0
sets the value of
x
to
0
, whereas
x == 0
is 'true' if x equals 0.
Let's take a look at your if statement in the first code block:
if (x = 0)
x = 0
sets the value of x to zero, but this expression has the new value of
x
(thus zero) as return value. So this
if
statement is equivalent to
if (0)
(with the additional effect that the value of x gets set to zero).
0
maps to false (all other integers map to true), so the else block gets executed.
In your second if block:
if (x == 0)
x == 0
is true, so the if block gets executed.