Before we round up the section on loops and flow control,
I want us to take a look at one more
statement and that's going to be the continue statement.
So far we've been working with the break statement and this is of course,
used to end the loop once a particular condition has been met.
But what if for one reason or the other,
we simply wanted to skip a particular iteration or a part of a loop?
We don't want to end the loop
exactly. We just want to skip a particular step.
This is where continue comes into play.
So let me give you an example, let me just remove
this code
and let's say for number in
an hour range, one
comma six.
OK.
Now you're probably wondering what exactly is range range here is simply a,
an inbuilt function
within Python
that will generate numbers between a set in our range.
So in this case, right now, we're saying numbers between one and six.
However, please do keep in mind that when you're working with the range function,
uh the first,
the first number here, which is one will be inclusive
while the last number here is which is six
will not be inclusive inclusive to be exclusive.
So
if I was to simply say, ok, just simply print
and then in brackets,
just print the number
you will see right now that we're gonna get 12345. Ok, so please do keep this in mind.
Now,
the function by default will print 12345, right?
But what if we wanted to skip three in this case? OK. So
we want to forgot to print 124 and then five, right?
So
how are we going to do this?
This is where the continue statement is going to come into play.
But first of all, I need to create the if statement to check
if the number
is equal to three, right?
So now I'm saying, OK, if it's equal to three,
just continue,
I don't need to add the L statement in here because
when you're working with the continue statement, it is understood.
So Python will simply say, OK,
once I get to three, I'm going to skip it out,
I'm gonna skip three and then simply continue with the rest of the loop.
So now if I was to run the program, now you can see it is 124
and then five.
So that's basically how they continue. Our statement would work.
Let me give you another very quick example. What if
I extended the range here from one to let's say 11
OK.
But now we wanted to check for numbers that are even
and then simply keep those even numbers. We only want to print out the odd numbers.
So again, how are we going to do this? Let's first of all, check to see
if it's divisi
divisible by two. I'm going to say percentage and then,
or two
right here
equals to what equals to zero at
my colon.
And now I'm simply going to say just continue
and then print number.
So now if I was to run the program, now you can see it's simply 1357 and nine,
that's how the continue statement actually works.
Let me just spend one minute to talk a little bit further about the range function.
There is one more thing
that you should be aware of.
The thing about the range function is that it can actually accept three values.
So as an example, let me just add my hashtag
so for example, with range,
you can have something like one,
OK? 10
and then two.
OK? So the function, the range function can accept three different parameters.
All right.
So if you wonder, OK, what exactly are these going to be? Well,
the very first number in here will represent the start value, which is one,
the second one here will represent the stop value, which is going to be 10.
While the third value right here will represent two,
which will be the steps that must be skipped.
So by default,
let me just show you how this work right now. If I was to go come over here right now
and then say 110
or two,
just as an example, let me just remove the continue statement in here.
OK?
If I, if I was to go ahead now, right now, I'm printing these numbers,
you will see
that it is now 1357 and nine. Why? Because we are skipping every two,
this step is gonna be two.
So it's gonna go to from one, it's gonna skip to three,
it's gonna skip to five and so on.
If I was to change this one right now to three,
can you guess what the values are going to be?
Now? It's going to be 147.
So if I run the program,
there you go 147.
So you may or may not work with the range function in the future.
But I just feel it's important for you to at least understand
is that the range function can accept up to three different parameters.
The first value in here being the start, second being the stop number. And then the
uh last valuing had been the step number. And keep in mind
that the stop value is always exclusive.
It's not going to be included in the
actual results of that particular range function.
Final thing to mention, final thing to mention,
you may also come across a particular statement called the pass statement.
It just goes like this, right?
You you sort of continue, for example, you will see a pass,
right
pass doesn't really do anything.
Honestly,
it's really used for passing comments. Maybe the
developer of the program doesn't want to execute something
yet but wants to leave like a note.
That's what the past statement is mostly used for.
We're not going to be working with it at all.
So for example, in here, I could say something like OK pass and then
I can add my hashtag
and I can say I will add the function
uh later.
OK? Just as an example, right? So let me just
go ahead
and run the program
and you can see right now the program still works. Uh anyway, right?
So it's not, it's not gonna,
it doesn't really do anything as such. So
uh that's pretty much it.
Thank you for watching the video.
I will see you in the next class.