Hello welcome to this video.
In this video I'm going to introduce you to while loop statements in Python while loop statements are
the type of statements that you add to your code.
If you want certain aspects of your code to run continuously based on certain conditions that are true.
One thing you need to be careful of with the while loop is that you must make sure that the condition
will eventually become false if not be called will continue to run and run and it becomes an infinite
loop.
So you need to set it up in a way that the loop can be terminated when the condition becomes false.
You can also use an else clause on L statement inside your while loop so you can wrap the else statement
inside a while.
Look this is what a basic syntax looks like for a while loop.
You basically have the wow and then you set the condition and then you have the code that will execute.
So let's create a while loop statement to 18 an index variable a variable that the while loop will use
to get started.
So I'm going to create a variable and when I call it I and I'm going to give it a value of 1.
So now that I've got the initial variable set this is a variable that the while loop will work with.
So you start the while loop by using the keyword.
Wow.
Then you specify the variable which is I.
So I'm setting the condition.
Now I'm saying while I wish I said to 1 while it is less than 7 I'll add my colon.
Then I will specify what I want to happen.
So this is the code that execute is going to print.
I on the screen now the way the code is at the moment.
If I stop here this will become an infinite loop.
That means it will keep running and it will never stop because as it is now the condition will always
be true because I will always be less than seven.
So what I need to do I need to use an increment operator to keep increasing the value by one.
So each time the loop goes round I wanted to increase it by one until it's becomes less than seven.
So what I need to do here I need to see I which is the variable i need to add plus equals to.
OPERATOR What this operator would do a tool each time the loop goes round.
It will add one to it each time on till this condition becomes false.
So if he keeps going around and adding one eventually this will become less than seven and then the
loop will terminate and jump out of this block.
And that will end the loop.
But if I don't put this bit on line five then the code on line three and four will keep running and
never stop.
It will become an infinite loop.
There is something that you always have to be careful about when you are using a while loop.
You need to make sure that at some point the code will become force.
That is the condition we become.
So what will happen now if I play this code.
It will print out one to six.
I've got my code saved.
I'm just going to play the code so you can see the output.
What should happen this code here on line 4 will print the numbers 1 2 6.
It will not print seven because the condition says as long as while this variable i is less than seven
I wanted to print I saw what this code on low five does each time the loop goes round.
It will add one to it until it gets to six.
It then terminates the loop because the condition no longer becomes true.
So let me play that click play and if you look here we can see inside my console I'm printing the number
one two three four five and six.
It doesn't print seven.
So that is the condition I have set.
As long as i is less than seven I wanted to print i.
So each time it goes round your prints one because that one is initial.
It goes round the loop again.
It repeats and I had one here.
He prints to it does the same thing again.
Each time it goes round it will print the number that has been added to the original value.
So that's basically how the while loop works.
But be careful when you set it up a while loop.
Make sure that it will eventually terminate by evaluating the condition you have set to be false if
you have a wild look.
That condition is true all the time be called will not X will not terminate.
It will keep running and running and running and running.
So you really do not want that.
So that's it for this video on while loop statements.
Thanks for watching.
Bye for now.