if (i=2)
this works because it it checking for integer value...
but if u put a string value eg,
if (i="as")
there will be an error as it is checking for string comparison..
so using == to compare will be much safer..

it will check if i is 2 and then determine where to keep executing. But you are right. If you go

Code:

if (i = 2)
<code>
else
<code>

... i is tested, but not the way you would expect. In that line of code, 2 is assigned to i and then i is tested. An assignment returns the value that is assigned. So you end up with this:

Code:

if (i) // where i = 2
<code>
else
<code>

Now, in C/C++, you can do that and it tests i for a non-zero value. That means zero = false, anything else = true. So if i = zero, which it isn't, then it skips the if code and goes on to the else

== works by returning 1 or true (either way its not zero) if the two operands are equal.

= works by assigning the right value into the left variable and returning the value of the left variable.

A neat little thing you can do with = is this:

Code:

x = y = z = 3;

It sets all 3 variables to 3. Why? = has right-left associability. First it will assign 3 to z. Then the value of that (3) is set to y, then x. Then the final value of that statement is 3 but your semicolon discards that value and execution keeps on going.