[no audio]
Friends here we are going to discuss
about Loop Control statements.
See, basically we have two types of Loops in your Python,
right? They are For Loop and While loop. Now we know that
loops are going to repeat for 'n' number of times based on
your condition.
Sometimes one time, sometimes three times,
sometimes hundred times,
sometimes even infinity times you are going to repeat
your logic or you're going to iterate some 'n' number of times.
Let's say a very simple example, suppose if I have a list
consists of, let me take simply some four values, then it is
going to repeat now four times,
and you're going to get numbers 0, 1, 2, 3, 4 times.
So instead of this directly you can write some list also, right?
[no audio]
That's it. Now, let me run it.
Now you're going to repeat, or you are going to iterate, or
you're going to execute this block, how many number of times?
1, 2, 3, 4, 5. Because you have 5 values in your list,
then your loop is going to repeat this block for 5 times,
or your loop is going to iterate this block for 5 times.
Right. That's fine.
But now we have some control statements for your loops means,
to control the iteration part of your loops.
They are like 'break', 'continue'.
Right. Let me explain this here.
So what I am doing is,
[no audio]
see, let me do simply one thing.
I am writing very first line itself
simply 'break', now see the result.
You're not getting any output.
The reason is, first of all 'break' is useful to stop your
iterations in your loop.
Whenever 'break' is encountered, right, simply your Python will
stop the loop part. Suppose
this line is outside of your loop, don't get confused. Stopping
the script is different, stopping the loop is different.
See we know, if you remember somewhere
we imported a module called 'sys',
and then if you remember 'sys.exit' is going to stop your script.
Sorry. Let me save it and run it.
[no audio]
See suppose I am writing here 'print(
"before exit")',
then 'print(
"after
[no audio]
exit")',
then let me write one more line,
this is 'print("after loop")'.
Now see the result.
You are getting, "before exit" only. That means your script is executing
only up to this. In third line
we have 'sys.exit'. Because of that you are going to stop
your entire Python script. Be clear, stopping script is different
stopping loop is different, right. Now,
let me comment or anyway, we don't want this, right,
let me remove all these lines.
[no audio]
Now see the result. Because of 'break' what is happening?
Simply your loop is going to stop. Actually, your loop has to repeat
5 times.
It has to iterate for 5 times.
But because of 'break', in the first iteration itself
your loop is going to stop. Whenever loop is going to stop
even your, then at that time
you're not going to execute any number of lines in your loop.
So, because of this 'break' your flow of execution part will
come out from loop. Then after Loop what you have? Simply
you are printing something, "after loop",
that's what you are executing.
Otherwise, let me write a 'break' after 'print' statement.
[no audio]
Now see the result. We are getting 3. Why? Before 'break' you have
'print(each)'.
So at least for first value you are printing. After printing
your first value or after executing your first iteration,
you have immediately 'break'. Whenever you have a 'break', actually
if you don't have break what will happen?
Your Python will take this 4 value, and then it'll print,
then it'll take 56, and it will print, it will take 7 and it
will print, then it'll take 8, and it will print. That means
it has to iterate for certain number of times. But because
of 'break' your flow of control instead of going for next value
it is coming out from your loop. That's it.
That is the usage of 'break'.
Suppose my intention is, first let me print all your values.
You're getting, right. Whenever if I get 56 number, then I
don't want to print remaining numbers. Whenever if I get a
number called 56 while printing your values, after the number,
after the number what you have? 7 and 8. Means it
has to iterate for two more times, but I don't want to iterate
whenever if my number is equals to, if my number is equals
to 56 at that time, after that
I don't want to execute my loop. Then simply use 'break'. Now,
see the result. After getting 56 you are not getting 7
and 8, because you are stopping your loop whenever if
you get number as 56, that's it.
[no audio]
Right. Just assume that
you're going to get some thousands of paths list, list of
paths you have in your hand. Somewhere in some path, somewhere
in some path
you have your required path.
Once if you are able to find your required path, then I don't
want to execute the remaining iterations for my loop.
Let me simply take in this way so that it will be very clear.
[no audio]
Just assume that I am writing a simple Python script to identify
all the paths, and assume that I got all the paths in my host
Right. Let me take suppose '/usr/bin'.
then, '/usr/
bin/httpd’,
something, some '/home
/users/xyz/weblogic/config.xml',
something like that.
Right. Now,
[no audio]
I need to find out the path in which my 'httpd' is
there. That is my intention. Then suppose, first of all to take
one by one path, 'for each_path in paths',
then I can print simply in this way, my 'each_path' I can print.
But I want to print the path which consists of 'httpd'.
[no audio]
Right. See that 'if 'httpd' is there, 'httpd', if it is there
in your path,
then I am printing.
[no audio]
It's printing.
But your loop is not stopping.
It is taking somewhere second iteration and in the second iteration
your condition is satisfied,
then you are printing. Anyway, because this is for loop it
is going to take next path and it is checking. Anyway, in
this it is not there, 'httpd',
that's why you're not getting any output.
But still your Python is trying to take remaining iteration
values, remaining values from your list, but my intention
is once if I get my path, I mean the path which consists
of 'httpd', then I don't want to iterate
remaining, for remaining paths.
I don't want to execute my loop for remaining paths.
Simply I want to stop. Because I got my required path
why should I iterate for remaining paths?
Why should I waste unnecessarily my execution time?
That's why what I am doing is, simply I am stopping.
Right. See if I don't use 'break', just for your understanding
I am writing, 'print(
"Now working on", I'm printing 'each_path'.
See the result. Working on first path.
Anyway, in this 'httpd' is not there
that's why you are not printing your path.
Now working on '/usr/bin/httpd'. In this 'httpd' is there
that's why you are printing that. So now you got your required
path. Why should we go and take next path?
I don't want to get, but your Python is taking no, because
this is a loop it is going to take all the paths,
I mean whatever the list of values you have.
But now my requirement is, once if I get my required path
then I want to stop forcefully my loop.
Yes, you can stop it in this way. That's it. Now see that.
Once if you print your 'each_path', then you are stopping, right. Now,
let me write something outside of your loop.
"outside of for loop". Now, you're getting "outside of for loop"
line. After printing your 'httpd' immediately
it is stopping because 'break' is there. Whenever 'break' is encountered
simply your loop will stop, not only your for loop, even it
will work even for your while loop as well.
Let me take a simple example in while loop.
[no audio]
Suppose I am doing
in this way, 'while True'. You know, this is an infinite Loop,
right? So initially I am taking some count, 'cnt=1',
and I am printing my 'cnt'. After 'cnt'
I'm increasing my 'cnt', 'cnt=cnt+1'.
[no audio]
Yes, you're going to get infinite numbers, but my intention
is in case if I get, if I print number I mean up to 100, once
I reach 100, then I want to stop my while loop. Then
what I am doing is 'if cnt == 100' stop your
while. That's it. 'break'.
See that
once 100 is reached you are going to stop forcefully
your loop. Stopping loop is nothing but stopping
the next iterations.
You are stopping. Instead of going for next
iteration simply you are stopping your loop. That's it.
So that is the use case of your simply,
I mean about the 'break'. Now, let me go with 'continue'.
[no audio]
So try to understand 'continue'. It is somewhat confusing, but
you need to understand it.
Anyway, while going forward we'll use this 'continue' and 'break'
in different scenarios so that you will be good.
But at least for time being just try to understand what is
the logic behind your 'continue'. See suppose
what I am doing is,
[no audio]
let me take
[no audio]
a range of 10 numbers,
then I am printing all the numbers,
first of all. Yes, you are getting from 1 to 9, right, 0 to 9,
but I want to get suppose 1 to 10.
But no problem, you can take anything.
Let me take 1 to 11 so that you are going to get suppose 1 to 10.
[no audio]
Now, I want to print the numbers 1 to 10 except 7 number,
then I can write in this way
first of all. If number is, whatever the number, if that is
not equal to 7, then I am printing. That's fine.
[no audio]
This is one way to write your logic.
[no audio]
Right. Now, you are not getting your number 7, right?
One more way is, first of all simply I'm writing
'print("printing your number")'.
[no audio]
Then, guys be clear,
[no audio]
I am going to write your 'print(each)' outside of your 'if' condition
see that.
"printing your number". "printing your number".
[no audio]
Right.
First of all, your 'print' statement is outside of your
'if' condition. Now, instead of this line simply
I'm writing 'continue'.
[no audio]
If number = 7,
I'm writing simply 'continue'. Now see the result.
We are not getting 7 no. Of course,
your previous logic is perfectly correct.
If number is, I mean, if each != 7 then only print
it. That is correct.
But at the same time, you can also write your logic in this
way. What will happen if you write 'continue' keyword. See whenever
'continue' is executing by your Python, then in that iteration
the remaining logic whatever you have
in your block that is going to simply skip.
[no audio]
Not only this line, even if you have some hundred lines
here, after 'continue', see here also, here itself
I have a 'continue', after 'continue' suppose
"this is the line inside of your if condition
after continue keyword".
[no audio]
See the result. Nowhere you are not, nowhere
you are getting that line.
The reason is,
let me take this,
see after 'continue', in your for loop block even if you
have some thousands of lines all are going to skip. Now your
Python will go for next iteration.
Actually, it is going to repeat for 10 times, right.
(1,11) means 10 numbers you are going to get. Once your
number = 7, if it is != 7, you are not
executing 'continue', then simply you're printing. But if it is = 7
what will happen? 'continue' is executing. Whenever
'continue' is executing the remaining lines which you have in
your block, they are going to simply mask.
They're going to skip. That's it.
For time being guys
don't worry much about your 'continue' keyword, simply assume
that whenever 'continue' line is executing by your Python,
then the remaining lines which are there after 'continue' keyword,
they are going to skip.
So while writing some real-time scripts
you will get multiple situations, there
you will understand that why we need this 'continue' keyword.
Up to that you have to wait.
For time being just assume that 'continue' is going to skip the
remaining lines in that iteration.
[no audio]
Right. Okay. These two are your loop control statements,
I mean 'break' and 'continue'. Apart from that
you are also having simply 'pass' keyword, or 'pass' statement.
What is this?
See not only for your loops,
it is also helpful for your functions, classes, and then 'if' condition.
First let me write something called 'if True' then
'print("ok")'. Now see the result.
This is fine, right?
But what I'm doing is, I am removing this line. 'if True:' I am writing.
Now you're getting an error.
The reason is, we know that whenever you have a 'if'
condition you should have some block inside of that, right,
at least one line, but for time being I don't want to write
anything. Of course, I don't know what I have to write
as of now. But I don't want to get an error, then simply write
'pass'. It won't do anything. Simply it will avoid,
or it will, simply it will make one block but it won't do anything.
That's it. The same way
[no audio]
let me take some simply loop, 'for each in', simply range of
anything you can take, right. Now.
I'm running. Again
you are getting an error because for loop, right, loop means
it has to iterate certain number of times, certain number
of times some code, or block, or some set of statements,
but we don't have anything
no. That's why it is giving an error.
So as of now, I don't know what I have to write but I need
a loop here. Just to avoid an error you can
use your 'pass' keyword. That's it.
[no audio]
Right. Okay guys, thank you for watching this video. This mainly
guys, mainly you need to understand about
'break' and 'continue', right?
Anyway, 'break' is clear, but maybe some people will have confusion
on 'continue'. But you will get clarified while going forward. Right.
Okay, Bye.
[no audio]