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

Comparison Between Different Types

What happens when you compare different data types in Flash? In this video we'll go over some of the basic operations when comparing different date types. The most valuable lesson you can take from this video is how Flash deals with such a comparison when working with a regular comparison operator(’==' and '!='.)

In this course, we have compared different variables of the same data type. We used a number of rational and equality operators to compare the variables or values which are of same type. Now we'll see how to compare the variables of different data types.

Let us declare a variable 'a' with type int and a variable 'b' with data type number. Let the value be a=1 and b=2 which satisfies the respective data types. Now we can use trace(a==b) to check whether these two values are equal. If yes, you get true or else false. Here we can see that the two values are not equal and hence, the output is false. However, if we use trace(a!=b), the result would be true because the comparison now is for inequality and the condition for inequality is met as the values are unequal. This was explained earlier too but now it is a kind of revision for us.

You might think, comparing variables of type int with type Number is well and good, but what if the comparison is to be done between type int and type Boolean (or type number and type Boolean?) We are going to discuss this here. We can explain this with the help of an example. Let's have variables a:int and b:Number . Assume that a=1 and b=0. Let's assign a Boolean variable now with value true: var isTrue:Boolean = true; We can say trace(isTrue) is true. If the value assigned to it is false then we get trace(isTrue) as false. Here we need to remember one thing that, a true value is considered to be 1 and a false value is considered to be 0 as said in the earlier videos.

So now we have a=1,b=0 and isTrue = true (=1). If we try to compare int and Boolean variables we have trace(a==isTrue), this will result as true. If we compare number type and Boolean type that is trace(b==isTrue), the result will be false because b=0 and isTrue=1 as it is true. Similarly, if isTrue is false then the value of isTrue=0, the results will now take a reverse shift.

We have compared variables of different types whose value is either 0 or 1. But if the value of integer variable a is 5, and if we trace(a==isTrue), the result would be false for both Boolean values (true or false) which is equal to 0 or 1. Neither 0 or 1 is equal to 5; the output will be false every time. In fact if we use the command trace(a!=isTrue), the result would be true as it satisfies the inequality.

One more key point we can take from this video is never use keywords as variables! We know that we can use any letter or word for a variable name but there are a few exceptions here as well. Data types and a few other keywords are already defined by the developers and are stored in the library for specific use. Therefore, usage of such keywords as variables in our program can cause problems. Hence, make sure that the variables declared don't match with any of the keywords.

Video Transcript

00:00Let's go revisit booleans for a second and compare between booleans and numbers
00:05So I'm just going to create here a new variable or actually I'm just going to grab yeah
00:09I'm going to create a new number why not so I'm going to create a new number and I'm going to assign it the
00:14value of one and I'm going to create a new integer and
00:19Although by default integers would be defaulted to zero. I'm going to explicitly say that I'm oh
00:25This is a mistake. You're now allowed to use the word int as a name for your integer because it's a it's a saved
00:31It's a saved word. It's saved for
00:34definitions of integers
00:36So I'm just going to call it I because int would be invalid
00:41Okay, so let's see let's create one more variable and we're going to call this variable is
00:47True, which would be a boolean and we're going to assign it the value of true
00:53Okay, so we created another three variables and we assign to them values now
00:57If we try to trace out and match between and does n equal is true
01:05Why is n equal is true? Well n is equal to is true
01:10and why is that because is true is true and
01:14True is an synonymous to one and false is synonymous to zero
01:20So is n equal is true
01:23Yeah, it is true
01:25And we should get a true value. Well, if we try to compare between
01:30our trace of
01:34I
01:35Equals is true that value should come back as false
01:43Okay. Now if we wanted the value to come back as true, we could have said not equals which is true
01:49Okay, but we're in equals right now
01:52Boolean equals
01:54The n but it does not equal to I because I equals zero now
01:58If we would try to compare between our is true to something completely random is true is is true equal to five
02:11No, he's true is not equal to five
02:13and the reason is is true while it's being compared to five is being converted into a number and
02:19Is true is converted into what number is true is converted into one
02:24So we're asking is one equal five
02:26No, one is not equal five and the way I know that and this is kind of like a little bit off topic
02:31So if it's not clear, then don't worry about it right now
02:35I'm going to call a function so far. The only function we've called was trace
02:39But I'm going to call inside of our trace another function, which is called number and
02:46The number function accepts any expression and converts it into a number
02:51so I'm going to take the is true and I'm going to close my bracket for his number and I'm going to close my
02:56Bracket for trace and I'm going to run it and see what value comes back
03:01So I'm going to comment down all these samples that we have up here
03:04And I'm going to ask what is if you would convert is true to a number. What number would it be?
03:10And we could see the number that would be would be one. Okay, so don't worry about this new
03:17Function for now if it's clearer than gray, but it's not the main topic
03:22So we're going to revisit functions and see them much much more into depth in future videos
03:27So we see that our Boolean is when it's converted to a number is turned into one
03:32So in our trace when we're asking is true equals five
03:36We're basically asking does one equal five and that's not true
03:41Well when we ask n equals is true one equals one and that is true
03:46And when we ask I equals true, we're asking is is zero equal one and that's not true
03:53Okay, so would the trace that we would come back and get back from is true equal five would be
03:59false and
04:00The trace that would come back from I
04:03Equals is true would be false as well
04:11While if we ask if n equals true that would come back is true

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!