In this tutorial we will talk about the while loop a while loop is used to iterate over a block of code
repeatedly until a given condition returns funds.
And the main difference between the while loop and the for loop the do while loop.
We are not certain of the number of times the loop requires execution on the other hand the default
low.
We know exactly how many times we need to run the loop and this is the syntax for the while loop.
While condition and we have a set of statements in the body.
So here is an example of a while loop the sine five to debatable a and using the while loop.
I'm gonna check this condition if A is greater than zero.
In that case the control goes through into the body or the Y loop but in S E and then the next line
we're going to decrement the value of a by 1.
The while loop get executed until this condition is found the while loop first prints out 5 and then
four three two one that this is the output of the Y loop when the value of A is 0.
Since 0 is none greater than 0 it comes out of the Y loop so the program first checks a given condition
and if the condition returns filed the lupus terminated and the control jumps to the next statement
and the program after the loop.
So in the Jupiter note book I have executed the example that I have just demonstrated and this is the
output.
Next is the infinite loop a loop becomes infinite loop.
If a condition never becomes false and you have to be careful while using a while loop because if you
forget to incremental count the wearable in Python or right.
Flawed logic the condition may never become false.
In that case the loop and run in financially and the conditions after the loop will start to consider
the same example that we have discussed the while loop.
If I remove this particular line in the y loop A will always be five and it will always be greater than
zero.
And the program keeps printing the value of a
so the condition never becomes false.
And this result in a loop that never ends.
So in my JUPITER KNOWN book If I comment on this line and execute this piece of code as you can see
here the program is in an infinite loop to stop execution go to kernel and then choose interrupt not
to stop execution.
Next the else statement for while loop in a previous tutorials we have talked about using the L statement
with the for loop and when we use the L statement with the for loop the L statement is executed when
the for loop has exhausted ie iterating when we use an L statement with a while loop the L statement
is executed then the condition in the while loop becomes false and the L statement is optional and it
get executed if the loop i.e. iteration complete normally but not a word if the for loop or the Y loop
does not end normally such as if we use a break statement or a written statement in that case the control
will not go to the end clause.
Consider this example we have assigned devalue Phi to the variable E and D while loop checks if A is
greater than 0.
If it is greater than 0 it prints out a decrement the value of a by 1 and then continues the loop until
the condition is false.
We have used the L statement with along with the while loop and after only when the while loop complete.
Or when the condition becomes false only then the else clause gets executed.
Let's execute this piece of code so as you can see until the condition is false do while loop word execute
it and then finally the else clause is executed.
Now let's consider then that example using the brake statement.
Consider this piece of code we have used an L statement along with the Y loop the Y loop has a condition
that it checks if the value of a is equal to 2.
In that case it breaks the Y loop so let's execute this piece of code.
If you notice the statement in the else clause is not executed this time that is because the while loop
did not end.
Normally it is terminated prematurely because of the brake statement here.
This is about the L statement with while loop.