(calm music)
-: So we are done with the arithmetic operators
and relational operators, right?
Then we had one question.
What if you have two relational operators
and which will result in true or false?
Right? And then you want to combine them.
So you want 1 true or false out of 2
or 3 true or false?
So what I'm saying is let's say I have two variables here,
which is x, y and also we have one more commission,
which is a and b.
And then I want to check if, I mean I want x
to be less than y
and I want a to be greater than b, this might result
into a true and false or it might result in true and false.
What I want is the output of I want is I want output
of these two combined can be either true or false,
but I want the combined output irrespective if they are true
or false individually.
So how do I combine this?
Now to combine these two different conditions,
we can use a logical operator in between, example,
this logical operator can be and or I mean,
and the second option is or.
So it can be and and or.
Now what exactly it means?
See, if you want that your output to be true.
So let's say if you want your output to be true.
So you can use, if you can say, hey, you know I want both
of these to be true, then only I want true.
Or you can say if any
of this can be true, I want it to be true.
Now this is very important in terms of computation.
So when you say your computer is smart, it's
because of a computer can decide
based on multiple conditions.
So to do that we use something called a logical operators.
So what we do is again,
this concept is coming from mathematics
is not a programming concept, it's coming from mathematics
where you can use something called a and operator.
We can use or operator
or we can use a not operator, which is with exclamation.
So the and operator in programming, we represent that
with the help of AND and the OR operator.
So we use ampersand for and and we use a pipe for OR
so we can use ampersand which is AND,
we can use pipe operator, which is OR,
or we can use the exclamation mark for not, okay, so
how do we decide what could be the output of two operations?
So let's say the first operation gives you true,
the second operation also gives you true.
So maybe we can do that with the help
of these two conditions.
If both are true, and then what should be the output?
So the output will be true.
Again, I'm talking about in terms of AND operation.
If one is true, one is false, it is false.
If one is false,
I mean the first one is false, second is true.
Again, it is false.
And if you want, if you have both of them as false,
again, it'll give you false.
Now when you want such type of output,
if both are true, then only should be true,
then we can use something called an AND operation here.
But what if you have a different set of things in your mind?
Let me just divide this.
And the second set would be,
let's say if the first one is true
and second one is also true, you will get true.
If the second one is true,
sorry, the first one is true, second one is false,
it'll give you true.
If the first one is false,
second is true, it'll give you true.
And if both are false, it'll give you false.
If you have a condition
or if you have a requirement where if any of them is true,
then it should be true.
So example, if you can see if any of one, so
of course both are true, that's great.
But even if you have one true, that's true,
even if you have one true, that's true.
Even if you have one true, that's true, right?
But if you have both false, then it'll be false.
Now if you compare with this and,
when you have both true, it'll give you true.
If any of them is false, you can see if there's are false,
you will get a false here.
So this is your AND and OR, so that's how you can combine.
We have one more here, which is NOT.
Now NOT basically used to reverse the thing.
Example, if you have true
and if you want the output to be false,
and if you have false
and if the output you want is true,
then you will apply a NOT operation.
Now apart from this, we have one more which we
have not talked about, which is excel.
Again, we'll talk about that in some time.
But then we have three basic operation here we
have AND, OR and NOT.
So basically how you combine this, if you wanna do
that in programming, of course you can also use AND operator
or pipe or you can use NOT directly.
The other option you have is something
called a short circuit.
So instead of using and you can use double and,
instead of using or you can use double or
and not is as it is, we don't have two nots there.
Now what's the use of this?
So first of all, let's talk about and.
So let's say if you have x, x is greater than y.
And if you say, and here and you can say a is less than b.
So we have these two conditions here
and I want both them to be true.
That's where you want the output to be true.
So example, if you have a boolean value, which is r here.
So you can say r is equal to x is greater than y
and a is greater than b.
If in this case, if both are true, the output is going
to be true, right?
But if you, what if you use r here.
So if I remove this, and if I put up r here, if any
of them is true, it'll give you a true.
As simple as that, okay?
Now why do we call it a short circuit?
The reason is, let's talk about OR.
In OR, see the point is if even if you have one true,
the output would be true, right?
So if the first condition itself is true,
it'll not even check for the second one.
Beauty, right?
I mean you're saving your time,
that's why it's called short circuit.
You are saving the time, you're taking a shortcut.
And you will say, hey then what happens
when you have AND here, so let's say, let me put AND,
now in this case, even if so, let's say first one is true
of course we'll check with the second one.
But what if the first one is false?
If the first one is false, it'll not even check
with the second one because the output is going
to be false, right?
So that is why it is called short circuit, okay?
So from this point we are always going to use short circuits
and an order of using a single ampersand.
So it can be, you can say double ampersand for AND,
double pipe for OR, and of course NOT will remain NOT.
So example, let's say if you already have a boolean value,
which is r, so I can say r
and if you want to save that value in s,
so whatever value of r you will get, you will save that.
In example, if r is t, s will be t,
but what if you put a ampersand here or so?
But what if you put a exclamation here?
So in this case, if this is true, if r is true,
it'll give you false, it'll give you a reverse value.
And this is very beneficial when you start working
with the project where you have future, you have a data
and if you wanna do something related to that data,
so based on the data you will change some,
you will do something, you will change the condition,
all those stuff, okay.
This thing looks cool here,
but how will you implement this in the actual code?
Like that's what we're going to see now.
So if you can see,
of course I have to go over the same variables.
I can say int x is equal to let's say 7.
And in fact, you know, yeah, let's do it on your line
and I will say int y is equal to 5.
So we got these two values
and then apart from this, I want two more variables.
Let's say a and b.
So I will say int a is equal
to 5 again and int b is equal to 9.
I'm just getting random numbers here.
And what I want to do is I want
to save the data in a boolean variables,
I will say boolean result
and let's see what the value you're getting.
So first I will declare it here
and I'll also print the value for this.
Okay, so basically we are trying to print result here,
but then I want to perform some operation on this.
So I can say if x is greater than y, okay,
I'm just checking for one thing.
And now you know we have done this multiple times.
I will just compile this code and run
and you can see we got through because the value which of x,
which is 7, is greater than the value of y, which is 5.
But then I want to put one more condition here
and I want both them to be true if I want
to get the outputs true.
So what you can do is you can use ampersand,
double ampersand here and you can say a is greater than b.
Now if you can see the first condition is true, okay,
this will give you true.
But the second one,
the second option here will give you false.
Now, what do you think is the output?
Think about this and let's see.
Now the output, which I'm expecting here is false.
Let's try and yeah, we got false.
So the first one is true, second one is false.
So in this case you both need to be true, example,
if I make this as less than,
of course you should not take change your condition,
you should change your values.
But just we are learning into, we are doing,
we are learning, right?
So it's fine.
Let's compile this code and run.
And you can see we got true.
Since both are true, it's true.
But what if you use a pipe here?
Now pipe simply means OR right?
So even if you are, so let's say
that both the conditions are true now
and both the operations are true, run.
And you can see we got true,
but what if, let me make the first one as false, right?
So you can see we have 7 is greater than 5.
So this will be false. But the second one is true, right?
But still if you compile this and run
and you can see we got true, right?
So this is how you use AND and OR.
When you have multiple relation, of course you can add more.
You can also add one more pipe here by saying the value
of a should be greater than 1.
That's your choice.
How many pipes you want to add, compile,
and run, and you can see it works.
So this is how you use AND and OR.
What about NOT operator here.
So example, let's say I will only say a is greater than b.
Now what do you think what the output is?
So if I compile this code and run
and you can see we got false, that makes sense, right?
Because the value of a is smaller than value of b.
But what happens just before printing result,
I'm saying not.
So whatever result of whatever value
of result is we are just doing the opposite of it.
If this is true, it'll give you false.
If this false, it'll give you true.
And if I compile and run, you can see we got true.
It's because this was false. Other one's false.
And now when I say exclamation result, it'll give you true.
So as I mentioned before,
it's very important once we go forward
towards conditions in if else for loops.
So this multiple conditions
or multiple operations, which will result in true
or false will be very helpful.
Now, apart from this, we have one more which we're
gonna discuss in the next video.