For this lesson, we're going to explore logical operators and then or within an if statement, when
we're evaluating the logical, the expression, the boolean expression from which the if statement takes
a decision either to execute, if it's true, do something or else do something else.
All right.
So we've seen already something like this in let's say we have a program where we type something like
this into a equals five and then if
a.
As of as a variable is greater equal to three, for example, we could say something like this Pranav.
A was greater as a as a variable was greater equal to three.
Yes, so this is a quick review of what we've done before else you're going to do something like this
printf and you're going to say that A is less than three, right?
So let's compile and run this for a second here, compiling one.
Let's see.
What did I just make a mistake?
Print F is correct.
Uh, let's see what it's saying here.
I am missing.
I have an extra parentheses.
I was read down.
What's going on.
OK, compile and run again.
And there you have it.
Uh, let's see.
I'm still getting an error.
Uh, my print f here is backwards print f ego.
Right.
So let's go ahead and run this again component.
Right.
Here you go.
So we know that A is greater equal to three.
Now let's put a slash in here.
And again, here, so we get a new line every time any of these to print out, right?
So compile and run.
OK, all right, so A is definitely greater equal to three since it's five, we started out with a value
of five.
Now what if the value is one and that's companioned run?
And in this case, as you can see, it is less than three.
So it's running the logic for us and it's branching to the proper printf statement according to the
value of eight.
Now, what if we wanted a range?
Right.
So let's change the logic again so we will vary this value.
So let's make this one actually first come in from the user.
So we'll ask the user to enter something.
So maybe something like this prentis enter a value for the variable.
Hey, let's make it a runtime assignment to a suskin F percent D since it's an integer and percent.
OK, now based on that we will have different scenarios.
So let's run this first again just to make sure companioned run and notice here I'm going to give a
value of nine.
So obviously I'm going to get a is definitely greater equal to three.
OK, so this is at runtime now.
What if we wanted to change the logic and let's rewrite.
Let me just delete this part here where we can identify where A is exactly and what what range of values.
For example, let's say we want to identify when it's between zero and 10 and let's say we want to identify
when it's between 11 or greater the greater than 10.
Up to 20.
OK, so we can do something like this if.
And first, let's let's just evaluate between zero 10 a.
Is and that includes greater equal to.
Zero.
OK, so we know that will be the lower bound of that range, but at the same time it's got to be less
or equal to 10.
So the way to do this is the and operator.
So this is an end.
OK, and so, I mean, both of them have to be true, both both a greater or equal to zero and A is
less or equal to 10 inclusive, then we could say that.
A is between zero and 10.
All right, well, let's let's just.
Test this back, so this here, as you can see, we're using a logical operator to evaluate a range.
So what you want to do here is the lower end of the range, and this one here will be the upper end
of the range.
So let's companioned run this.
And let's put a value like five, we know five is right in the middle of that, so it is definitely
between zero and 10.
Let's run this program again and let's run and let's put a value above it.
So let's put an 11 notice.
I'm not going to get anything because I don't have an else statement.
Right.
So, as you can see, if if it's false, simply do nothing, you do not need to print a between anything.
Right, for this case.
All right.
So remember, I mentioned something about verifying, again, other ranges.
Right.
So can we do that so we can we verify if it's between?
Or higher than 10, but less than or including 20, you could write another if statement and you start
with a lower range.
Remember, we already included 10 in the first if statement.
So the second statement, we're going to exclude it.
We're going to simply say a.
Greater than 10, and but this time we're going to include 20 A is less equal to 20, then we can print
off.
A is between.
Eleven and 20.
Which means it includes 11 and it includes 20 as values.
Well, let's prove that so you'll notice here.
So if I run this companion run.
And if I put the lower value 11 within that range, notice the first if statement is not going to run,
the second if statement picks up, why didn't the first if statement not run?
Well, simply none of the conditions were.
Well, one of them was false.
A greater equal to zero was true.
However, a less equal to 10 was false.
It's an N condition.
Both sides right here and right here have to be true for this to run.
How about the next expression?
A greater than 10.
Great.
We're there.
It's 11.
And is it less than 20 at the same time?
At the same time?
Yes, that's an apt condition.
OK, all right, fantastic.
So now we're going to look at the air condition.
So how do you write an or let's play with an author and let's say something like this.
Let me show you what's going to happen if A is greater equal to zero or and this is how you write the
your.
A is, let's say, less or equal to 20, right, notice I'm putting a range here, but only one of them
have to be true to print.
All right.
To to to to actually execute the statement right under the if so, fundamentally, the author is completely
different than the end.
So we have to be really careful on how we pick which operator we want based on what what our design
requirements are.
So what does this mean?
A is greater than.
Zero or is less, I would say greater and equal.
Or equal.
Two zero.
Or it's much more common here or is less.
Or equal.
To 20.
All right, let's see if that's true, if the logic is correct.
So this here again is an Amber operator.
This is an all star operator.
These are logical operators.
And then or let's see what happens.
So let's compile and run here.
Since I've made modifications, let's put a value of 12 and let's try to understand what's going to
happen.
So if I put a 12, the first if statement will not execute its Pranav statement.
We know this because it's not within this range.
We will definitely get this and we will definitely get this.
So let's see.
So we got two lines of code pointing out A is between 11 and 20 is greater than or equal to zero.
And I should have put a slash in there or is less or equal to 20.
OK, so let me just put a suggestion here.
Excellent, so let's run it again and give it a value now that's completely out of range.
Right, so let's put a minus one.
What do you think is going to happen as soon as I hit enter while this one will be false, so this will
not run.
This will also be false.
This will not run.
This also will be false and it will not run out.
I'm wrong.
And why was I wrong?
Because a less than 20 is true.
Right.
So, again, A is less than 20.
So all you need is one of the members to be true for the whole statement to be true.
Right.
So we know with an or is different B, it has a different behavior than it.
And keep that in mind.
All right, so we know that minus one is less than 20, that's not a problem for in your statement,
which means this if will yield a true, you will have to execute what's under the true statement or
the true line for the safety.
All right.
So let's do it again.
Let's give it another value.
Compile and run.
I don't need to compile.
I'm just going to run it.
Let's give it a 22.
So we know we're out of range for the 10 to 20.
However, we know that it's greater than zero.
Right.
So what's going to happen is now the first member, a greater than zero is true for the last live statement.
And we will definitely get that.
And it says here, A is greater than or equal to zero or is less than 20.
What we know that it is greater than 20.
However, it is greater than zero at the same time.
So no problem, it will print that statement.
And that's what an order condition allows you to see.
OK, all right.
So now that we've seen how an and and or behaves, can we combine them?
And yes, there will be situations where you need to combine and then.
OK, so let's go right up another if statement here.
If let's do a small range A.
Is great or equal to zero and A is less than, let's say, five.
OK, and let me just put parentheses here for this grouping here so you can group them or and I'm just
going to write in your statement here.
And I'm going to do another group here, A is greater than seven.
And A is less than 10.
Let's see what happens.
And princess.
The condition is true.
And let's see, when can this happen?
OK, so this is a pre, how should I say, convoluted condition that we're exploring here, but we want
to see one.
Is this condition true?
Right.
So this could be a good exercise to play with.
So let's compile and run and let's give a value of six.
Right.
Notice, the first one is between zero and not a problem.
A is definitely greater than zero.
But notice the condition is true.
Did not happen.
Well, let's think about it.
Why didn't six trigger a true for the last statement.
Well, was a greater than zero true.
Was it less than five.
False.
So this is a false.
OK, let's go over here.
There was a greater than seven false was a less than than than ten.
True.
However, this is an end condition.
So this whole thing is false and this whole thing is false.
So we have false or false gives you a false.
So the last if statement will not run.
OK, let's run this again.
And try to get the the condition is true to run, so let's say we put eight this time, OK, and notice
now the condition is true.
Why?
Because the last range here is true.
The second range here.
Right.
Or the first range here is false.
Right.
So this one is true.
But we have another condition as long as we have one member or one group.
On either side of the war statement, true, you will get a true.
OK, so again, I need you to experiment with and in order to really get a grasp of how to combine them,
depending on, again, the requirements you're dealing with.
So this these are very basic exercises on how and or behave.
You need to explore.
And furthermore.
See you then.