In this video we'll learn about c++ logical operators.
These operators allow us to build complex conditions in our programs.
C++ has three logical operators. These are not and,
and or.
These operators work on Boolean expressions
and evaluate to a Boolean value themselves.
There are several ways to write these operators.
You can use the keyword like syntax using the words not and,
and or.
Or you can use the operator symbol syntax using the bang sign,
the double ampersand and the double pipe symbol or the double vertical bar.
Personally I've never seen any c++ code
that uses the keyword like syntax
so I'll use the operator symbol syntax in this course.
Let's quickly review Boolean algebra by using a truth table.
The logical not operator simply negates the result of the expression
it's applied to.
Note that this is a unary operator since it only works with one operand.
So if the expression a is true, then not a is false.
And if the expression a is false, then not a is true.
Let's look at the logical and.
The logical and is a binary operator,
and it's only true when both its operands are true.
So you can see in the table when expression a is true
and expression b is true,
then a and b is true.
a and b is false for all other combinations.
Finally, let's look at the logical or.
The logical or operator is also a binary operator.
The value of the logical or is true when either expression is true.
So the only time it's false is when both expressions are false.
Using just these three logical operators, we can write some pretty complex logic.
And if we combine the logical operators with the relational operators,
we're in great shape to begin to solve many, many problems.
Let's see what happens when we combine these operators together in an expression.
Just like we had to understand precedence with the mathematical operators,
we need to understand precedence for the logical operators.
Logical not has the highest precedence, followed by logical and,
followed by logical or.
Of course, as with mathematical expressions, you can use parentheses
to achieve exactly the behavior you're looking for.
Let's wrap up logical operators by looking at a few example expressions
and then talking about something called short circuit evaluation.
Here a few examples of logical expressions.
In the first expression, we're determining if num1
is greater than or equal to 10 and less than 20.
Notice that we use num1 in both expressions.
We can't just write num1 greater than or equal to 10 and less than 20.
We have to write num1 greater than or equal to 10
and num1 less than 20.
Since we have an and, both expressions must be true
for the entire expression to be true.
In the next logical expression, we're using a logical or.
In this case, either expression or both can be true for the entire
expression to be true.
So if num1 is less than or equal to 10,
then the entire expression will be true.
Also if num1 is greater than or equal to 20,
then the entire expression will be true.
Notice how both expressions cannot be true since num1 cannot be less than 10
and it can't be greater than or equal to 20 at the same time.
The next logical expression is checking if it's not raining
and the temperature is greater than 32 degrees Fahrenheit.
The following expression uses a logical or
and it will be true if it's raining or if it's snowing.
Finally, the last logical expression
is checking to see if temperature is greater than 100
and it is humid or it's raining. Remember here,
that the and has precedence over the or.
I think you can see how powerful our logical expressions can be
by simply combining these primitive operators.
Also notice how important our variable names become as well.
Let's finish up by discussing short circuit evaluation.
C++ will stop evaluating a logical expression
as soon as it knows the result.
For example, suppose we have an expression like the first one.
Expression one and expression two and expression 3.
If expression 1 is false,
then there is no way that the entire expression can be true
since all the expressions must be true.
So why bother evaluating expression 2 and expression 3
when the compiler already knows that the result will be false.
This is called short circuit evaluation
and is commonly used in many programming languages.
Programmers take advantage of this behavior to optimize their code.
In the second example if any of the expressions is true,
then the entire logical expression is true. So
if expression one is true,
then the compiler already knows that the entire expression is true
and it won't evaluate expression two or expression three.
However if expression 1 is false,
then the compiler continues evaluating expression 2
since it doesn't know the result yet.
We'll use logical operators in depth when we learn about control structures.
But for now, let's head over to CodeLite and see a few simple examples in live code.
I'm in CodeLite in section 8 workspace
and I'm in the logical operators project.
In this project, we'll go over some few, a few examples of using logical operators.
Let's start with a really simple one here.
What I've got is I'm going to use this variable num
to read numbers into just simple integers.
I'm going to establish a range where the lower end is 10 and the upper end is 20.
So that'll be like a number line, where this will be 10 here and this will be 20 over here.
Now I'm going to ask the user to
enter an integer and tell them what the bounds are, 10 and 20 in this case.
Then I want to be sure that that number they entered is within the bounds,
and I just want to display true or false if it's within the bounds.
And what we're doing here is we're going to say not inclusive of 10
and not inclusive of 20.
Okay, so it's going to be between -- it's going to be 11 to 19 inclusive.
All right. So that's what we're going to do.
Now we're going to store that true false value in this variable right here
called within bounds.
And here's our formula, here's our expression, right here.
Basically we're going to check to make sure that num is greater than lower,
so num is going to be greater than lower so so is that
user typed in 15,
we want to make sure that it's greater than lower
and we want to make sure that it's less than upper.
So we want to be sure that it's in here somewhere,
right. So that's where we've got that logical and condition.
So we'll do that. And then
we'll simply print out the number is between lower and upper and then true or false.
And we can just display within bounds here.
So let's run this with a couple of test cases.
And here we go. Enter an integer - the bounds are 10 and 20.
Now let's enter an integer that is within those bounds.
15 is right in the middle, and we'll press enter.
And the result is 15 is between 10 and 20, true.
Okay. So let's run another test case.
Let's go off the right side to 22. Now we would expect the false.
22 is between 10 and 20, false.
Let's do a test case on the left side.
We'll go five, we also expect the false.
And that's what we've got.
And then don't forget to test those edge cases, the 10 and the 20.
So let's try 10
we expect false, right? Because being on 10 is not being between 10 and 20,
in this case.
So I get back a false. And the last test case we could run is 20,
and we also expect a false and that's what we're getting.
It's very common to have these
errors, where instead of checking
greater than here, you're doing greater than or equal to
when you should be doing greater than and so forth.
So you've got to be sure to test your code. Okay. So that's a simple example
of testing to see if a number is between two numbers.
Now let me comment that out,
and we'll do another example here.
And of course we're assuming that lower is less than upper and all of these examples.
Now we want to do on this one is we want to test to see if the number is
outside the bounds.
Okay. So I want to be, I want to be true if the number is outside the bounds. So
we'll do the same idea here, right? We've got we've got the line and here's the 10,
and here's the 20.
And if the number is over here somewhere, we want a true.
And if the number's over here somewhere, we want it true.
Now we could just simply negate the previous result, right.
So if the number is in here, if the number's not in here then it's obviously either here or here,
right. But what we'll do is we'll do it explicitly.
We've got a variable called outside the bounds.
And we're going to check to be sure that number
is less than lower so that's taking care of this bit right here
or number is greater than upper.
There's no way we can use an and here. That would be a mistake, right, because the number will never be
less than 10 and greater than 20. That won't happen it's impossible.
So that would be a mistake and that's a pretty common mistake
but you'll find it when you test. And there's your or.
So let's run this example.
And in this case, the bounds are 10 and 20.
So let's say we want to go 25. Now we expect true now,
and that's we're getting because 25 is outside of 10 and 20.
Let's try the lower end.
Let's try five.
That's also true,
and let's try one of the this, let's try a number in between,
15, here. We expect that to be false and it is because 15 is
not outside the range. And then we could test with 10 and 20, and I'll leave that up to you guys.
Okay. So let me comment that out.
And the third example is actually checking to see whether
the numbers are right on the bounds, whether they're 10 or 20.
So what we're doing here, again, is we've got the line got our number line,
and we've got 10 over here and 20 over here.
And we want to be sure if we want to return true
if it's 10 or if it's 20.
Okay, right on the bound.
So we've got another variable here called unbounds. I'm setting it to false,
and the condition is really simple. If the number is equal to the lower
that's it so if the number is 10 or
the number is equal to the upper.
Okay. So if we've got a 10 or a 20, in this case because that's what lower and upper are,
this will return true. Remember it can't be 10 and 20 so we need the or here.
And with the or if either piece is true, the entire thing is true.
So let me clear this and we'll run this example.
So we should get back true for 10 and 20.
So here's 10. There we go, we've got a true.
We'll run it again with 20.
That also gives us true and everything else should give us a false, right? No matter what we put in.
Let's put 15,
we get a false and you could use some test cases outside 20
and less than 10 to check it out as well.
So hopefully you're with me here. That's pretty straightforward
stuff but pretty powerful stuff.
This is how you do input validation.
You're asking a user to enter a number between something and something or
you know enter the the amount of your check.
Obviously, they're not going to enter negative numbers.
And there may be a limit to the upper end,
so you can validate your data because if you have bad data,
you're going to get bad results.
Now in this case here, I'll comment all of this out
and we'll I'll go through it and explain it to you.
Okay. So what we've got here is we've got
and,
let me comment that bit out as well, all right, so here's where we're at.
Here's the idea. We want to determine if you need to wear a coat
and that determination is based on the temperature and the wind speed.
So I've got a Boolean value
and that Boolean value right here is going to be where coat that will be true or false.
Whether you need to wear a coat or not.
And I'm going to read two variables in from the user. I'm going to read the temperature
in Fahrenheit and the winds, the wind speed in miles per hour.
And here are my limits.
So wind over this value requires a coat.
So if the wind is over 25 miles an hour, I need a coat.
And the temperature below this value requires a coat.
So if the temperature is below 45 degrees Fahrenheit, you need a coat.
Okay. But we're going to test this two different ways.
We're first going to use an or condition and then we're going to use the and condition.
So for the or condition what we're saying is that
if the wind speed is greater than 25
or the temperature is less than 45, you need a coat.
For the and condition, we're going to say
that the temperature has to be less than 45 and the wind
speed has to be greater than 25.
Okay. And the conditions are pretty much,
I hope what you would expect at this point.
You can see what we're doing here.
We're setting for the or condition.
We're setting it right here we're checking - obviously
we're reading the temperature and the wind speed from the user.
And then we've got temperature less than the temperature for the coat.
That's one of the expressions.
And then we've got the or wind speed is greater than the wind speed for a coat.
So if it's an or that means that if this is true, the whole thing is true.
And if this is true the, whole thing is true so either piece is true.
Down here we're using an and.
So for this where coat to become true
both these pieces must be true. So the temperature must be colder than the temperature coat.
And the wind speed must be greater than the wind speed for a coat.
Okay. So let's run this and we'll get two output statements.
Okay. So let's see - let's say that the temperature is 30 degrees Fahrenheit, that's pretty cold.
And the wind speed is
60 miles per hour, it's almost a hurricane.
Both cases say you need to wear a coat. So in this case,
the or case says true. Obviously, both those pieces are true and the and case
would obviously say true as well.
Okay. Let's run another example here.
Let's say that in this case the temperature is pretty cold,
but the wind speed is not so bad.
So in this case, according to the first rule, which is the or rule,
I need a coat because it's cold.
The second rule says I don't need a coat because the wind speed in this case is too low.
Okay. So hopefully that makes sense. You can see how powerful this can be. Just a really simple
switch of a condition can totally change the logic really depending on what we want.
Remember, check those edge cases those are the ones where people tend to make mistakes on,
greater than or equal to.
Sometimes you don't mean equal to, you just mean greater than or the other way around.
So testing is so, so important.