Transcript for:
Module 9 Video - Logical Operators: Part 2

In this video, we will look at the NOT operator. The NOT is represented by the tilde character, which is a little squiggly line, typically up near the top left of your keyboard. Now, you might recall that we have two predefined logical values, true and false. So if I say tilde true, in other words, not true, that should yield a zero or a false, and indeed it does. Similarly, if I say not false, that should yield a true. Now, you might ask the question, what happens if I apply the not operator to a numerical value instead of to a logical 0 or 1? Well, I have already defined some variables, a, b, and c, and so let's see what those do. a is a 5, so if I say not a, What is that going to give me? Because, whoa, a is a 5. What is not 5? That could be almost anything. Well, let's see. Oh, that's a 0. Now, the reason is the way that MATLAB works is if you ask any form of a logical question concerning a numeric value, 0 is accepted as false. Anything else is accepted as true. So a 5 is assumed to be true, and so it gives you a 0. On the other hand, if I say not b, b is 0, that should return a 1, which it does. Now c is a minus 3. Now if what I said is true about any non-zero value being true, if I say not c, that should also yield a false, assuming that it interprets the minus 3 as true, and indeed it does. Sometimes when you are asking questions, it's easier to phrase the question as not something rather than what the thing actually is. So, for example, you might want to ask the question, is x not greater than low? Instead of asking the question, is x less than or equal to low? You might think you could ask that in this form. I want to invert not. the question x greater than low. Now since x is zero and low is minus two, then x greater than low should be true. And then I have not that, which should give me a false. Oh, it gave me a true instead. Now the reason is the priority of operators. The not operator has priority over almost everything. And so what it does is it says not x. which gives us a 1 and then it says okay is 1 greater than low and the answer is true sure it is so in other words you've got to put parentheses on that so if you want to ask the question in that form not X greater than low you have to put the X greater than low in parentheses now the parentheses force it to ask the relational question first is 0 greater than minus 2 yes that's true and then not that which will give you a zero. The moral of this is when you're using combinations of logical and relational operators, until you are really, really confident that you know exactly how these things are evaluated, it's probably best simply to put in parentheses. Sometimes they may not be necessary, but they won't hurt as long as you put them in the right place. But that may keep you from... making some mistakes and things like that can be really really difficult to track down. Okay before we wind this one up let's do one more quick example. Let's return to the question we asked in the previous video. Is x between low and high? But this time let's consider the case when x is not between low and high. In other words it's outside of that range. One thing that we could do in order to achieve this is to ask the question, is x between low and high? And as we saw earlier, we can ask that question by saying low less than x and x less than high. But now we want the opposite of that. So we put the entire thing in parentheses and we precede it with not. And since x is between... low and high. The expression in the parentheses will be true, thus the overall question will be false, meaning that x is not outside of those bounds. Again, remember the not operator has priority over almost all of the other operators, so you need to be real careful when using not to make sure you've got the priority of operators correct. In our next video, we will consider the other form. of and and or, which do not exhibit the so-called short circuit behavior.