What I want to do in this video is show you
that for loops aren't the only types of loops you would use,
although, they probably are the most common.
You could also use something like a while loop.
And maybe the while loop might be more intuitive,
at least maybe in the Python context.
You can pick.
So I'm going to write the same program.
It's going to do the exact same thing,
but instead using a for loop, I'm going to use a while loop.
So I'm going to still start with a sum equal to 0.
But I'm also going to start with i equal to 0.
So the while loop isn't going to automatically define
what my i values are.
And it's not going to automatically change
my i values for me.
I'm going to have to do that for myself.
But I want to do the exact same behavior.
And I'm going to leave my old code down here,
so you can compare it a little bit.
But then I'll delete it, because I
don't want the same thing to be run twice.
So I'm going to set i equal to 0.
And this, in case you're curious, the last time,
in the for loop, i is only a valid variable inside the loop.
Now, I'm defining it outside of the loop.
So it's actually a global variable.
I'm defining at the highest level.
It can be used anywhere inside of the program.
And so over here, let's define a while loop.
So while-- so I'm going to keep doing
what's inside of the loop clause.
I'm going to do it while i is less than 10.
Remember, we want to do it for i equals 0, 1, 2, 3, 4, 5, 6, 7,
8, 9.
Everything up to, but not including 10, all of the 0
through 9.
So while i is less than 10.
Do-- what I'm going to put in this clause--
once i is not less than 10, or essentially,
once i is equal to 10, don't do it anymore.
So it's going to have the same behaviors
as this right over here.
And what I'm going to do is I'm going
to say sum is equal to sum plus i.
And then I'm going to print sum.
So this is the exact same logic that I
had when I wrote the for loop.
But I'm going to have to add one more line here,
if I don't want this thing to run forever.
Because in the for loop, you'll remember,
every time we run this clause here, as we get out
of the clause, it'll assign i to the next element in this list
here.
Over here, we haven't defined how i should change.
So what we have to do is in every loop,
we have to increase the value of i.
So we can say i is equal to i plus 1.
And I want you to really think about how these are equivalent,
how both of these are equivalent things.
i's started at 0 before I even entered into this loop.
And then sum started at 0 for either of these.
These say sum is equal to sum plus i.
Well, that's the same print sum.
Now, over here, i will then-- once we go through this loop,
in the for loop-- i will then be assigned
to the next element in this list, which is 1.
Over here, there's nothing to say to do that.
So we're just going to say, i is now
going to be the previous i, which is 0, and add 1 to it.
And these are equivalent, because each
of these elements in this list, in range of 10 that we define
it, are just 1 more than the previous one.
So instead, we could just say, i is
going to be 1 more than it was before.
And so this should produce the exact same result.
And actually, if I don't want to get rid of this code,
if I want it there just for future reference,
but I don't want the interpreter, the Python
interpreter to run it, I could comment them out.
And you comment things out by putting a little hash
sign there.
So a hash sign there, that literally says, hey,
interpreter, ignore all of this stuff.
And it could be useful if you have some code in there
that you want to just not be interpreted for now
and maybe you want to save it for later.
And it's also useful, if you want
to tell someone who's actually reading the code,
if you want to tell a human being who
is reading the code what this code does.
So for example, you could write a little comment here.
What did I do?
What is that thing?
You could write a little comment here that says,
this while loop calculates the sum of 0 through 9.
And you'd say including 9.
So if someone were to come back later, instead
of them having to try to figure out what this says,
they could read your comments, and say, OK.
This part of the code right here calculates the sum of 0 to 9.
And I could say, and places it.
So when you put this hash tag, it
essentially comments out that line.
So if I want to go to another line,
I have to put another hashtag there.
And places it in the variable sum.
So then people would know, I know what it's doing.
And I know that when all is said and done,
that the sum will be placed in this variable called sum.
But anyway, let's run this program, and one,
show that it ignores these things.
If you wrote this text without a hashtag,
the interpreter would say, what is all this?
This isn't Python code.
But this is useful for someone else reading it.
And it will also ignore this down here.
So I can delete this.
Delete that line right over there.
And then let's save it.
And then let's run it.
It gives you the exact same results that we got before.
So let me run it.
So these are the results that we got before.
You might remember, 0, 1, 3, 6, 10, all the way up to 45.
Now let's run this thing, assuming
that I haven't made any bugs.
And I get the exact same thing.
I get the exact same thing.
So this while loop is doing the exact same thing.
It's more explicitly defining i, I guess.
And it takes you a little bit more code to write it.
But it's the exact same behavior.