So, we saw in that previous thing one block within a block.
And, and we're going to do that.
We can have ifs, we can have loops that
get indented, but then we can indent even more.
We call that nested, where there's an indented
area that's in an area that's already indented.
So here's a nested decision.
And it might be easier to start on
this side, where I'm going to have a first choice.
Is x greater than 1, yes or no, and if
it's yes, I'll do some work, and then I'm going to
ask another question, and if that's yes, then I'm going to
do this, then I'm going to come all the way back in.
And the way we encode this in Python is, x equals 42,
if x is greater than 1, it's true, so we continue working in the indent.
And now we say, oh, if x is less than 100, which is still true,
so we go in farther, and we do this, and now we come out.
We don't come out to here, we actually keep going
all the way to here, so that ends both blocks.
And so if you sort of think about this,
again this is where I want you to start seeing
what's in a block of code and what's not in
a block of code, and how the indents sort of,
like, put a boundary on the blocks of code.
And so, the first thing you should see is, sort of, like, that
purple part, the, the x less than 100, print, that's kind of a box.
And you can see the box on the, on the sort of flow diagram as well.
The boxes are there. The boxes on the flow
diagram are places where there's one entrance and one exit.
And then there's also, sort of, the larger box, right?
There's this if box that includes the smaller box.
So, the, there's this nesting, which is boxes
within boxes, or indented areas within indented areas.
Now that was a, what we call a one-way decision, where
you're doing if, and this code either runs or it doesn't run.
It is extremely common to want to basically say,
look, I'm going to do one of two things. I'm going to
ask a question, if the question is true, I'm going to do
one thing. If the question's false, I'm going to do another thing.
So that's what we have shown here.
We say, is x equals 4, is x equal to question mark?
If it's yes, we're going to go here.
If it's no, we're going to go here.
We,re going to execute one or the other, and then we're going to continue.
So we're really at a fork in the road here, right?
We're, we're at a fork in the road, going to make
a choice, and one or the other, but never both, right?
So, we're going to do one thing, or we're going to do another thing.
We're going to do one of the two, and
depending on what the question that we ask,
the question that we ask is, which one that we're going to do.
So here's a little bit of code.
x equals 4, is x greater than 2, the answer is yes.
Then we come out and hit this else and we automatically
skip, right, because we're only going to do one of the two.
And here's the picture, x equals 4. Is x equal to yes?
Print, done.
Which means we'll never do both this and that, never do both, both sides.
We're going to do one or the other of the sides.
And just sort of going with the box,
that is our box, oops, go back, go back.
This is our box, right?
It's sort of the indent followed by the final indent.
The else is really kind of part of it.
And then we can draw the picture here. It has one entry and one exit.
Okay. So we have one-way ifs, and we have
two-way ifs, and now we have multi-way ifs, okay?
So, here is a multi-way if,
and it introduces a new reserved word, elif,
which is a combination of else and if.
And this one, probably, is just as easy to talk about the picture here.
The first question is asked, there's still
going to only be one, there's only going to
be one, one and only one of these three choices are going to run.
Once it's run one, then it's done, okay?
So the, the way to think about this, if x is less than 2, we're going to
run this one, and then we're going to kind of
flush all the way out to the bottom.
If x is not less than 2, and it's less than 10,
we're going to run this one, then flush out the bottom.
And if x is not less than 2, and x is not
less than 10, we're going to run this one, and flush out the bottom.
So, one of these three, one, two, three, one of those three is going to run.
And it's going to run based on the questions that are being asked.
The questions do get asked
in an order, and the order does matter, okay?
So that is a multi-way if.
If, else if, else. So this is kind of like an otherwise.
The else is like an otherwise, you know, one way or another, we're going to run
something, and if none of these first two have run, we will run this one.
We call it a multi-way if, okay?
So, here's an example of our multi-way if.
That, if we say x equals 0, x equals 0.
Is it less than 2?
Yes, it is.
So we run Small, print Small, and then we flush out the bottom.
If we switch, instead, x to 5, x is 5.
Is it less than 2? No, it is not less than 2.
Is it less than 10?
Well, 5 is less than 10. So the answer is yes, so we print Medium,
then we flush out the bottom. One and only one are going to execute.
Now, in this case, we got x is 20.
And so we come through here. Is it less than 2?
No, it is not.
Is it less than 10? No, it is not.
So we're going to do this one, and then flush out the bottom.
If we go here, it's false, false, go here, all else being equal, we run that one.
So this one doesn't run
and that one doesn't run, right? Because these are like gateways.
If it were true, it would run it.
But it's false, so we're going to skip it.
This one, it's false, so we're going to skip it.
But then we hit the else, that's like a catch-all.
And then if none of these were true, then it will run the else.
Any questions?
Okay. Well,
I'm going to ask you a question, in a second.
Okay, so just a couple of things that probably you're wondering about.
You don't actually need an else.
You can have a multi-way, x equals 5, if x is less than 2, there's no else here.
You'll notice that this print just comes back.
And so this way, it could, if both of these are false, it could
skip them both and just run right through here, and there's no else clause, okay?
So, in this case, if this one's
going to, the way this one's going to run is, x equals 5
if x is less than 2, it's, it's not.
And it skips to here.
Else if x is less than 10, which it is, it will run that one and come here.
But, for a different value of x, like 95, boop boop.
If x was 95, or 59, this would be false. It would skip it.
This would, elif, would still be false, and it would skip it,
and the only thing it would print out would be, All done. Okay?
Okay, you can also have many elifs.
So, better change to green.
It checks this one, if it's true, it runs the first one.
If it's false, it checks this one.
If that's true, it run this one, and then it skips, right?
And so, so the way to think about
this is, is, it just goes through and checks this one
false, this one false, false, false, oh, I finally found one.
And now I'm done.
It still is going to do one and only one of these.
This one has an else, so that sooner or later, it is going to do one.
And it only will do the else if all of these are false. All have to be false.
Then it will, actually, come and hit the else clause.
It's great, because there are lots of situations where
you're like, oh, is it before eight in the morning?
Or is it between eight and noon?
Or is it between noon and five?
Or after five? After midnight? I don't know.
Okay, so, here, coming up, is a question.
And, you, there's two puzzles and I'm going to
stop so you can look at them for a while.
And I want you to figure out,
in both sides of this, which of the lines
will not execute, regardless of the value for x.
So in both sides, there is a line
that won't execute, regardless of the value for x.
Which will never print?
There's two problems, problem A and problem B.
Okay, I'll have some coffee while you think.
[NOISE].
Okay, hopefully you paused it so that you could actually think for a bit.
So, so I'm going to guess you probably
got the first one right, that's pretty straightforward.
I mean, actually, you're in great shape if you got both of them right.
If you got any of them right, you're in great shape
because that means you're starting to get it.
Starting to like, oh, I'm seeing, kind of, this flow picture, there's a picture.
I look at these characters that seemingly
look like gibberish, and a picture arrives.
Or a pattern of access execution arises.
That's what we want to see.
So, the in the first one, which will never print?
Well, we're looking for kind of a value for x which will be defective.
So if x is less than 2, we're going to do this.
Else, if x is greater than or equal to 2, we'll do this, else we'll do that.
Well, here's the problem with this one.
For all values of x, it is, is either going,
x is less than 2 is either going to be true or greater than equal to 2.
Greater than or equal to be, pah, for X to be
greater than or equal to 2 is going to be true.
So it's going to run this one, or it's going to run that one.
So for big numbers, numbers above 2,
it's going to run this one; below 2, it's going to run that one.
So this one is never going to run, okay?
Because the, one of the first two is going to be
true, and so the third else situation is not going to run.
Hope you got that right.
Okay, so let's take a look at the next one, okay?
So the question is, you know, if x is less than 2, do this,
if x is less 20, do that, and if x is less than 10, do this,
and otherwise do that. Well, the one that will never execute
is this one. And, x equals 15,
no, x equals 15 is a bad one, x equals 5 is the one that will,
sort of, cause it to behave badly.
And so, if x is 5, this is false.
If x is less than 20, this is true, and then it's done.
So the problem is, this is the one that will never execute, because
if a value is less than 10, it's also less than 20, so this will be true.
So for a value like 5, which happens to be less than 10,
which you would think would cause that line to execute, does not.
This one executes because it's checked first.
Now, if we just moved this code, took this code and
we moved it down here, then it would make more sense, okay?
Oops.
If we moved it down there, it would make more sense.
But basically, the answer to these is, change color, this one won't
execute, and this one will never execute for any value, so there's the answer.
Okay, so we're almost done with conditionals.