- [Instructor] Conditional statements like this
if, else statements rely on logical operators.
We briefly talked about these earlier
and now we can see them in action.
A conditional statement is looking for a condition
to either be met or not met,
meaning we can use any expression
that returns either true or false as the condition.
In this example, I'm using the backpack age method
which returns the number of days
since the backpack was originally acquired
and then I'm asking if the everyday backpack age
is larger than or equal to 30,
then we return backpack is used,
otherwise we return backpack is new
meaning the backpack is really under 30 days old.
This is a good example of how conditional statements work.
I can now change those to say,
backpack age must be less than or equal to 30.
Then of course I would have to change the logic here
to say backpack is new at the top
and then backpack is old at the bottom.
If I want to test to see if backpack age
is not equal to a specific number,
I can just put a bang in front,
say not equal to or not absolute equal to
and then we will only return true
if we get any value outside of 30
but if we get 30 then we'll have that set to true.
Now, in addition to these comparison operators,
we also have logical operators
that allow us to make more complex statements.
In specifics, we have And and Or.
So let's say I want to test to see
both whether the backpack, it has a certain volume
and how many pockets it has.
So if it has a large volume and a lot of pockets,
then I say, it's a big pack.
If it has a small volume
and just a couple of pockets, then it's a small pack.
In that case, I need to create an And statement.
So I'll say, everyday pack
and then we're looking for volume first.
So I'll say floor volume is larger than let's say 15 liters
and so two on percents, everyday pack again
and this time we're looking for the pockets.
And I say, if the pockets are larger than or equal to five.
So now we're testing for both of these conditions
and only if both of these conditions are met
do we say backpack is big.
Otherwise backpack is small.
Save that and over here it says backpack is big
because the volume is 30 and the number of pockets is 15.
We can then test to see.
So if we say volume needs to be 35
for the backpack to be big, save
and then we'll say backpack is small
because one of these two things do not meet.
When we're using the And statements,
both of these two conditions need to be met
for the entire expression to return true.
We also have the Or statement.
It's signified with two pipes like this.
Now we're saying if either the first statement
or the second statement or both are true,
then we return true, otherwise we return false.
Now you may have noticed this does not test
for either A or B, but not both.
We don't actually have an expression for that.
We'd have to combine some different expressions
to make that work, but the logical operator Or
will test for either condition A or condition B or both.
So between the standard comparison operators
and these two And and Or, you're able to string together
whatever kind of logical expression
you want to test for whatever condition you want.