You need to use === (or SameQ) instead of == (or Equal) to test the condition. This is because === always returns True or False, whereas == can remain unevaluated. For example:
a === b
(* False *)
a == b
(* a == b *)
The fact that == remains unevaluated is why it is useful in Solve, Reduce and related functions, where you can write an expression such as a x^2 + b x + c == 0.
Now, == does evaluate in cases such as comparisons between numeric quantities and strings or when the objects being compared are identical. For example:
1 == 1
(* True *)
"abc" == "def"
(* False *)
2 == "a"
(* False *)
a == a
(* True *)
However, make note of the fact that comparison between machine numbers and exact numbers can give different results for == and ===:
1 === 1.
(* False *)
1 == 1.
(* True *)
This is because SameQ tests if the two expressions are exactly the same, down to the representation (which they're not), whereas for Equal (see link to docs above):
Approximate numbers with machine precision or higher are considered equal if they differ in at most their last seven binary digits (roughly their last two decimal digits).
SameQof two finite-precision numbers also applies a tolerance; it's just less than that applied byEqual. So it isn't strictly correct to say thatSameQreturnsTrueif and only if the expressions are identical. (Also, one can overrideSameQ, but if you useTrueQhere that is no longer an issue, and the difference betweenEqualandSameQis then a moot point.) – Oleksandr R. Jul 28 '12 at 01:13MachinePrecisionand differ only in the last bit. – rm -rf Jul 28 '12 at 01:35If[opt == Automatic, True, opt, opt]for cases where==does not evaluate – rm -rf Jul 30 '12 at 14:46SameQif different concepts of sameness are needed (which could of course give arbitrary results), whereasTrueQis a binary distinction that cannot readily admit any other meanings. In this case one could equally well use the fourth argument ofIf, which is perhaps more succinct than addingTrueQ. – Oleksandr R. Aug 03 '12 at 13:30