Equality
Equality
In this video we look into how we compare values and variables using the equality operator (==). We also differentiate between the assignment operator (=) and the equality operator (==).
We should focus a bit more on this part -- it's very interesting and easy but can be a bit complicated too. Equality is abbreviated with the symbol ==. In mathematics, equality means = but this is not the case in Flash. In Flash, = and == have two different meanings. The single equals sign (=) is used to assign a value to a variable. So we can't use this sign for equality or for comparing two values. The == symbol only can be used for comparing two values or variables.
Use of the Equality (==) Operator
What is the actual use of the equality operator? In programming, we have to deal with more than just assigning values. We need to compare two variables for equality when we start working with conditional statements. We use Boolean values for this operation. The compiler checks for the equality of the two values and if they are equal, it generates a true output and if not, the output is false. So we can use this operation for comparing two values and we can ask the compiler to run a loop if they are equal and skip the loop if they are unequal. This will be explained in later videos. But for now, one should remember that the equality operator is for comparing the values and not for assigning the values.
Difference between Equality (==) Operator and Assignment (=) Operator
It should be very clear that == is the equality operator and = is an assignment operator. Here's a small example:
var numBoobs:uint = 2;
var strBoobs:String = "two";
var c:uint = numBoobs;
Given those declarations, we have a couple of questions for you to see if you have this concept down pat. (There are cookies at risk here!)
- we type trace(c == 2); Does this return true or false? (No peeking until after the third one!)
- we type trace(numBoobs == strBoobs); Does this return true or false?
- We type trace(numBoobs = 3); Does this return true or false?
- We type trace(numoobs); what is the value returned?
- (extra credit) We type trace(c); what is the value returned?
We're going to take these one by one. For each correct one, you get a virtual chocolate chip cookie! Here we go:
var numBoobs:uint = 2;
var strBoobs:String = "two";
var c:uint = numBoobs;
- we type trace(c == 2); Does this return true or false? TRUE. We assigned 2 to numBoobs. We assigned numBoobs (now 2) to c: so yes, c is equal to 2 (c == 2).
- we type trace(numBoobs == strBoobs); Does this return true or false? FALSE!! Regardless of numBoob's value, a uint (number) will never be equal to a string. 2 does not equal "two": numBoobs == strBoobs will return false.
- We type trace(numBoobs = 3); Does this return true or false? TRUE. We assigned 3 to numBoobs. If you simply assign a variable in a trace, it's going to be true.
- We type trace(numBoobs); what is the value returned? (Who is buried in Grant's tomb?) 3, of course. We just assigned it in the third question!
- (extra credit) We type trace(c); what is the value returned?
This can be tricky: needs a little concentration. 2 is returned: if you got that one, you get TWO cookies. Don't feel badly if you didn't: it IS tricky! But the answer is simple. Once you assign a value to a variables, it will hold that value until something else is assigned to it: period. So, when we declared c, we assigned numBoobs to it right then. NumBoobs equaled 2, did it not? So c == 2. Later we changed numBoobs to 3. Fine: did we assign anything new to c? No, we didn't: it still contains the 2. Easy as that!
If you get confused between '=' and '==', big trouble ahead. If we make an error and place an assignment sign instead of equality sign or vice versa, this will alter the output to a great extent. Moreover, the application does not show any error during compilation and therefore, it becomes very difficult to debug this kind of error. If you are working with AS3 in Flash, the application is developed in such a way to show even these errorz. But even then, we cannot expect the compiler to notify the error every time which may result in waste of time. So be sure to really understand this topic: watch the video again if need be, read this text and go over those examples. By now you should easily be able to differentiate between the two operators.
Ready to Level Up Your Skills?
Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!
Enroll Now for Free!