Used in many programming languages (mostly those derived from C) to denote an equality operator. It is a binary operator that will return 1 if the left-hand operand is lexicographically equivalent to the right-hand operand and 0 otherwise.

Example in C-

int a;
a = 5;
if ( a == 5 ) printf("Hi mom.\n");

Will print "Hi mom.\n" because a is 5.

However, there can be problems. Newbie programmers (and some not-so-newbie programmers) tend to get == (the equality operator) mixed up with = (the assignment operator).

int a;
a = 5;
if ( a = 5 ) printf("Hi mom.\n");

That code assigns a value of 5 to a, then evaluates a to see if it has a value - which it always will do! Some compilers will flag this with a warning message such as "Expression always true.", but some won't. This can be quite a hard problem for a newbie to solve. (If you were to put a = 0, the inverse would happen: the compiler would complain that the expression was always false).

One way to solve this problem is to write your expression like this -

if ( 5 == a )

If you accidently write = instead of == there, your compiler will make an error at compile-time because you are trying to assign a value to an actual number, which is illegal. This will mean you don't have a nasty run-time error to deal with.

However, the above way is a bit ugly, and the best way to deal with this problem is just to learn not to make the mistake. Once you've done it a few times you're unlikely to do it again.

Log in or register to write something here or to contact authors.