Narrator: Welcome back.
Now, up until now, we saw the 'break' keyword
that we can use to break out of a loop.
And you see here that we're able to loop with a 'for' loop.
But, with a 'while' loop, as soon as we break, we're done.
We've just, well, broken out of the loop.
But could we do that in a 'for' loop?
Let's find out.
Yep, break keyword, works in a for loop as well.
But, there's actually two other things that
we can use besides 'break'.
One is called 'continue',
and you can see here again, blue highlighting.
If I do 'continue' here, and I click 'run'...
There you go.
We get our looping again.
But, you're thinking, "Why do we need this?"
Like, it seems completely useless, right?
Well, a break, when we use the 'break' statement,
it breaks out of the current and closing loop.
So when we used a 'break' statement,
we broke out of this loop, right?
This whole 'while' loop, we just exited it.
And same with the 'for' loop, we completely exited it.
However, with a 'continue', what we're saying is,
"Hey, whatever happens when you hit this line,
continue onto the top of the enclosing loop."
So the current and closing loop, what is it?
It's this 'for' loop.
So it's going to go back to the for loop.
So that, if I do something like 'print' here,
so that we get it printed twice, or you know what,
let's just move 'print',
down here...
Like that.
And then same here,
we're going to just 'print' at the bottom, after continue.
If I click 'run', nothing gets printed.
Because as soon as it sees continue, it's going to say
"Okay, I'm going to go back to the loop"
and it's going to keep going, keep going,
keep going, until, well, the loop ends.
So that we never actually hit line 10 and line four.
So that becomes really useful in conditional logic as well.
Finally, the third one is called 'pass'.
And pass is, well, not very useful.
It essentially does nothing.
If I click 'run' here,
and again it shows me here line 10, something's wrong.
Well, we have to move print up to the top so that while,
we don't get my list of index of three, which doesn't exist.
So again, going back here, if I click 'run',
pass doesn't do absolutely anything.
It just essentially passes to the next line.
So why is that ever useful?
It's very rare that you'll see 'pass' in your code,
but 'pass' is a good way to have, let's say,
a placeholder while you're coding.
For example, you wanna loop through the 'for' loop,
but we don't know what we wanna do yet, in the code.
Let's say that here, we're still thinking about it.
If I do this and I click 'run',
I get an error here because, whoa, whoa, whoa...
'Expected an indented block.'
Because now, the 'for' Loop or the Python interpreter,
is looking for the next, like,
"What am I supposed to do with the Loop?"
"I'm trying to loop and there's no code for me."
So, you can add a 'pass' here to at least fill something
for the 'for' Loop to say,
"Hey, I'm thinking about it, I'm just gonna pass it for now,
just so you have something so you don't bug me,
and I'll come back to it."
So if I click 'run', that still works.
So 'pass' is one of those placeholders that we can use
so that there is a line of code that
does absolutely nothing, that we can still pass through.
Again, a very rare thing to see in production code,
but while you're developing,
some developers like using 'pass.'
And there you have it.
Break, continue, and pass.
I'll see you in the next one.
Bye bye.