Friends, we know that loops are useful to repeat or iterate
a logic and already we have seen for loop.
Now we have one more loop with your Python that is called while loop.
Let me explain about your while loop by taking simple example.
First of all, I am taking simply 'print(
("welcome to loops")'.
Suppose I want to print this message only one time, then I can
directly write 'print ("Welcome to loops")'.
But I want to print this message 10 times, then what I can do?
One thing is, I can write this statement 10 times,
'print ("Welcome to loops")',
"Welcome to loops", likewise, 10 times I can write. But already
we know for loop, that's why what I am doing is, I am going
to iterate. I am going to iterate this logic for 10 times
'for each in range(10)'.
Not only 'range(10)', directly you can provide a string which
consists of 10 characters, such that once if you run your code
it is going to repeat your, it is going to print your message 10 times
But I want to print this message infinite number of times.
[no audio]
I want to repeat this message for infinite number of times.
Let me modify this for loop with, 'while True'. Now see the
result. So to get more clarity
I'm writing one more line here so that you can understand
whether it is going to repeat it infinite number of times or
not. See that.
It is going to repeat for infinite number of times.
Right. So sometimes you may need to repeat your logic for infinite
number of times.
Let me give you a very simple example. First let me stop it.
So if you write 'False', then it will stop.
[no audio]
That's fine. So first of all, you have to understand that if you want
to repeat a logic for infinite number of times, then you
have to go with while loop.
Right. So guys how it is going
to work? Yeah, let me explain that as well.
[no audio]
See whenever if you run your script with this while loop,
because of while, right, while is a loop,
first of all, this logic will repeat. But how many number of times?
If you write directly 'True' here, then it is going to repeat
for infinite number of times.
Same logic will re-execute for infinite number of times
if you write 'True'. How it is doing means, whenever there
is a while, your Python will check first the value here.
It is a True, right, if it is True
it will execute once. Then again, your Python will come back
to this position and again, it will check.
Anyway, this is a fixed value 'True', that's why again it will execute.
Again our Python will come back to here,
again this is 'True'. It will execute.
Likewise it is 'True' for lifetime
so it is going to repeat for infinite number of times.
That's why this is a infinite loop. Right.
[no audio]
That's fine. Now,
[no audio]
what is the use of infinite loop? Let us assume that you
need to monitor a file system for lifetime.
Then what I will do is, simply 'True'. Here,
I will write the logic to monitor your file system. As of
now, simply I am writing 'print' statement, "Monitoring
[no audio]
file system usage",
just for our clarity
I'm writing one more line.
[no audio]
Now see that,
for every 1 minute, I need to monitor my file system usage,
but for lifetime. Then what I will do? Just I'm importing 'time' module.
[no audio]
Then after every 1 minute, right, but instead of
one minute, what I will do is, as of now I am taking simply
one second. Now for every one second now our script will
monitor your file system usage. In case if it has reached the
threshold value, then you can send mail alerts, right. Now see that.
Once if you run your script and if you leave it, it is going
to monitor your file system for lifetime. That's it.
So this is one of the usage, when you want to repeat your
logic for infinite number of times, right? That's fine.
Now sometimes you don't know how many number of times you
need to repeat your logic, but after a certain number of
times you need to stop.
Means just assume that
[no audio]
I want to print a number then next
time I want to print your number by adding some random number.
I mean first let me write logic so that you can understand.
Let me take initially a 'value'.
let's say some number 4. Then I am taking 'while True'.
Then I am printing your 'value', initial value. Then what is
the next value you want to print? I am taking next 'value +',
some random number I am adding.
Now if I run this, see that it is going to get
some numbers. Right.
Let me stop it.
[no audio]
So in this way if you run, you are going to get your numbers
infinite number of times,
I mean infinite numbers you are going to get. Initially 4, then
460, then 4 something, 900 something, like that you are
going to get. But I want to, I don't want to get all the numbers.
I want to get the numbers only suppose
let me write, if your value
is less than or equal to some, this number.
[no audio]
So here what I am writing,
I am writing a condition such that, previously here you have 'True', right?
[no audio]
So now let me write 'True' first.
[no audio]
You're going to get some numbers, but I don't want to get
all these numbers.
I want to stop this loop once if I reach my value more
than this. Now see the result. In case if your 'value' is
greater than
6789, then you are going to stop it.
Actually, I'm repeating this for infinite number of times, but
I don't want to repeat infinite number of times.
I want to get the numbers which are less than this.
[no audio]
So intentionally you can stop your while loop based on condition as well.
[no audio]
Right. Initially this is the value.
I am printing that value. For my 'value'
I'm adding something and I am then printing, right.
Now to get more clarity, let me take simply
[no audio]
'while True', simply print("hello").
Now it is going to repeat infinite
number of times, but I don't want to print infinite number of times.
Right. I want to print only for suppose four times.
Let me take initially 'cnt=0'.
Now what I am writing is, here I am writing 'cnt' value
if it is
'<=', '<5',
[no audio]
or you can take even maybe '<= 5'.
Let me take initially 'cnt = 1'.
[no audio]
Right. Now what I am doing is I am increasing count value as well.
[no audio]
Now see that you're going to get your output only four five
times, you are going to repeat this logic only for five times.
Then how it is working? Nothing
is there. Simply first you are initializing some condition.
Sorry, I'm sorry some variable, initializing, 'cnt = 1'.
So while, first of all while means, you have to assume that
the lines which are there under your while loop will repeat
for infinite number of times, right?
So how it is going to work?
We know that. 'while' first Python will check this condition
result, 'cnt' is initially 1, right. 1 less than equals to 5
means 'True'. If it is 'True',
your Python will execute these two lines once.
So because of this you're also increasing your 'cnt' value.
Initially it is 1, now after executing first time, for first
iteration your 'cnt' value is becoming 1 + 1, w. Now with
this 2, now 'cnt' is 2 not 1. Your Python is coming
to this position, and is checking this condition, 2 less than
or equal to 5, yes, 'True'.
Then again it will execute this logic. But while executing this
logic now you're making your 'cnt' value from 2 to 2 + 1 = 3.
Then with this 3 your Python will come to here and 3 less
than equals to 5, yes, 'True'. Then it will execute once again.
Now your 'cnt' becomes 4, 3 + 1 = 4.
Then again, your Python will come to here with it, 'cnt' equals
to 4, 4 less than or equal to 5, 'True'. Then it will execute
once again this logic. Now 'cnt' value 5. With this 5 again
your Python will come to this position, 5 less than or equal
to 5, 'True'. 'True' means, again this logic will execute once.
But now 5 + 1 = 6, and 6 less than or equals to 5? No, 'False'.
If it is 'False', now your Python is going to stop your while loop. That's it.
[no audio]
Right. So here we are fixing how many times you want to repeat.
[no audio]
Right. Not only that, see based on other condition,
while adding some number for your initial value, you are
going to get your value more than this
after certain number of times.
You don't know how many times you want to repeat,
but you know the condition. Once our value is more than this
then I want to stop it. So here guys, be clear.
We don't know how many times you are going, after how many
times you are going to get this number we don't know.
But you know that certainly, definitely after certain number of times
your number will be more than this. At that situation
you want to stop it,
Right. So while loop in Python is used to repeat or iterate over
a block of code, as long as the test expression or condition
is true, right?
So that's what we are doing here.
As long as the condition result is true
then it is going to execute this line. That is one case. Second
one is, we use this loop when we don't know beforehand the
number of times to iterate.
That's what with this example. Really
we don't know after how many times you're going to get this,
I mean after how many iterations you are going to get your
number more than this, we don't know.
But we know that because of this calculation somewhere,
somewhere you'll reach your number more than this, at that time
I want to stop it.
So that is logic of while loop.
Right. So guys, be clear.
One thing is to repeat your logic for infinite number of times.
Second thing is to repeat your logic until the condition is true.
And one more thing is, that is the important one, before starting
your logic you don't know how many times you want to iterate your logic,
but based on some condition you can stop it.
So that condition you can use it in your while loop, okay.
Okay guys, thank you for watching this video. While going
forward we will see real-time use case of your while loop
with many examples, okay.
[no audio]