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

Logical Operator

We are already familiar with Rational Operators, Equality Operators and Explicit Equality Operators. Now it is time to learn Logical Operators. You will understand this easily if you are familiar with the words AND, OR, NAND, NOR, NOT etc. However, there is nothing to worry about even if you are new to these words as we'll discuss everything in detail about the logical operators. In this video we are going to discuss two important logical operators which are commonly used in programming. The first of it is AND, represented by the symbol (&&) and the second is OR, represented by the symbol (||).

AND (&&) Logical Operator

The first thing to remember is that this operator is represented by the double ampersand (&&) and not the single ampersand (&). Most people make a mistake here by just typing one ampersand instead of two. So keep in mind that the AND logical Operator is represented by (&&). Now, let's move to the concept of equality once again before we start with the (&&) operator.

In the above exercises, we compared an equation for either equality or inequality. It was very easy to compare any two values. But the problem arises when the equation has more than two variables or when we need to compare more than two equations at a time. To overcome this problem, we use Logical Operators along with the Rational and Equality Operators. For example if we need to compare two equations say c>a and c<b at a time, we can write this equation as trace(c>a && c<b). Now the output is given as true only if both conditions are met: c>a as well as c>b. If either one of the two is false or both are false, the result is given as false. Therefore, && is a logical operators which turns on (true) only if both the conditions are true and not in any other case.

OR (||) Logical Operator

We have seen the need for logical operators in the AND logical operator section, so we won't go deeply into why we need to use the OR logical operator. Just a few important things to note here: first the OR logical Operator is represented by the symbol (||). Secondly, when we use logical operators between any two equations, the result is true if either one of the two statements is true or when both statements are true. For example, if we consider the same example with (||) Operator trace(c>a || c<b), the result is true if at least one of them is true -- or the result is false only when both statements are false. Initially, the compiler checks for the first equation: if it's true then the second equation is not evaluated and the result is given as true. But if the first one is false, the compiler then checks the second equation and gives the result depending on the trueness of the second statement.

Video Transcript

00:00So far we've learned about rational operators and equality operators, and the last operators we want to talk about in this round is logical operators.
00:13Logical operators enable us to compare more than one thing.
00:17So as if we do a really quick recap about rational operators, let's create here a Boolean value is test.
00:28OK, I'm just going to call it isTest, which will be a Boolean value.
00:31Let's give it an initial value of true, which you could just hard-coded and really set it to true.
00:37Now let's try to set a value into isTest that would be a rational operator.
00:42Let's check if 3 is greater than isTest, which is a valid statement.
00:51Is 3 greater than 1? Yep, 3 is greater than 1 because isTest is converted into 1.
00:58So is 3 greater than isTest? Let's check how much isTest equal now.
01:04Is isTest still true? And it is. Very good.
01:11Let's move on. You could leave it in the same row.
01:18Let's move on. So we've done a rational operator, operators greater, smaller, equals.
01:25The next set we tested were equality operators, such as isTest equals 1, equals, equals, equals isTest.
01:40And that should come back as false because isTest is not explicitly equal to 1.
01:45Although it is equal to 1, it's not explicitly equal to 1.
01:49So if we test our isTest now, it should come back as false.
01:54But if we wanted to stay as true, we could, instead of that, ask it if it's not explicitly equal to 1.
02:01And 1 is not explicitly equal to 1. So our trace should come back as true.
02:07Okay, so we're still at true. Let's just type it here just for reference.
02:15So we've talked about rationals. We've talked about equality.
02:21Let's look at logical. Logical enables us to compare more than one item.
02:27So for example, isTest, let's create a few variables for us to test.
02:33We're going to create a variable called a, which will be a number, not a null, a number.
02:40And we'll give that value of 10.
02:45And we'll create another variable, which we're going to call b, which will be a number as well.
02:49And we'll give it the value of 20. And we'll create one last number.
02:56And we're going to give it a value of 30. And now because we're working only with rational numbers,
03:02there's no real reason for us to use numbers. Let's leverage some of the knowledge that we had from the last videos.
03:08And let's use UN because we're only going to work with positive integers.
03:14So I'm going to change that to be UN. It's going to be better for the processor.
03:18It's going to make things run more smoothly.
03:22Let's delete this part because we're not really there.
03:26Let's look at our isTest again.
03:30So we've seen that we could compare between, let's say, a is a greater than b.
03:38Or let's put there something that actually would be true as b greater than a.
03:43And we've seen how to do that. And that would come back as true.
03:47What if we wanted to compare if b is greater than a and that b is smaller than c?
03:55The way we would do that, we would use the end-end operator,
04:00which will basically mean that we want to compare this value and that value.
04:06And only if both values come back as true, then return true to our trace, to our isTest in this scenario.
04:13So let's continue. So if b is greater than a and end,
04:20if b is smaller than c, then return a value of true to isTest.
04:28So if we trace out this value, we should still be getting true.
04:34And indeed, we're getting still true.
04:39Let's put here an equation that's not true.
04:45Put here an equation that sounds quite easy to do that.
04:48Let's type it for practice.
04:51If b is smaller than a and end, b equals equals 20.
05:00This is true, but it's never even going to get here because this is false.
05:04And because this is false, as soon as we trace out, because b is not smaller than a,
05:09b is larger than a, isTest would come back as false.
05:18Or in other words, what we're basically tracing right here,
05:21if I'll just directly go for a trace, I'm basically tracing isFalse and endTrue,
05:31which is the exact same thing as this expression.
05:35Or is that Boolean?
05:37And isFalse and endTrue?
05:40No, because only if it's true on both sides, then it would return as true.
05:44So this trace should return false.
05:46Very good.
05:49Okay, so we're starting to get a hang on how to work this logical operator.
05:55What if we wanted to check or?
05:58Which basically means we want to check if this side is true or the other side is true.
06:05How would we do that?
06:07Okay, let's see how we would do that.
06:09For example, isTest equals, let's check if C equals, explicitly equals 30.
06:18Or C equals 20.
06:24Now let's trace that one out.
06:28So notice that the way, oh, I've added here one, two, and many brackets.
06:36Notice that this is the sign that I'm using.
06:38I'm using two pipes.
06:40I'm using two pipes to say or, not one, two.
06:44Same thing for the end.
06:46You always type two.
06:48Okay, so what would the value that would come back here?
06:52So one, is C explicitly equal to 30?
06:55Yep, definitely.
06:5630 equals 30 no matter how you try and wrap it around.
07:0120, is C equal 20?
07:02No, so 20, that would be not true, but it doesn't really matter because it's an or.
07:06So because it's true on this end, the isTest is going to return as true.
07:11So if you run the application, isTest will come back as true.
07:15Now, just as a side note, this equation would never even compute because FlashReady would see that this is true
07:22and it would see that there's an or statement here, so this wouldn't even ever execute.
07:28It's kind of irrelevant as soon as this became true.
07:31Well, if the value here would be not true, okay, we could make it not true right here just by making it not.
07:40So is C not explicitly 30?
07:42It absolutely is explicitly 30, thus this is false, and this is false as well.
07:49So the value that would come back would be false because there is no true.
07:55What would happen if we would take the exact same value, let me just grab this, copy this,
08:03and change the or to be end.
08:08What value do you think would come back?
08:10I'm going to keep quiet for a second here so you could think about it.
08:14And the value is false again.
08:21Let's try to analyze it and figure out why is it false.
08:24The value that's coming back here is false, and the value that's coming back here is false as well.
08:29So false and false together are still false because we would expect true on both ends for it to be true.
08:36While in an or statement we will expect only one to be true, or at least one to be true.
08:42And it does not match in that case either.
08:45So let's try one more experiment here.
08:48Let's ask if C equals 30 and end B equals 20.
09:02And if we look back into our variables, if you forgot them, B equals 20 and C equals 30.
09:08So is this valid? Absolutely.
09:11Because C equals 30, that's true, and B equals 20, that's true as well.
09:15And because we're looking for an end, it's comparing both.
09:18And if we check our is test, which we'll output in our last is test, it should come back as true.
09:24Excellent.
09:26Now if we grab the same equation, and instead of that change that to be an or,
09:34and so we're asking if this is true or that is true, and we already know that both are true,
09:40then for it's either or, if one of them is true, that's it.
09:44Okay, it's true.
09:45Okay, so basically, well not only basically, the value that would come back would be 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!