Hello, everybody.
Sorry if I scared you, just checking your alertness.
All right, now we know our way in loops.
We can all say that, right, because we have been over lots of examples in force for loops and wire
loops and seen examples and seen homework and done the homework.
And we've been through a lot.
And this path, we've been through a lot, really, it wasn't easy, but now it's time to be professionals
and to be professionals, you know, to to you get to know the hacks of coding and to know some cool
tricks in programming.
All right.
So in Python, we have sort of flexibility of the code.
That's why we call Python, because it's flexible.
And let's see here a few tricks that we can apply upon while loops.
All right.
So stopping a while loop with a break, huh?
All right.
So up until now, we've seen that we can stop while loops only by having a false statement inside the
condition.
All right.
This is the only way that we've seen in previous videos.
We can stop it by having a false condition inside the wire loop.
But this that we already know.
But what if there is an additional way to stop a while loop without mentioning and without addressing
the condition inside the whale?
So there is a way to stop a while, even though the condition is true.
We can use our friend the break.
All right, we have encountered the break, if I recall, in collections, and we can also apply here
inside while loops.
And how can we apply that?
We'll see it in a few seconds.
All right.
So we have an example, which is based on the previous example, because I don't want to make things
harder than they are.
So it's the same example from the previous two videos.
And let's see how we can apply the break here and how can we stop the while loop, even though even
though the condition here is keeps being true.
All right.
So let's see, how can we do it?
So basically here in the code below, we again have the number equals zero, which is the counter.
We had the condition while number smaller than 10.
This is the while when its condition and here inside the while loop inside the bulk of code.
Here in the dots, we added we added an additional item.
We have an if and the if statement says if no equals two, then break.
So break will stop the loop once number equals two.
So let's go over the flow.
We're starting the code we're having here.
Number equals zero.
We go inside the wire loop, it's zero.
We get to the if the if is ignored because number is zero at the moment.
So we just print it and grow the number by one.
All right.
From this point we go again to the top and now we have no equals one again going downwards.
This statement is still still ignored.
And again, we're growing the number by one and now we have no equals two.
So two is smaller than ten.
Still true.
So we go inside the wire loop going inside and then we get to the if and now we have if no number equals
two.
So two equals equals two.
This statement is true.
And then we go inside and right here we have the break key word.
And basically what happens now is that the code exits the wire loop completely and goes further down
the code.
So at this point, the wire loop is executed by the code and we ignore it.
We do not do the iterations again and again up until we get to ten, which is in two, which is exit
the wire loop completely because we have a break here.
This is the whole point.
So the break basically can overcome the the condition that we have here in the wild condition.
The break is stronger than the condition on top.
And this is how can we exit?
This is how we can stop a while.
Loop with a break.
All right.
From here, I think we should see an additional example.
Again, a friend from the best seems break.
We have a friend from the base here, which is else.
OK, so now let's see, how can we use while with else?
And it's a pretty logical thing to do.
All right.
It's a pretty logical thing to do because let me just go to the slide from the previous lecture.
So this is the slide from previous lecture.
So here what we're doing, by the way, is the same Codrea, the example.
This one is only with if and break, but it's the same numbers, same everything here.
OK, so let's have a quick look over here and the previous lectures, previous lectures, example here.
And we can see here that the the number grows by one again and again, right up until the number goes
straight up to be what?
To be nine.
All right.
And after the number is nine, we also print it and then we go again.
And I was false.
Now, ten smaller than ten is a false.
So what basically happens the code, because it's the while loop completely and runs whatever comes
after the while.
But what if I wanted to tell the code, don't just go outside the wire loop, do what I tell you to
do.
I want you to do to run something specifically.
I want you to maybe tell me that we outside the wire loop.
Right.
So let's see an example here for what we just discussed.
All right.
So, by the way, my thing in teaching is explaining the same thing in different different types of
views, two or three times up until all the students get the hang of it.
Even the the ones that have some topics, they are strong in in some topics they have difficulties in.
So I'm explaining the same thing in different perspective.
So if you get the hang of it, just straightforward lecture and those of you that have a bit of trouble.
So I'll go over what I just said again.
And let's see the physical example of what we just discussed, so in the past, we used if and else
and it worked perfectly together, but we can use the alst together with wire loop as well as we just
talked about.
We will tell the code instead of exiting the while loop.
Once the condition is false, just go straight ahead to the else.
Same as we had in its statements once.
If a condition is not met.
So go to the elf's, go to the default exit point.
So here we also have a default exit point, but this time it's of that while loop.
So let's see the this is the example.
It's right here what I've just marked and let's see some two bullets regarding what it's going to do.
All right.
So add plus one to the number while it's smaller than five.
So this is the ball right here.
We have a no no equals one.
And inside the wire, which spread the number and add plus one to it.
OK, this is the first part.
The second part is once the condition is false, go to the else and multiply the number by hundred,
then print it.
OK, so we going for one here, first iteration, then we have to second iteration three, four.
And then once we have the number equals five here.
Five is not smaller than five.
This statement is false.
Don't just exit the wire loop, go straight to the LS and and execute this gold right here.
OK, so once we go to the house number is multiplied by 100 and number is no longer less than five.
All right.
And the output will be is following.
We have one to three and once the number is is bigger than five, then we're going to execute this line
right here, which is this print.
Yeah.
And we also execute the multiply by a hundred and our number as five.
So this would be five hundred.
Have a small mistake here.
All right.
So this will be the output.
And I think it's pretty simple.
We also have here default exit point, same as we had for if statements.
And so we have learned several tricks applied on wire loops.
We have seen here that if and break inside.
So we could actually while as we wish and also we have seen an example of how can we use an AL statement
in order to execute the code that we want once the wire loop will be false here.
All right.
These two examples cover the advanced part of wire loops.
I think that we will be meeting in the next video where we go over the assignments and your homework,
basically.
And there'll be a bit challenging, I'm telling you, in advance.
But I think you could get the hang of it because you don't have any material that he's not in the slides.
So all the assignments, you can resolve them with the information that we have taught together that
we have learned together.
And I think that will be just fine.
Thank you.
And I will see you in the next video.