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

There Are Many Ways To Say No

In this video we'll take another look at working with not (!): so far we used our '!' operator while comparing variables. (a!=b). In this video we'll look at another way to work with negative equations in ActionScript - !(a==b)

After learning about AND (&&) and OR (||) operators, now it is time to understand what a NOT operator is. A NOT operator can be defined as the negation of any statement. Most of the math text books have the symbol (~) for the NOT operator, but in programming we use the exclamation mark (!) to denote the NOT logical operator.

NOT Operator (!) (Complement of a statement)

The NOT operator gives the complement of any statement when it is applied. For example, if we write trace(!true), the answer would be false because in terms of Boolean values, false is the compliment of true. This is the reason why a NOT symbol before an equality sign checks for explicit inequality (!==). As we get familiar with this operator, we'll understand how this operator is used in various forms. Here we'll list a few of these before moving to the next video.

Various Ways To Use NOT operator (!)

Firstly, we know that trace(!true) is false. But if we write trace(!true==false), this equation is split before giving the final result, trace(false==false) which is true of course. The same equation can be written like this as well, trace(!(false==true)). Here the result is same as the equation simplified to trace(!(false)) which gives the result as true. Consider another example trace(!(0)). We are using extra brackets just to avoid mixing up variables which may lead to confusion in big equations. This equation is converted to a Boolean form, trace(!(false)), since 0 is the value used for false. So the final result of trace(!0) is true.

NOT Operator (!) using Variables

There are a few important things to note while using the NOT Operator with variables. For example, take a variable isTrue which has a Boolean value 'true' assigned to it. We can write trace(!isTrue) to find the compliment of true which is false. But when we use isTrue further in the program, its value is not altered permanently due to changes made by the NOT Operator. The change is temporary for the particular line and the variable never loses its original assigned value. But if we want to change the value permanently, we must use the assignment operator along with NOT Operator. For instance, when we write isTrue = !isTrue, this will assign a value to the variable which is its compliment. In this case the value of trace(isTrue) changes from true to false.

Video Transcript

00:00And I want to go to is test.
00:03And I want to tell is test,
00:05I'm basically going to put here an equation,
00:07such as, so far we've seen a way
00:10of comparing between two items,
00:11such as, let's say, does A equal B?
00:15And does A equal B?
00:17A does, is not equal B.
00:19If we wanted to ask if it's not,
00:20we could have said, is not equal B, right?
00:24But we have one more way of doing it.
00:26And I want to show that one more way.
00:28So that one more way of doing it
00:29would be not A equals equals B.
00:35So we're basically splitting out the operations,
00:37which is at first we're going to ask if this is equal,
00:41and then we're going to switch it.
00:44So if this is equal, which it's not,
00:46so this would come back as false,
00:49and this is a switcher which would flip it around,
00:51which would turn it into true.
00:53So if we trace that out,
00:55although A does not equal B, is test,
00:58our first is test should come back as true now.
01:02And we see that our first is test comes back as true.
01:05Now just to get this really digged into our memory,
01:09let's just work directly with values.
01:13So if I try to trace out not zero,
01:16what would be the value of not zero?
01:19Zero would be converted into a Boolean,
01:22and zero in a Boolean would be false,
01:25so not false would be true.
01:31So our first trace now should come back as true,
01:34and it does.
01:36Let's experiment a little bit more.
01:38So what if we trace out not false?
01:45Same thing around.
01:46Not false is the same thing as not zero,
01:48so we don't even have to trace that.
01:49Let's just put that here in our extras,
01:52because it's basically the same step
01:54because flash is converting our zero into a Boolean,
01:57and then flipping it.
01:59Same thing would go if we would trace
02:01not one.
02:02Not one should come back as false
02:04because one is true,
02:06and the opposite of true would be false.
02:08So if we test that out,
02:09we should get false as our first option,
02:12which is true,
02:13which is basically the same thing as tracing not true.
02:16Not true is false.
02:19Okay, let's try one more here.
02:21So we've got the not already,
02:23so let's see if we take our Boolean as is.
02:26So our Boolean right now,
02:29if we wanna trace our Boolean,
02:32and our Boolean value right now is test,
02:34let's see what it is,
02:34because we don't remember.
02:36So our Boolean value right now is true.
02:38If we go to our Boolean and say not before it,
02:41before we ask our Boolean,
02:44it's just gonna flip our Boolean.
02:45So instead of being true,
02:47it will flip and be false.
02:53What if we wanted to store that switch?
02:56Although if we trace right now is test,
03:00and let me just comment out the rest of the traces
03:02so they don't get too confusing.
03:09Okay, so we got them out of the way.
03:10Let's go back to our traces.
03:12So I'm tracing out is test.
03:15Now if I test out is test right now,
03:17we store that it should come back as true
03:19because the last definition that we had here equaled true.
03:22What if I wanted to switch the value of is test?
03:26Now if I would just go and trace not is test,
03:31and then trace again is test,
03:35our is test value won't change
03:36because we didn't assign that value into our variable.
03:40If we wanted to assign the value,
03:42as we assigned before,
03:43we would say is test equals not is test.
03:47So we're taking the value of not is test
03:50and assigning it back into our original variable.
03:53Or in other words, is test was true,
03:57so not true is false,
03:59and is test from now on will equal false.
04:02So if I trace out our last trace should come out as false.
04:06Oh, well obviously if we trace out false,
04:08it'll come out as false.

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!