02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

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!)

  1. we type trace(c == 2); Does this return true or false? (No peeking until after the third one!)
  2. we type trace(numBoobs == strBoobs); Does this return true or false?
  3. We type trace(numBoobs = 3); Does this return true or false?
  4. We type trace(numoobs); what is the value returned?
  5. (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;

  1. 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).
  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.
  3. 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.
  4. 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!
  5. (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.

Video Transcript

00:00So I'm going to click on my command comma to select everything inside of my function
00:05and I'm going to delete it now make sure that you're inside of an area inside of your function
00:09this is our main function and I'm going to select and this is where we've been all this time so
00:14we're going to click on command comma and I'm going to click enter enter and just get rid of it
00:20okay so we have a clean slate again the next topic I wanted to talk about was rational operators
00:26and rational operators reconnects with our boolean values as let's see how that happens
00:33so if I trace out a value here I'm just going to trace here some value what if I wanted to
00:38compare between two values and see if they're equal to each other the way you check if values
00:43are equal to each other and flashes by the rational operation of assignment operator of assignment and
00:50the way we do that for example is one equals one if we didn't want to assign one into one which we
00:57don't wanted to check if they're equal we would place a trace of one equals equals one now if
01:05we run the application we're going to see that flash tells us true one does equal one well if
01:12we trace out is two equal one flash will output false because two is not equal to one we're getting
01:22back boolean values of true or false now as I could work directly with numbers most of the
01:29time I wouldn't really work with numbers directly I would have a variable so for example
01:33let's have a variable of a which is a number and assign to it a value of five
01:38and let's create another variable of B which would be a number and let's assign into it the value of
01:4610 and let's create another variable call it C which would be a number and let's assign into it
01:51the value of B okay so C would be equal 10 and let's try this out one more time so instead of
02:01this trace that we've traced out numbers directly let's use our variables so what would be the
02:07value if we try to trace does A equal B does A equal B would equal A equal B no it's not equal
02:18so it would equal false well as if we try to trace that does A equal C that would be true
02:25and the value would output as true so if we run it again we're going to see false as our first
02:30value oh and we see we have here a little problem right because we didn't A does not equal C
02:38B equals C because we assigned the value of B into C so let me correct that mistake and ask does B
02:45equal C one equal would be assignment and we don't want to assign the value of C into B right now
02:51we've done that we've done we have signed the value of B into C already earlier we don't want
02:57to assign a value we want to compare two values so if I run the application one more time now we're
03:03going to see the values that we I was expecting which false would be the value of our first trace
03:08and true would be the value of our second trace let's just talk here for a second what would
03:14happen if you would actually assign A equals C in your trace where you're actually telling
03:22flashes take the value inside of C and put it into A so basically because we assigned it
03:31we assigned a value in the third one notice what output we got when we assigned the values our
03:37third trace equaled to 10 why did it equal to 10 because we basically assigned the value of C
03:46which was 10 into A and now A equals 10 so we outputted 10 it's very important to notice this
03:55and not make this mistake because making this mistake would be very costly and you might
04:01it might take hours to figure out now same same is true for if we wanted to put the value
04:07of B into C we would we wouldn't want to put an equation sign because flash might
04:14might not understand what we want from it this is a completely valid statement
04:18if we run that application one more time flash is not generating any error
04:23although what we wanted actually to do was assign the value of B into C
04:27or even better right now let's say we wanted to put the value one into C and then trace out
04:35C let me just type in here so A to C we want to would output
04:4410 okay and that's really important to notice
04:59so let's comment out these traces and focus on our on our third on our new trace which is our C
05:05now if we assign we're what we tried to do what we wanted to do but we made a mistake we did
05:10we put a double equation and we tried to assign what we wanted to do was assign the value of
05:15one into C but instead of that we actually didn't do anything all we've done here is
05:21we checked to see all we are doing here is checking to see if C equals one
05:34so it checked it return a true or false but all this happened in the same line
05:39and it's over so now if we run our C our C will not change a value it still equals 10
05:45on the other other hand on the other hand if we do it right and we do assign our value
05:53and then we trace it out then our trace will will come out okay so it's very very important
06:00because this is one of the most common errors that beginner developers make which and not only
06:06well in action script three it's much easier because a lot of times you're going to get
06:10compiling errors or warnings sometimes you won't and mistakes like this could
06:16cost you hours of tinkering and figuring out why that happened so it's very important to
06:21always be extra careful and notice this assignment when you want to assign a value into a variable
06:29or when you want to compare two variables that it's completely different that one equals
06:35is assignment well two equals is comparison

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!