In this video we will learn about logical operators. logical operators are useful for writing compound conditional
statements so already in the previous video we have learned about conditional statements, conditions we
can define some condition for writing conditional statement we use a relational operator that is less
than data than, not equal to for whatever the operator finds we can use these operators for writing the
condition so we have written some program in the previous video.
Now if I have more than one conditions.
How to join them suppose i have two conditions that is X is less than y as well as I have
one more condition that is X is less than Z.
So x y z are some variables I want to combine these two conditions.
What is the method of combining them.
So if I combine then that will be called as compound conditional statement.
So that compound conditional statement can be form using logical operators that are AND OR and NOT
is used for negating the statement means if it is true then it will become false or vice versa
AND, OR are used for making a compound conditional statement.
So these operations are interesting.
These are logical for AND in C++ symbol is && and OR it is || you will find it on keyboard
find it on the keyboard somewhere near in enter key.
You will find this, you have to hold shift for this one so there is a pipe.
So these are the operators for AND OR.
Now how they work.
If I use AND how it works.
See it is logical.
So that depends upon its truth values so that we will see using a truth table.
So first let us look at AND if I take an example from daily life.
See when we talk.
we use this word and for joining.
Two sentences right.
So what is the meaning of that and.
even though we are joining two statements or sentences but and place what roll.
So let us observe that clearly.
So I take one example through that I will explain you what does it mean by
And. see if I say that I'm wearing black t shirt and a cap.
So I have combined two statements.
So let me elaborate once again I'm wearing black t shirt and I am wearing a cap.
So those statements are that I have joined them.
And so now you tell me what I said just now.
Is it true.
Let us check it.
t-shirt this is 1st statement.
I'm not writing a full sentence, I'm wearing t shirt then Next one is I'm wearing cap.
Now these two are two different sentences right.
Next I will combine these two by using and so t-shirt is one statement and I will write that word
and instead of using operator so that in C++ you have to use operators but then this is gap.
Now I have two statements these two statements, now look at this I'm wearing t shirt, true.
Suppose I'm wearing cap then also true
Then if I say I'm wearing a t shirt and am wearing cap will the complete statement be true or false
it will be true.
If I'm wearing a cap it's true.
Second one I'm wearing t shirt.
Yes but I'm not wearing cap false.
If I say I'm wearing cap if you see I'm not wearing cap which is false so t shirt is true but cap is false
so not complete statement.
I'm waiting t shirt and I'm wearing cap so the complete sentences is wrong it's false so this will be
false.
The next one I'm wearing t shirt.
suppose i'm not wearing t shirt I'm wearing some other thing like a blazer or shirt then my statement will
be false.
I suppose I'm reading cap.
If you find me I'm reading cap now.
If I say that I'm wearing t shirt and I'm wearing cap so one off my sentence is true.
Which one.
The cap one is true but the t shirt one is false.
So in that way the complete sentence is false though one of them is true but the other one is false
so the complete statement is false.
So this would be false.
Then last one I'm not wearing t shirt even I'm not wearing cap but I am saying that I'm wearing t shirt
and I'm wearing cap.
Both are false.
So then also my statement is false.
This is the truth table it is called as a truth table.
So truth table for AND for AND operation I have written and here.
So this is truth table for that.
So now we can understand when AND will be true if both those statements are true then only it will
be true if any one this statement is false then the result of and is false so for preparing
both conditional statements when you are joining these two.
And if you are writing and in between them then if this is a true one this is also true.
then only the entire statement will be true if any one of the statement is false then the resultof
and is false.
So this is true but this is false If suppose.
Then the result is false. this logical operation works on boolean value so that is true and false only
right and the result is again true or false they should be very much clear for you right. now,
Next we will see OR operation.
So for OR operation I will use these two only and I will just change this one I remove this and
I'll make a table for OR. yes it's ready for or now.
Now I have two statement I'm wearing t shirt I'm wearing cap next let me read it out.
I'm wearing t shirt or I'm wearing gap.
These are two statements I have them with or, or means either this or that.
yes the word or now gives the clear meaning right.
This or that, AND this also and that also.
Both, but or means this or that.
Right.
So let us see how it works.
So I will take these examples and show you if both the statements are true.
Suppose I'm getting a cap so I'm wearing a t shirt true.
I'm wearing cap too.
Both are true.
So my statement is true.
Yes this is true.
Now second one I'm wearing a t shirt.
Yes I'm getting t shirt true that I'm wearing cap I'm not wearing cap it's false it's false.
Now let me complete the statement.
I'm wearing t shirt or I'm wearing cap so or means.
This one is true right t shirt is true.
So this is true.
So in case of or this is true now let us come to the second one next one.
third one see false.
I'm not wearing T-shirts suppose.
But I'm wearing cap.
So if I tell the statement that I'm wearing t shirt or I'm wearing cap so if I'm not wearing t shirt
but I'm getting cap then my statement is true because they said OR.
So this is true next.
Last one.
What are false.
I'm neither wearing t shirt nor wearing a cap but I am sayingthat I'm wearing t shirt or am wearing
cap.
So none of them is true.
Suppose none of them is true then this is false this is the truth table for or.
How to remember this one if any one of them is a true it will be true this is true so it is true this
is true.
So this is true both are false then false so the difference between AND and OR is in and if one is
false everything is false.
In or if one is true everything is true.
this way you can remember it so it means in this one if I write OR right in C++ programming if i write or if this is
true and this is false then also that is true.
And even if this is false.
And this is true then also those are true if both are true theen also that is true and when
it could be false if both the conditions are false then it will be false.
So this is how logical operators are useful for combining conditions mostly students get confused here in
the beginning.
That right two conditions they write a comma or they leave blank no conditional statement, two conditional statement
using relational operators that are joint using logical operators.
Then one last thing I have to show you that is NOT so I'll just remove this table and I show you not
so the last operator that is not not is used for negating a statement negating means if it is true it will
become false if it is false it will become true.
So if suppose I'm wearing t shirt if it is true then not of that one means not I'm wearing t shirts.
So really I'm wearing a t shirt right.
True but not means it is false.
Not getting t shirt so it is false.
Next I'm wearing t shirt, suppose false I'm not wearing t shirt but I said that I'm wearing t shirt I'm
wearing t shirt now if you make not off that statement means not wearing.
That is true.
So it will be true.
So if your statement is a true then the negation will become false and if it is a false then the negation
will become true.
This is very useful.
So it looks simple and right now it may look useless but it is very useful. where it is useful.
I'm show you in some other topic.
So that's all about these logical operators.
These are used for writing compound conditional statement.
We will see how to use them in the next video I will write a program and show you.