Now that you know about while loops,
let's talk about
another kind of loop, for loops.
Here's a for loop I made to praise
the most delicious kind of pizza,
pineapple pizza.
You might think, "I liked while loops,"
"Why should I care about
this new kind of loop?"
Well, soon you will see
that a for loop is a way
to write simple kinds of while loops
in a bit of a shorter way.
So, before we inspect this for loop,
let's go ahead and look at a while loop
which will be a bit more familiar.
We'll get back to this for loop
in a moment.
So, just like you saw
in the intro to while loops,
We start with this variable y,
and we write a message
using that location y.
We say that we want to keep going
as long as y is less than 300
and change y by 40 each time.
Now, just to reinforce each of these,
which should be review,
we can say that "Pineapple pizza is
the best" is an important message,
so we probably want to be
writing it more.
so we should start higher
--so we change that--,
it should go on for longer,
so we make it bigger,
and it should be spaced
closer together --don't you think?--
so that's a little bit better.
Now, let's see how we can change
this while loop into a for loop.
I'm going to comment out this while loop,
just so you can see that it's doing
what I'm saying it's going to do.
And we'll create a for loop
and some space to fill in later.
Something really important,
kind of different for a for loop,
it's not just one thing that goes in here,
it's actually three things.
We tell the computer that
by using these semicolons
to separate the three parts,
we'll have one part here,
one part here, one part here,
and then, the inside of the loop,
just like we had before.
So, what goes in each of these parts?
You can think of it as a start,
or initialization,
then we'll have a semicolon,
then, some sort of instructions
on how long to repeat,
and then we're going to have
some sort of change happening.
So, how does that correspond
with this while loop?
Really concretely, we can say
that the start sets up
this variable y here,
so let's copy and paste that over
to this first part of the for loop.
Similarly, we can say
this middle part is saying
how long we should keep going for
and that goes in the middle.
We say the change happens at the end here,
we'll put that at the end of the for loop.
These three parts always have to occur
in this order in your for loop.
You always have to start by saying
this is what the variable
should start out as.
Here it's 27.
Then you say how long to keep going for.
Repeat as long as it is less than 354.
Then you need to put
how you are going to be changing things.
So here we are going to be changing
by increasing y by 24.
Last, you just have to do
whatever you wanted to do
inside the for loop.
So we'll go ahead
and write that text there.
And there we have have it,
pineapple pizza is the best.
Everybody will know.
Now, let's think a bit more closely about
what's happening with this for loop.
If we wanted to change where it started,
well, we just change the start here.
If we wanted to change where it ended,
we just change this endpoint.
If we wanted to change the spacing,
we would just change the increment number.
We can also change
--just like with the while loop --
what value we have for x.
One thing that can be confusing
about a for loop
is to remember what these semicolons do.
You need to remember that they always need
to be there to separate the 3 parts.
If we don't have them there,
then we are going to get
some weird error messages
about our for loop,
so whenever you see
that just double-check that you have them.
You also may get overenthusiastic
and add an extra one at the end,
but if you just remember that semicolons
are only there to separate the 3 parts,
then we can say that this last semicolon
isn't separating anything,
it's just trailing on there, so we can
get rid of it because we don't need it.
Now, I know you're probably
getting a little bit tired of seeing
these for loops getting converted
into while loops and back,
let's just do it one more time,
so you can see a for loop really is
just another way of writing
a simple kind of a while loop,
and you'll be really confident
in understanding
how to go from a for loop
back into a while loop.
You can do that with any kind
of for loop, not just this one.
The first thing that we do is think about
where to put this first value
that we have inside of our for loop.
Since it's just initializing
this variable,
remember that it has to go outside
not inside the while loop,
and it should go before as well,
because we need to be using it
during our while loop.
Then you think about
where should this condition go,
--that's pretty easy--,
usually the stop condition,
or the repeat until condition,
needs to go inside here,
we're gonna say while y
is less than 313 we'll keep going.
Finally, the change.
We always put the change
at the end of the while loop,
in the loops that we've seen,
so we should go and do that here.
Now, we just need to move this text call
inside, and there we have it.
Comment out this whole loop, and you see
that we made the exact same thing happen.
Hopefully by now, you can see
that this new for loop
isn't actually necessary.
We could go through our entire programming
lives writing loops that looked like this.
But, it is a bit impressive
how much more concise this for loop is.
It makes it a bit more clear,
once you're used to it, what's going on.
How we're starting a variable,
we're saying how long to keep going,
and then changing that variable here.
So, if you really hate for loops,
you don't have to use them ever,
but you should be used to seeing them.
And if they're confusing, just remember
you can convert them back
into a while loop like this.
And if you're feeling adventurous,
try using a for loop in your new program,
or going back to some of your old,
simple while loops,
and converting them
into for loops, if appropriate.
A final note: you cannot always convert
a while loop into a for loop,
only when they're
in a really simple form like this,
where it's really clear
what variable you're starting out with,
how long it's going for,
and how it's changing.