Hello everybody and welcome back to this tutorial. And right now we are going to
take a look at a different loop which is called the while true loop,
or while loop. Basically, there are different types of this loop, whether
they are infinite or not. We are going to cover both of them in this tutorial. I
will show you different examples for this actual loop, how you can use it, what
you can do with it, and where you can use it as well. So open up your IDLE right
here, let us open it up. Let us try to code it right here. If it gives us a
syntax error we will just open up a real program, or notepad, or something like
that, and we will code the program there. So, first of all, the while loop is most
likely always going to be used with the boolean values. There are a bunch of
these, you will see in different programs that there are a bunch of while true loops.
Basically, a while true loop is a loop that is most likely going to be infinite
until the value is changed, or until the loop is broken out of. Now what I mean by
that is, basically, if you type while True, so while True:, let me just show you, and
add two dots, and you just type your print('Hello'). Let's imagine what this will
actually do. So while True, and true is always true, you can't change true to be
equal to false, it will print hello. If I press here ENTER you can see it will
print the hello string forever, which we don't really want. So let us control + c this. As
you can see if you control + c it will print out keyboard interrupt, which we
actually want to do. If you didn't do this, this would run forever. So, same as
that you can just type here something like, while True:, or let's do
something like this. So, i will be equal,
let me find equal, okay, i will be equal to 0. So we set a variable i to have a
value of 0. Then what we will do is we are going to do something this, so while
True:
let us increase i by 1. So i = i + 1. You can see nothing is really
happening, there is nothing being printed out, but
we are also not getting these three arrows prompted back to us. That is why
this is an actual while true loop and it will perform this forever, until we
actually perform a keyboard interrupt to this while true loop. It will increase
the i, which we said to be equal to 0, by 1 for every iteration. So what it does
it goes to this line, it performs this line, it sees that there is not another
line in the actual loop, it goes to the beginning, it performs this
line again, and it does that forever. So if I control + c this and just type here
prints(i), you can see that the i now has a value or something around,
I believe, 36 million, or even more, 364 million? If I can count the digits well
enough? Probably can, but doesn't even matter. So you can see the usage of a
while true loop. Now I will show you in the next tutorial how you can actually
escape a while true loop, even though it is infinite, without
actually closing the program itself. So there is an actual statement or word that
we can use that will help us do that. But for now on, let us check out what
else we can do with the while loop. So, it doesn't have to be a while true loop,
it can also be a while false loop. But then if you just type here something
like this, while False:, i = 0, and you just press here enter, you will see
that nothing will get executed because while False is always false and it will
not execute this loop. Therefore, this actual loop has no meaning at all.
So, now that we checked out the while loop with the boolean values, let us
check out the while with normal values. So we're going to set our i
variable back to 0, so it doesn't have this huge value to it,
we will set it to be equal to 0. And
what we are going to do is
while i < 100: for example, so let's do it like that.
Then add the two dots, let's see what we can actually do here. So
we can just do something like this, so print('i'), let me
just add the single quotes, so 'i is still smaller than 100'. And then we can
add two spaces and add the actual string of i, str(i), to the actual
statement right here. And close another bracket because we opened double brackets, so
we also need to close double brackets. And also if we just run this, this will
also become an infinite loop because we are not increasing i anywhere. If I just
simply run this it will print a statement forever. What we want to do so
it doesn't do that, we want to increase i by 10. So let's just do i = i
+ 10. And if I run this, you can see it finishes relatively fast. It says
i is still smaller than 100, and then it prints i right next to it. So we can see
that i was smaller than 100 up to the 90, then in the next iteration what this did
is it checked out, or let me just go from the beginning. So, first of all, i was 0.
Then the loop checked weather i was smaller than 100, it found out that i indeed is
smaller than 100, and then it proceeded to execute the code in the while loop. It
printed out this statement right here, and then it increased i by 10. Then
it performed that until the 90. Then you can see that it checked out whether
90 is smaller than 100, it printed out 90 is smaller than 100, and then it
increased i by 10. Then in the next loop it checked out whether 100 is
smaller than 100. It found out that 100 indeed isn't smaller than 100, and then
it didn't proceed with the next statement in the while loop. But let me
show you how you can actually change the output of this just by switching these
two lines right here. Let me just think about this...maybe it is not right here. If
90 is smaller than 100...yeah, let me show you like this. So what we will get
right here is we will not have this statement right here. Instead of this
statement, we will have a hundred statement, which will not be correct.
Therefore, be careful which line you write first. So let me just show you. If
I only switch these two lines one with another, this statement will become
different. So if i is smaller, first of all let me set i to be equal to 0, and
then if i is smaller than 100, pardon me, while i smaller than 100. And then,
first of all, we will increase i, so i = i + 10, and then we will print(
'i is still smaller than 100!'). We will have to add the string
of the i, so plus str(i) itself,
close double brackets, and press here enter. And you can see we get an
entirely different output right now just by switching these two lines, even though
we perform the same code in both of them. You can see that right here we don't
have this line right here, and we instead of it get the 100 printed out. Which is
not really correct, i is still smaller than 100, and then i is
100. That is not really correct. Therefore, be careful which line you
write first. So first of all write this line, and then
increase it right afterwards so it can go to the next loop. So let me just check
out what else we need to cover. We can also show another example of the while
loop, so let me just show you. It doesn't have to be while i is smaller than
something, it doesn't also have to be while True or False.
First of all, we once again set i to be equal to zero, we will
reset it, and then while i is not equal, so we perform is not equal by specifying
!= right here, so while i !=, for
example, 100...what we want to do is, basically, let's do something like this.
So i = input, so we will prompt the user to input a number, 'Enter any number'.
So this could be something similar to the actual password checker.
We are just doing this in an infinite loop until the user actually specifies
the correct number that's specified in the program. So
I'm going to show you that right after this as well. So enter any number,
and then we can actually, or we don't really need to print anything, let's just
enter it here. So 92, we get prompted once again, 23, 33. And if I just type here 100,
okay, so 100, let us try something
differently. Okay, yeah, we are good, we got a string of 100. You know I
remember I forgot to actually add i equals the integer of i, so let us just
rewrite this real quick. So while i, first of all set i to be equal to 0, while i is
not equal to 100, we want to perform the input function. So i will be equal to
input('Enter any number'). Simple as that. Then we want to make i to
be equal to int(i), or you can actually do that in this line as well. So
you can do something like this integer out of the input, you don't have to do it
in a second line. Let us delete this right here. Now since your first
encountering with something like this, what this will do is it will perform the
function inside first, so it will prompt us to input this, then it will turn into an
integer and store it in i. So let us see how this will work.
Press enter through 23, 33, 44, and then 100, and you can see once you specify 100 it
checks out right here if i is not equal to 100. Then
it says i is equal to 100, therefore it will not perform this. So this is another
useful type of usage for the while true loop, pardon me for the while loop, where
you simply check whether a certain value is not equal to another certain
value. Now let's see how we can actually write this for the password. So, password
will be equal to, for example, let's use something else, so right here we can use
'hello world', so that is our password. Our standard, good ole hello world string. So
right now we want to enter a while true loop, or not a while true. So while, let me
just double check this, first of all. Okay, so we can do something like this, while
inputted_password is not equal to the password, we want to
prompt the user, so inputted_password equals input enter
password. So let me just try this right here, inputted_password is not defined, yeah, I
thought so. Let me just go right here inputted_password
equals...well, let's think about this, maybe we can actually approach it with a
different principle, also using the if-else statement. Let me just think
about it. Okay, let me first of all try it like this, enter password, simple as that.
And then I enter the actual while true loop. So let me just do it like this, so
hello will be our password, and right now while inputted_underscore
password is not equal to password, inputted_password will be
equal to another input. I'm just checking something out, whether this will work or
not, not really sure. We'll have to figure it out...inputted_password is not defined.
Yeah, because we actually called it input_password, not inputted_password, so let
me just fix that real quick. So, input_password is not equal to password, input
underscore password equals input. I'm really curious whether this will work, we
are going to have to check it out. Okay, so enter password, we are right here
getting prompted to enter the password. Let's input 123, hello, and then
just world, password. And if we just specify the correct password, which
is helloworld I believe, we get exited out of this program, which means we
specified the password correctly. So that is good. So, basically, that is all we
actually have about the while loop. Of course, we are going to use it and abuse
it later on, of course, in the actual real programs that we are going to code. For now
this is just some of the basics. So, I hope you enjoyed this tutorial and I
hope I will see you in the next one. Bye!