The difference between = and ==
 
 
 

A common problem when you’re writing MEL code is to confuse two very similar looking but different operators:

If you mix these two operators up it can cause very hard to find bugs in your MEL scripts.

Imagine you want to print a message if the value of variable $a is equal to 10. If you try the following:

if ($a = 10) {
	print "equal to 10!";
}

You’ll find this script always prints “equal to 10!” no matter what. This is because this script mistakenly used a single equal sign (the assignment operator), so the “test” is actually assigning 10 to $a. An assignment evaluates to the assigned value, in this case 10. Any value other than zero is considered true, and so the condition is always true!

The correct code in this case is:

if ($a == 10) {
	print "equal to 10!";
}

(Notice the use of == instead of =.)

Using an assignment in a conditional can actually be a useful shortcut in certain situations, however beginners should always beware of mixing up = and ==.