Comparison Operators

Comments
Beware of accidentally using a single equal sign (e.g. if (x = 10) ) where a comparison is intended. Always use the double equal sign which is the comparison operator (e.g. if (x == 10) ) and in this example tests whether x is equal to 10 or not. The single equal sign is the assignment operator, and in the above example sets x to 10 (puts the value 10 into the variable x).


 * (x == 10) is only true if x equals 10, but the (x=10) will always be evaluated as true.

For example C evaluates the statement if (x=10) as follows: 10 is assigned to x (as the single equal sign is the assignment operator), so x now contains 10. Then the 'if' conditional evaluates 10, which is always TRUE, since any non-zero number is taken as TRUE. Consequently, if (x = 10) will always evaluate to TRUE, which is not the desired result when using this 'if' statement. Also, the variable x will be set to 10, which again is not a desired action.

Modified from Arduino Reference If