0:00:01.459,0:00:03.043
Now that you know about while loops,
0:00:03.014,0:00:05.242
let's talk about[br]another kind of loop, for loops.
0:00:05.811,0:00:07.397
Here's a for loop I made to praise
0:00:07.398,0:00:10.534
the most delicious kind of pizza,[br]pineapple pizza.
0:00:10.535,0:00:12.965
You might think, "I liked while loops,"
0:00:12.966,0:00:15.134
"Why should I care about[br]this new kind of loop?"
0:00:15.135,0:00:17.333
Well, soon you will see[br]that a for loop is a way
0:00:17.334,0:00:20.634
to write simple kinds of while loops[br]in a bit of a shorter way.
0:00:20.635,0:00:22.693
So, before we inspect this for loop,
0:00:22.694,0:00:26.316
let's go ahead and look at a while loop[br]which will be a bit more familiar.
0:00:26.317,0:00:28.532
We'll get back to this for loop[br]in a moment.
0:00:28.533,0:00:31.266
So, just like you saw[br]in the intro to while loops,
0:00:31.250,0:00:33.584
We start with this variable y,
0:00:33.604,0:00:36.728
and we write a message[br]using that location y.
0:00:36.729,0:00:38.498
We say that we want to keep going
0:00:38.499,0:00:42.767
as long as y is less than 300[br]and change y by 40 each time.
0:00:42.768,0:00:46.668
Now, just to reinforce each of these,[br]which should be review,
0:00:46.669,0:00:50.600
we can say that "Pineapple pizza is[br]the best" is an important message,
0:00:50.601,0:00:52.400
so we probably want to be[br]writing it more.
0:00:52.401,0:00:55.268
so we should start higher[br]--so we change that--,
0:00:55.269,0:00:58.466
it should go on for longer,[br]so we make it bigger,
0:00:58.467,0:01:01.636
and it should be spaced[br]closer together --don't you think?--
0:01:01.637,0:01:03.612
so that's a little bit better.
0:01:03.613,0:01:07.505
Now, let's see how we can change[br]this while loop into a for loop.
0:01:07.506,0:01:09.567
I'm going to comment out this while loop,
0:01:09.568,0:01:14.027
just so you can see that it's doing[br]what I'm saying it's going to do.
0:01:14.028,0:01:19.836
And we'll create a for loop[br]and some space to fill in later.
0:01:19.837,0:01:22.539
Something really important,[br]kind of different for a for loop,
0:01:22.540,0:01:26.377
it's not just one thing that goes in here,[br]it's actually three things.
0:01:26.378,0:01:27.932
We tell the computer that
0:01:27.933,0:01:31.201
by using these semicolons[br]to separate the three parts,
0:01:31.536,0:01:34.473
we'll have one part here,[br]one part here, one part here,
0:01:34.474,0:01:37.331
and then, the inside of the loop,[br]just like we had before.
0:01:37.332,0:01:39.579
So, what goes in each of these parts?
0:01:39.580,0:01:45.866
You can think of it as a start,[br]or initialization,
0:01:45.867,0:01:47.966
then we'll have a semicolon,
0:01:47.967,0:01:50.994
then, some sort of instructions[br]on how long to repeat,
0:01:50.995,0:01:55.967
and then we're going to have[br]some sort of change happening.
0:01:57.174,0:01:59.630
So, how does that correspond[br]with this while loop?
0:01:59.631,0:02:01.074
Really concretely, we can say
0:02:01.075,0:02:04.133
that the start sets up[br]this variable y here,
0:02:04.134,0:02:08.880
so let's copy and paste that over[br]to this first part of the for loop.
0:02:08.881,0:02:13.400
Similarly, we can say[br]this middle part is saying
0:02:13.401,0:02:17.196
how long we should keep going for[br]and that goes in the middle.
0:02:17.197,0:02:24.743
We say the change happens at the end here,[br]we'll put that at the end of the for loop.
0:02:25.698,0:02:29.518
These three parts always have to occur[br]in this order in your for loop.
0:02:29.519,0:02:32.066
You always have to start by saying
0:02:32.067,0:02:34.294
this is what the variable[br]should start out as.
0:02:34.295,0:02:35.574
Here it's 27.
0:02:35.575,0:02:41.562
Then you say how long to keep going for.[br]Repeat as long as it is less than 354.
0:02:41.563,0:02:44.496
Then you need to put[br]how you are going to be changing things.
0:02:44.497,0:02:47.835
So here we are going to be changing[br]by increasing y by 24.
0:02:48.747,0:02:50.944
Last, you just have to do
0:02:50.945,0:02:52.866
whatever you wanted to do[br]inside the for loop.
0:02:52.867,0:02:54.671
So we'll go ahead[br]and write that text there.
0:02:54.671,0:02:57.337
And there we have have it,[br]pineapple pizza is the best.
0:02:57.338,0:02:59.234
Everybody will know.
0:02:59.235,0:03:03.397
Now, let's think a bit more closely about[br]what's happening with this for loop.
0:03:03.398,0:03:07.767
If we wanted to change where it started,[br]well, we just change the start here.
0:03:07.768,0:03:11.166
If we wanted to change where it ended,[br]we just change this endpoint.
0:03:11.167,0:03:16.034
If we wanted to change the spacing,[br]we would just change the increment number.
0:03:17.367,0:03:20.641
We can also change[br]--just like with the while loop --
0:03:20.642,0:03:23.443
what value we have for x.
0:03:28.453,0:03:30.540
One thing that can be confusing[br]about a for loop
0:03:30.866,0:03:32.971
is to remember what these semicolons do.
0:03:32.972,0:03:37.165
You need to remember that they always need[br]to be there to separate the 3 parts.
0:03:37.166,0:03:38.868
If we don't have them there,
0:03:38.869,0:03:41.465
then we are going to get[br]some weird error messages
0:03:41.466,0:03:42.599
about our for loop,
0:03:42.600,0:03:45.634
so whenever you see[br]that just double-check that you have them.
0:03:45.635,0:03:48.980
You also may get overenthusiastic[br]and add an extra one at the end,
0:03:48.981,0:03:53.651
but if you just remember that semicolons[br]are only there to separate the 3 parts,
0:03:53.652,0:03:56.871
then we can say that this last semicolon[br]isn't separating anything,
0:03:56.872,0:04:01.164
it's just trailing on there, so we can[br]get rid of it because we don't need it.
0:04:01.165,0:04:04.328
Now, I know you're probably[br]getting a little bit tired of seeing
0:04:04.329,0:04:06.497
these for loops getting converted[br]into while loops and back,
0:04:06.705,0:04:08.636
let's just do it one more time,
0:04:08.637,0:04:10.557
so you can see a for loop really is
0:04:10.558,0:04:13.286
just another way of writing[br]a simple kind of a while loop,
0:04:13.287,0:04:15.766
and you'll be really confident[br]in understanding
0:04:15.767,0:04:18.498
how to go from a for loop[br]back into a while loop.
0:04:18.801,0:04:22.863
You can do that with any kind[br]of for loop, not just this one.
0:04:22.864,0:04:24.854
The first thing that we do is think about
0:04:24.855,0:04:28.193
where to put this first value[br]that we have inside of our for loop.
0:04:28.704,0:04:31.062
Since it's just initializing[br]this variable,
0:04:31.063,0:04:34.056
remember that it has to go outside[br]not inside the while loop,
0:04:34.057,0:04:35.798
and it should go before as well,
0:04:35.799,0:04:38.365
because we need to be using it[br]during our while loop.
0:04:38.366,0:04:40.631
Then you think about[br]where should this condition go,
0:04:40.632,0:04:41.831
--that's pretty easy--,
0:04:41.831,0:04:45.942
usually the stop condition,[br]or the repeat until condition,
0:04:45.943,0:04:47.567
needs to go inside here,
0:04:47.568,0:04:51.873
we're gonna say while y[br]is less than 313 we'll keep going.
0:04:52.871,0:04:54.326
Finally, the change.
0:04:54.326,0:04:56.567
We always put the change[br]at the end of the while loop,
0:04:56.568,0:04:59.639
in the loops that we've seen,[br]so we should go and do that here.
0:04:59.640,0:05:04.235
Now, we just need to move this text call[br]inside, and there we have it.
0:05:04.696,0:05:09.796
Comment out this whole loop, and you see[br]that we made the exact same thing happen.
0:05:11.556,0:05:13.177
Hopefully by now, you can see
0:05:13.178,0:05:15.678
that this new for loop[br]isn't actually necessary.
0:05:16.132,0:05:20.101
We could go through our entire programming[br]lives writing loops that looked like this.
0:05:20.102,0:05:23.968
But, it is a bit impressive[br]how much more concise this for loop is.
0:05:23.969,0:05:27.604
It makes it a bit more clear,[br]once you're used to it, what's going on.
0:05:27.605,0:05:29.400
How we're starting a variable,
0:05:29.401,0:05:31.091
we're saying how long to keep going,
0:05:31.092,0:05:33.367
and then changing that variable here.
0:05:33.368,0:05:36.729
So, if you really hate for loops,[br]you don't have to use them ever,
0:05:36.730,0:05:38.463
but you should be used to seeing them.
0:05:38.464,0:05:40.206
And if they're confusing, just remember
0:05:40.206,0:05:42.960
you can convert them back[br]into a while loop like this.
0:05:42.961,0:05:46.500
And if you're feeling adventurous,[br]try using a for loop in your new program,
0:05:46.501,0:05:48.664
or going back to some of your old,[br]simple while loops,
0:05:48.665,0:05:52.101
and converting them[br]into for loops, if appropriate.
0:05:52.102,0:05:56.700
A final note: you cannot always convert[br]a while loop into a for loop,
0:05:56.701,0:05:59.299
only when they're[br]in a really simple form like this,
0:05:59.300,0:06:01.942
where it's really clear[br]what variable you're starting out with,
0:06:01.943,0:06:05.532
how long it's going for,[br]and how it's changing.