Boolean logical operators are used
for testing logical conditions.
This is working dot CPP
from chapter four of the exercise files.
There is a Boolean type
in C++, and it's called bool.
And so we'll create a couple of variables here.
A will be true
and B will be false.
And we can print these values using print F
and treating them as if they were integers.
(typing)
And when I build and run this, you'll notice that I get
1 for a,
because true is one
and false is zero so b will be false.
And in fact,
if I were to assign a different value
to a
it still will print as one
because it's a Boolean type
and any non-zero value is true
and true is one in the Boolean type.
The Boolean type is generally implemented
as the smallest possible size of an integer
which is usually eight bits on most systems.
So if I try to print the size of it
size is percent as is ZD
and use the size of operator
on a,
and I'll multiply it by eight to get bits
and we'll build and run this
you see sizes eight bits.
So on this machine with this compiler
a Boolean variable is eight bits.
And it may or may not be different on your computer.
But generally speaking, try to implement it
as the smallest possible size of an integer.
Now C++ has three Boolean logical operators.
And so I can say if
a and b
so the double ampersand is a logical and
then I put true and
otherwise
will puts false.
When I build and run, we noticed that it says false
because the expression a and b is not true.
So the result is false.
So there's a and there's or which is two vertical bars.
And if I build and run that we get true
because a or B is true
because a is true.
If I just say a it's true, there's a knot operator
which is an exclamation point or a bang.
And if I build and run that, it says false.
Now, Boolean operators,
the operands to the operators can be the result
of any logical expression.
So I can say 42 is greater than five and B
and this will be false because B is false.
And if I say and a
I'll get true because a is true.
And so both sides of the logical and expression are true.
42 is greater than five.
That's true.
And a is true, or I can combine them.
I can say not a, and now it's false.
The, and, and or operations perform
what's called a short-circuiting evaluation.
And this is useful to know in circumstances where
the evaluation of an operand is significant.
And so if I say a or B
like this
the B will never be evaluated because a is true.
And it doesn't matter whether B is true
because with the or expression
the result is going to be the same either way.
So when I build and run this as is true
and B is never actually evaluated.
And so the same is true.
If I say B and a,
because B is false
it doesn't matter if a is true or not.
So it's never evaluated.
Some people do notice that there's no Boolean exclusive
or operator.
An XOR operator would evaluate true
if one and only one of the two operators was true.
There's several reasons for this admission.
According to Dennis Ritchie, one of the original designers
of the C language, the XOR operator would be superfluous.
And because it cannot be short-circuited
it would not work like the AND and the or operators.
The XOR is effectively the same as the not equals.
If I say B is not equal to a,
that's only going to be true if
one of them is true, and one of them is false
which is the same as the XOR condition.
So I can evaluate that and I get true
or for circumstances where the opera ends may
not be a Boolean type.
You can use the not operator
in front of each of the operands,
and that will force them into a Boolean condition
and you get the same result.
So the logical operators are simple.
They're straightforward.
They operate on logical conditions and
they may be used for logically combining conditions.