Hello everybody and welcome back.
And in this video right now we will cover the while loop in our python programs.
So we already covered two loops which is the for loop and if else loop statement right now what we want
to do is cover the while loop which is a loop that we will use.
It is mostly used in order to perform actions forever.
For example if you just type here while x x equals one and then do some action it will perform those
actions forever In Case X really is one in case it is not one it will not perform anything.
So let us actually start by as always opening up our Python interpreter and these syntax is similar
for the four and a half loop.
So basically you just type while and then some statement and then two dots and then you basically present
through then tab the next command and you specify what you want to do if or while that statement is
true.
So let's see simple use also a equals three
while a equals three.
So double times equals since that is actually changing the the checking the value of eight with debit
print Hello And if you enter this you will see that this will print Hello forever.
Now why this will print Hello forever.
Well basically because a is always three we specified eight to be three.
So let me just close this.
So control C2 interrupts as we can see keyboard we interrupt what we actually type read here first of
all a while three then we type here while a equals equals three and then we set print Hello.
It printed Hello forever and it would basically go forever.
In case I didn't stop it because A will always be three but if we just type here A equals
Whoops
whoops can find equal equals two and then we pressed once again you will see that this actually or wait
I forgot to print.
So a equals three while a equals three A equals two print eight and you can see it only printed a once
in indeed it go forever as in the previous video or parliament as in the previous example.
What this basically means is as we can see right here a set of three.
So while a plus three we would print a but since at the at the In Our while loop we specified a to b
to it didn't really print a forever since in the next loop when it came here it was checking if a equals
three but eight wasn't three then it was two then.
So it basically broke the loop and it only printed out a to b to one since it only went through this
entire go once.
So that is the simple use of the while loop you can also just type it like this.
So set up boolean value for K which is either true or false.
Right.
Now let's say true and then while a Brent hello.
Now you can guess that this will bring Hello forever since it basically says while true print pillow
as we can see if I press your entry it will print Hello forever.
You can also do it basically while true.
Whoops I didn't type hear anything print.
It's true.
And they will also print it's true forever.
You can also specify a to be false.
And then while a which means while false print is false and you will see this won't print anything since
A is false and this will not go through a loop it will only go through.
It's weave this spaceflight statement is true if it is not true it will skip the loop.
So that is a good thing to know it can be useful for many things.
Now let's see a combination for example of this slope and a different slope that we used before which
is the if else statement.
So let's call this and write our next program and nano non nano Python for pi.
We specified the path the python user in Python.
And now we can write our program.
So let's set some value x to be true.
So we set the value x to be true.
Nothing really there to explain.
Now let's start our while loop while x.
So while x then two dots we tab the next commander we will specify the right here.
So basically already now we know that it will execute the commands in while loop since x is true.
So it will not keep the X.
The while loop.
So right now let's specify something called user input for example or user name and then we equal that
and use the raw input.
We do not need to specify anything before.
We only specify when we want to use an integer.
So right now we want to get a string from the user so raw input enter name here and we close on the
one time parentheses and let's use now.
And if l slope so if it's user name equals equals to Q which basically in our case will stand for quit
or let's actually.
Well let's leave it on Q So and then we specify two dots break we will type your break.
Now the break command basically breaks out of the loop equal in our case closed the entire program since
our entire program is a simple one.
While loop but e the use it or break as I said is only used to break out of a wall of a loop it can
be used in our infinite loop so such as this one until you specify Q it will work so let's continue
this program.
So if user name equals Q You'll break out of this loop else we can just type here print shallow and
then we specify the S for the string and then specify the percent S for user name and this will print
out shallow and then the name for every name we type until we specify Q Now if we specify Q It will
break out of the loop and close our program.
So let's see how this will work.
So we control 0 to save control X to exit we see h modded to be an executable and then we can run it
as we can see pops as you can see it asks us for names so let's say first name John.
Hello John.
And it will also write after death asks us for another name.
We can specify here Jack and it will say hello Jack.
And now this will do basically this forever until we type here.
Q And if we tap your Q You can see it will close our program since we said if the name equals Q You'll
break out of the loop.
So that is another useful thing to do.
As you saw right here there wouldn't really be another way to close this program properly if there wasn't
a statement for a simple command to break out of the loop.
So that would be about it for the basics of while loop.
For now we covered all of these three loops which is for if and a failed statement and while loop and
in the next video we will go into something called lists which we already saw in the previous video
but we didn't really explain it there.
Well right now we want to explain this in a little bit more detailed so that would be about it.
For this tutorial and I whole pursuing the next one by.