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

Equality

<h2>Equality</h2> <p>In this video we look into how we compare values and <a href="/courses/video/5/157/Using-Variables.html">variables</a> using the <a href="/courses/video/7/58/Equality-Operators.html">equality operator</a> (==). We also differentiate between the assignment operator (=) and the equality operator (==).</p> <p>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 <a href="/courses/video/5/157/Using-Variables.html">variables</a>.</p> <h2>Use of the <a href="/courses/video/7/58/Equality-Operators.html">Equality (==) Operator</a></h2> <p>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 <a href="/course/basics/conditionals-if">conditional statements</a>. 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 <a href="/course/basics/loops-1">loop</a> if they are equal and skip the loop if they are unequal. This will be explained in <a href="/course/basics/loops-1">later videos</a>. But for now, one should remember that the equality operator is for comparing the values and not for assigning the values.</p> <h2>Difference between Equality (==) Operator and Assignment (=) Operator</h2> <p>It should be very clear that == is the <b>equality</b> operator and = is an <b>assignment</b> operator. Here's a small example:</p> <p>var numBoobs:uint = 2;</p> <p>var strBoobs:String = &quot;two&quot;;</p> <p>var c:uint = numBoobs;</p> <p>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!)</p> <ol> <li>we type trace(c == 2); Does this return true or false? (No peeking until after the third one!)</li> <li>we type trace(numBoobs == strBoobs); Does this return <a href="/video/5/162/Boolean.html">true or false</a>? </li> <li>We type trace(numBoobs = 3); Does this return true or false? </li> <li>We type trace(numoobs); what is the value returned? </li> <li>(extra credit) We type trace(c); what is the value returned?</li> </ol> <p>We're going to take these one by one. For each correct one, you get a virtual chocolate chip cookie! Here we go:</p> <p>var numBoobs:uint = 2;</p> <p>var strBoobs:String = &quot;two&quot;;</p> <p>var c:uint = numBoobs;</p> <ol> <li>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). </li> <li>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 &quot;two&quot;: numBoobs == strBoobs will return false. </li> <li>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. </li> <li>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! </li> <li>(extra credit) We type <b>trace(c);</b> what is the value returned?</li> </ol> <p>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 <a href="/courses/video/5/157/Using-Variables.html">variables</a>, 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!</p> <p>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 <a href="/courses/video/4/155/Error-1067-Implicit-Coercion.html">errorz</a>. 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.</p>

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