 Hello, this is Christian. Welcome. And let's talk about this logical operators in Java, the logical and and logical or operators, also known as the short circuit operators. So here is a diagram I borrowed from the cupscavideas.com website. Thanks for this nice picture. So this is known as is as a closed circuit. As you can see, you have a battery here that provides the current or the power source coming from the positive to the negative direction current always flows in that direction. So we have a switch here that will connect or disconnect the cables here between the green cables here that will turn on the flow current to this light bulb. The light bulb acts as a resistance and will absorb the energy and it gets lined up, right? And the current will continue to flow back to the battery this way. So we have a closed circuit. Now what happens when we introduce a third wire? Say we put that here and we don't connect the cable yet. So when we connect the cable to the battery, you'll see that the current will start flowing from the intended path. Out here, it's going to go through that blue wire. And we assume that the blue wire has no or very little resistance. And here the light bulb is acting as a resistance. So therefore current always tend to flow to the path that has the lesser resistance. Okay, of course, the length of this cable here plays a major role too, but we assume that this is short enough that the resistance is less than the one out here. Okay, so that's what happens when you also use the logical and logical operators and the code. So let's go and see example in Java. Okay, so in that unit five here, I'm going to create a class first and we'll call this in the chapter five. I just call these logical operators. And then we'll put here logical operators as well the class name. Okay, so now let's go and add the main function first. Okay, I want to just do a really simple example, right? So I wanted to find out if N1 is larger than N2 and N1 is positive. Okay, so positive meaning like and not zero. Okay, and not zero. Okay, so you can do something like following and N1 is we'll give it a 10 and two is 15. Oops. Okay, so you will device a code like the following. This is a simple if statements. If N1 is greater than N2. Okay, I'm going to nest that inside another if I mean with another if if N1 is greater than zero, right? That will give you the result. If those are true, then we echo the statement out saying just say true here. Okay, else the whole thing here would be false. Oops. I'll put here false. And if this is not true, then you have to echo that out too, because otherwise we get nothing here. So I'll put here either ways, I'll put in a false we know. Okay, this is the outer false. We know the difference. It's going to output either this, this or this, right? All right, so for this example, we know for sure that it's going to be what it's going to be the outer false because N10 is and two one is less than N2. So let's run this. All right, here we go. And as expected, we're going to get a outer false. Okay, because this is already false. Therefore, we don't need to check the inner part at all. Okay, so when we put here is a zero. All right. Then this is also false. This doesn't work. Let's put 20 instead. I'm sorry. So 20, we know this is true. And then we need to check the inner part. And since N1 is larger than zero, we're going to get the word true here, as expected, right? So we run, and we get true. So very good. Okay, so we have this kind of code here. We have a one if condition nested right inside another if here, we form, you can rewrite this to form a compound condition. So I'm going to go just combine this together, remove that, replace all of that with the ampersand like this. Okay, this is the exact same statement. Of course, you're going to get different output. But let's see, if this is true, then I don't, yeah, I don't need this anymore. This either just be true or false in this case. Okay. So I'm just going to put here true or false because it's going to be outer inner. There's only one this time. Simpler now. So here we're going to test both of these conditions. So the first one here will always be tested doesn't matter if you have one or two or 10. Okay, you always run from left to right and direct in that order. So we're going to test the first condition first, if this is true, then we check the second part as well. Okay, using the truth table right for for the ampersand is the truth table means both or all of them have to be true. So you get a true result in order for this statement to run. Otherwise, if any one of these is false, then you get the false part. Okay, that's the ampersand logic, right? So we run this, you're going to get the same result as before. We're going to get the true, right, because both are true. Okay, so if I put this to zero, we know that the first part is false because in the one is indeed less than in two. So therefore, this is false, and if we get the second part, this is also false because this is not equal to zero. So both are false, so we're going to get false and therefore we get the false statement. Okay, now this is what the logic, the short circuit operator takes place. Okay, what it does behind the scene is that it checks for the first one. If this is true and only then will the second part needs to be checked because we don't know if the second is true or false. It could be false, it could be true so that the whole expression needs to be evaluated together, right? However, if the first part is already false, so it doesn't matter what the second part is, this can be true or false. It doesn't care because the logical end says both must be true in order for this entire thing to be true. So if you already counter a false, so therefore Java would not spend its CPU time to evaluate the second part at all. And that's where we get the false part. Okay, so how do we know that's true? Well, we have to test that, right? So we can do that with simple here. I'm going to write another function. This will return a boolean and let's call it check one or take the end one and end two. And we'll just replace this condition inside here, okay? So we return end one, greater than end two or not, we get either true or false. But before we do that return, we're going to write a statement here just to prove that it's invoked in here. So I'll put here check one and we'll put the value here as well and one greater than end two. So we get the result is the true or false. Okay, so I do the same thing for the second one. Let's copy that, put another statement here. We'll put check two and we take only one parameter here. So we check for check two is greater than zero. So the same result true or false, right? It comes back and now we replace these values with the function called check one. We put in one and two. Okay, this part here will be for the check two and one. Okay, so let's find out if this, the case, run. And here we go. We see that when we check, it goes for the check one. Again, that must be run in any condition. So we check that first and we found that that's false. So when it's false, then it ignores the second check. As you can see, we did not get the check two statements here. So it just go to the else part. So in this case, we get short circuit. Okay, because short circuit because if you think about that back to the wire cable, this is a blue cable. The blue cable goes here and it skips that route. It goes right into the finish part because it gets short circuited right there. Now if I change this back to say 20, right? Now it's going to run both of them because this is true. We don't know the second one. We need to check that. So again, if you run it, you're going to see both statements being checked. So here we go. And the both are true. So therefore the whole result is true. Okay. Now if you put something like a negative 10 and a negative 15, then that would be different, right? Well, I mean it's going to check both of them because minus 10 is indeed larger than minus 15. And it checks the second part. You're going to get a false over here. Okay. So let's run it again. And you get a true and false as we expected. The previous one was like true, true. Okay. So that is how true circuits work. The logical and works the same way in just the opposite of the logical, I mean logical or logical and right. So in this case, we check for the first part. If this is true, then we don't need to check the second at all because all it takes is just one true from any of these conditions here. Right. And that's the reverse of the end. So in this case, is this true? Yes, because and what is indeed larger than two. So therefore, we're not going to see the second statement. And as you can see, we only check the first check. All right. So now let's go back and put into the 20. So the first part is false. So you know, therefore, we need to check if there's a second is true or not. We don't know that. So we need to check both of them. And here we go. We got that. Right. So that's how short circuit works in Java. And there's another operator that uses the single pipe symbol for the, this is called the bitwise or and the bitwise end. You can also use this to check for boolean values in this condition. And what this will do is it's like the opposite of the short circuit. In this case, it doesn't care whether this is true or false. The thing is going to force everything here to be checked. Okay. So I mean, I don't recommend it. It's not common in Java or you use it this way and it's not common to use it to do this way. Well, because it's redundant, like I mentioned, right? If you can see, it's going to check both of them, whether they're true or false. If I put back to a minus 10, you can see that for the app, for the logical end, we know that the first part is a true. So we check the second and so on. But previous one, it checked both of them, even though they were false, right? Or it would be kind of the same thing. So therefore, if it's a logical or, then we don't need to check the second because this is already true, but because of this bitwise or, it's going to check for all of them. So you're forcing everything to be checked. Again, I recommend it, but be careful because it's not kind of common to use it this way. Use it for a different reason. This is the bitwise and a bitwise or can be a little bit tricky and can be a typo because usually you intend to use this but you forgot and you mistyped that you use that instead. So just be careful. Okay, so I hope that kind of clarifies the idea behind a short circuit operator, especially logical end and logical or. Thank you.