02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoPrevious 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.

Overview

What are operators and why do we need them to create programs. We'll discuss various types of operators and their uses later in this course

01:49

Default Values

To understand operators, we first need to understand how variables work -- mainly when it comes to default values and why they differ for various variable types.

06:41

Equality

In this video we will compare between equality and assignment and learn how to work with both. We also differentiate between the assignment operator (=) and the equality operator (==)

06:40

Rational Operators

In this video we will see how to check if a variable is: bigger, smaller, greater or equals, smaller or equals and so on..... using rational operators

05:52

From Rational to Boolean

All the operators we worked with so far returned in our trace panel a value of true or false. In this video we'll see that connection of our operators results to the Boolean data type as we explore

05:26

Equality Operators

So far we've seen how to compare and see if values differ or match but we did not have a way to ask if not. In this sample we'll look into the equality operator(make it a bit more complex)

05:59

Comparison Between Different Types

In the heart of programming is the capability to compare between things. In this video we talk about comparing between different variable data types.

04:15

Explicit Equality

Sometimes knowing if something is equal just isn't enough and we need to know if it is explicitly equal and that's where explicit equality operator comes in...

08:05

Logical Operator

Logical operators enable us to compare a few things at a time such as AND, OR and NOT. We will learn the syntax to represent these operators using few examples

09:52

There Are Many Ways To Say No

doing exactly the oposite of the logical logic seams crazy but actually its a very great way to get things done. Let us learn how to say no in terms of programming

04:15

Null vs. Undefined

Why are there two ways of saying nothing? I ponder that as well as I try to explain their roles. In the process, we will learn the syntax and its application in code

06:20