1
00:00:01,459 --> 00:00:03,043
Now that you know about while loops,
2
00:00:03,014 --> 00:00:05,242
let's talk about
another kind of loop, for loops.
3
00:00:05,811 --> 00:00:07,397
Here's a for loop I made to praise
4
00:00:07,398 --> 00:00:10,534
the most delicious kind of pizza,
pineapple pizza.
5
00:00:10,535 --> 00:00:12,965
You might think, "I liked while loops,"
6
00:00:12,966 --> 00:00:15,134
"Why should I care about
this new kind of loop?"
7
00:00:15,135 --> 00:00:17,333
Well, soon you will see
that a for loop is a way
8
00:00:17,334 --> 00:00:20,634
to write simple kinds of while loops
in a bit of a shorter way.
9
00:00:20,635 --> 00:00:22,693
So, before we inspect this for loop,
10
00:00:22,694 --> 00:00:26,316
let's go ahead and look at a while loop
which will be a bit more familiar.
11
00:00:26,317 --> 00:00:28,532
We'll get back to this for loop
in a moment.
12
00:00:28,533 --> 00:00:31,266
So, just like you saw
in the intro to while loops,
13
00:00:31,250 --> 00:00:33,584
We start with this variable y,
14
00:00:33,604 --> 00:00:36,728
and we write a message
using that location y.
15
00:00:36,729 --> 00:00:38,498
We say that we want to keep going
16
00:00:38,499 --> 00:00:42,767
as long as y is less than 300
and change y by 40 each time.
17
00:00:42,768 --> 00:00:46,668
Now, just to reinforce each of these,
which should be review,
18
00:00:46,669 --> 00:00:50,600
we can say that "Pineapple pizza is
the best" is an important message,
19
00:00:50,601 --> 00:00:52,400
so we probably want to be
writing it more.
20
00:00:52,401 --> 00:00:55,268
so we should start higher
--so we change that--,
21
00:00:55,269 --> 00:00:58,466
it should go on for longer,
so we make it bigger,
22
00:00:58,467 --> 00:01:01,636
and it should be spaced
closer together --don't you think?--
23
00:01:01,637 --> 00:01:03,612
so that's a little bit better.
24
00:01:03,613 --> 00:01:07,505
Now, let's see how we can change
this while loop into a for loop.
25
00:01:07,506 --> 00:01:09,567
I'm going to comment out this while loop,
26
00:01:09,568 --> 00:01:14,027
just so you can see that it's doing
what I'm saying it's going to do.
27
00:01:14,028 --> 00:01:19,836
And we'll create a for loop
and some space to fill in later.
28
00:01:19,837 --> 00:01:22,539
Something really important,
kind of different for a for loop,
29
00:01:22,540 --> 00:01:26,377
it's not just one thing that goes in here,
it's actually three things.
30
00:01:26,378 --> 00:01:27,932
We tell the computer that
31
00:01:27,933 --> 00:01:31,201
by using these semicolons
to separate the three parts,
32
00:01:31,536 --> 00:01:34,473
we'll have one part here,
one part here, one part here,
33
00:01:34,474 --> 00:01:37,331
and then, the inside of the loop,
just like we had before.
34
00:01:37,332 --> 00:01:39,579
So, what goes in each of these parts?
35
00:01:39,580 --> 00:01:45,866
You can think of it as a start,
or initialization,
36
00:01:45,867 --> 00:01:47,966
then we'll have a semicolon,
37
00:01:47,967 --> 00:01:50,994
then, some sort of instructions
on how long to repeat,
38
00:01:50,995 --> 00:01:55,967
and then we're going to have
some sort of change happening.
39
00:01:57,174 --> 00:01:59,630
So, how does that correspond
with this while loop?
40
00:01:59,631 --> 00:02:01,074
Really concretely, we can say
41
00:02:01,075 --> 00:02:04,133
that the start sets up
this variable y here,
42
00:02:04,134 --> 00:02:08,880
so let's copy and paste that over
to this first part of the for loop.
43
00:02:08,881 --> 00:02:13,400
Similarly, we can say
this middle part is saying
44
00:02:13,401 --> 00:02:17,196
how long we should keep going for
and that goes in the middle.
45
00:02:17,197 --> 00:02:24,743
We say the change happens at the end here,
we'll put that at the end of the for loop.
46
00:02:25,698 --> 00:02:29,518
These three parts always have to occur
in this order in your for loop.
47
00:02:29,519 --> 00:02:32,066
You always have to start by saying
48
00:02:32,067 --> 00:02:34,294
this is what the variable
should start out as.
49
00:02:34,295 --> 00:02:35,574
Here it's 27.
50
00:02:35,575 --> 00:02:41,562
Then you say how long to keep going for.
Repeat as long as it is less than 354.
51
00:02:41,563 --> 00:02:44,496
Then you need to put
how you are going to be changing things.
52
00:02:44,497 --> 00:02:47,835
So here we are going to be changing
by increasing y by 24.
53
00:02:48,747 --> 00:02:50,944
Last, you just have to do
54
00:02:50,945 --> 00:02:52,866
whatever you wanted to do
inside the for loop.
55
00:02:52,867 --> 00:02:54,671
So we'll go ahead
and write that text there.
56
00:02:54,671 --> 00:02:57,337
And there we have have it,
pineapple pizza is the best.
57
00:02:57,338 --> 00:02:59,234
Everybody will know.
58
00:02:59,235 --> 00:03:03,397
Now, let's think a bit more closely about
what's happening with this for loop.
59
00:03:03,398 --> 00:03:07,767
If we wanted to change where it started,
well, we just change the start here.
60
00:03:07,768 --> 00:03:11,166
If we wanted to change where it ended,
we just change this endpoint.
61
00:03:11,167 --> 00:03:16,034
If we wanted to change the spacing,
we would just change the increment number.
62
00:03:17,367 --> 00:03:20,641
We can also change
--just like with the while loop --
63
00:03:20,642 --> 00:03:23,443
what value we have for x.
64
00:03:28,453 --> 00:03:30,540
One thing that can be confusing
about a for loop
65
00:03:30,866 --> 00:03:32,971
is to remember what these semicolons do.
66
00:03:32,972 --> 00:03:37,165
You need to remember that they always need
to be there to separate the 3 parts.
67
00:03:37,166 --> 00:03:38,868
If we don't have them there,
68
00:03:38,869 --> 00:03:41,465
then we are going to get
some weird error messages
69
00:03:41,466 --> 00:03:42,599
about our for loop,
70
00:03:42,600 --> 00:03:45,634
so whenever you see
that just double-check that you have them.
71
00:03:45,635 --> 00:03:48,980
You also may get overenthusiastic
and add an extra one at the end,
72
00:03:48,981 --> 00:03:53,651
but if you just remember that semicolons
are only there to separate the 3 parts,
73
00:03:53,652 --> 00:03:56,871
then we can say that this last semicolon
isn't separating anything,
74
00:03:56,872 --> 00:04:01,164
it's just trailing on there, so we can
get rid of it because we don't need it.
75
00:04:01,165 --> 00:04:04,328
Now, I know you're probably
getting a little bit tired of seeing
76
00:04:04,329 --> 00:04:06,497
these for loops getting converted
into while loops and back,
77
00:04:06,705 --> 00:04:08,636
let's just do it one more time,
78
00:04:08,637 --> 00:04:10,557
so you can see a for loop really is
79
00:04:10,558 --> 00:04:13,286
just another way of writing
a simple kind of a while loop,
80
00:04:13,287 --> 00:04:15,766
and you'll be really confident
in understanding
81
00:04:15,767 --> 00:04:18,498
how to go from a for loop
back into a while loop.
82
00:04:18,801 --> 00:04:22,863
You can do that with any kind
of for loop, not just this one.
83
00:04:22,864 --> 00:04:24,854
The first thing that we do is think about
84
00:04:24,855 --> 00:04:28,193
where to put this first value
that we have inside of our for loop.
85
00:04:28,704 --> 00:04:31,062
Since it's just initializing
this variable,
86
00:04:31,063 --> 00:04:34,056
remember that it has to go outside
not inside the while loop,
87
00:04:34,057 --> 00:04:35,798
and it should go before as well,
88
00:04:35,799 --> 00:04:38,365
because we need to be using it
during our while loop.
89
00:04:38,366 --> 00:04:40,631
Then you think about
where should this condition go,
90
00:04:40,632 --> 00:04:41,831
--that's pretty easy--,
91
00:04:41,831 --> 00:04:45,942
usually the stop condition,
or the repeat until condition,
92
00:04:45,943 --> 00:04:47,567
needs to go inside here,
93
00:04:47,568 --> 00:04:51,873
we're gonna say while y
is less than 313 we'll keep going.
94
00:04:52,871 --> 00:04:54,326
Finally, the change.
95
00:04:54,326 --> 00:04:56,567
We always put the change
at the end of the while loop,
96
00:04:56,568 --> 00:04:59,639
in the loops that we've seen,
so we should go and do that here.
97
00:04:59,640 --> 00:05:04,235
Now, we just need to move this text call
inside, and there we have it.
98
00:05:04,696 --> 00:05:09,796
Comment out this whole loop, and you see
that we made the exact same thing happen.
99
00:05:11,556 --> 00:05:13,177
Hopefully by now, you can see
100
00:05:13,178 --> 00:05:15,678
that this new for loop
isn't actually necessary.
101
00:05:16,132 --> 00:05:20,101
We could go through our entire programming
lives writing loops that looked like this.
102
00:05:20,102 --> 00:05:23,968
But, it is a bit impressive
how much more concise this for loop is.
103
00:05:23,969 --> 00:05:27,604
It makes it a bit more clear,
once you're used to it, what's going on.
104
00:05:27,605 --> 00:05:29,400
How we're starting a variable,
105
00:05:29,401 --> 00:05:31,091
we're saying how long to keep going,
106
00:05:31,092 --> 00:05:33,367
and then changing that variable here.
107
00:05:33,368 --> 00:05:36,729
So, if you really hate for loops,
you don't have to use them ever,
108
00:05:36,730 --> 00:05:38,463
but you should be used to seeing them.
109
00:05:38,464 --> 00:05:40,206
And if they're confusing, just remember
110
00:05:40,206 --> 00:05:42,960
you can convert them back
into a while loop like this.
111
00:05:42,961 --> 00:05:46,500
And if you're feeling adventurous,
try using a for loop in your new program,
112
00:05:46,501 --> 00:05:48,664
or going back to some of your old,
simple while loops,
113
00:05:48,665 --> 00:05:52,101
and converting them
into for loops, if appropriate.
114
00:05:52,102 --> 00:05:56,700
A final note: you cannot always convert
a while loop into a for loop,
115
00:05:56,701 --> 00:05:59,299
only when they're
in a really simple form like this,
116
00:05:59,300 --> 00:06:01,942
where it's really clear
what variable you're starting out with,
117
00:06:01,943 --> 00:06:05,532
how long it's going for,
and how it's changing.