Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
For loops, instantiate, check if they should terminate, and then run some code.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
We've done do, while,
and we've done while,
0:00
we got the pre-check, the post check.
0:02
This one is the for loop.
0:04
When you know how many times you want
to loop, this is a great example for
0:07
when you want to use fors.
0:10
They're often called the C style for loop.
0:12
And it looks like a whole bunch of code
jammed into a little statement and
0:14
it's a little overwhelming at first.
0:18
Most C like languages implement them,
so you'll see it in Java Script,
0:20
you'll see them all over the place.
0:23
And it is an old school way
of looping over an array,
0:25
which we'll talk about arrays here in a
little bit, if you haven't heard of them.
0:28
But it's sort of an old school and
it's a little bit frowned upon, so
0:31
let's just jump into it.
0:35
Let's do 99 bottles of,
you know the old song,
0:37
99 Bottles of Milk on the Wall,
right, right?
0:40
Yeah, so, it's milk.
0:44
So, you go 99 Bottles of Milk on the Wall,
99 Bottles of Milk, you take one down and
0:47
pass it around,
98 Bottles of Milk on the Wall.
0:51
So, that sort of thing.
0:53
So, the for loop example, what we
would do here, is we'd start at 99.
0:54
We're gonna start our loop at 99,
and as long as there's some,
0:59
we're gonna take one off the wall,
and we're gonna sing about it.
1:02
And we're gonna drink our milk,
and we're gonna throw it away, and
1:05
then we're gonna go grab 98.
1:07
So that's a for loop.
1:08
This is the expression that's
happening and this is checking.
1:09
And these three little
1:14
new lines here have words and those words
also look a little bit intimidating.
1:18
Let's talk about what they are.
1:23
So, we have initialization.
1:24
So that runs the very
first time this enters.
1:26
So the very first time for
loop is encountered, initialization runs.
1:29
That's where you want to set up different
variables that you're tracking stuff with.
1:32
The second,
Java calls this thing a termination.
1:36
What that is, if this statement
returns false it will terminate.
1:39
This will run before the for loop goes and
then increment will happen afterwards.
1:43
So, this statement runs and checks,
and if it returns a true or false.
1:47
And if it returns true,
it continues looping.
1:51
If it returns false, it stops.
1:53
And the increment,
1:54
what happens is, every time the loop
goes through, this statement will run.
1:55
It doesn't have to increment
like we saw with the pluses.
1:59
I'm using these words because this is
how it is in the Java documentation, but
2:01
I find them a little bit confusing.
2:05
What we're gonna do in this next statement
with our for loop, is we're gonna do
2:07
99 Bottles of Milk on the Wall,
[LAUGH] I almost said the other thing.
2:10
And it's going to count down.
2:13
So let's do it.
2:16
You can go refill your milks now,
if you'd like to.
2:16
So milk, okay, so the for
loop looks like this.
2:21
So this is where we're gonna initialize.
2:28
Here we're gonna initialize
ta variable named i and
2:30
i is going to be less than 99, okay?
2:33
So as long as this is true,
we want to keep going, okay?
2:37
So this is the termination.
2:40
Then, each time we're gonna decrement,
okay?
2:43
[LAUGH] In the increment
spot we're gonna decrement.
2:46
So that's what minus, minus does, right?
2:49
It says, i = i- 1.
2:51
Decrementing by 1.
2:55
Shorthand for doing that, okay?
2:57
And then we'll say, %d,
2:59
bottles of milk on the wall....
3:05
And a %n, or that will get messy.
3:13
With the %, not a /.
3:16
Okay, and good, that's closed out.
3:19
And we will compile Milk.
3:25
>> [INAUDIBLE]
>> Oh, but you guys caught it, thanks!
3:30
>> [APPLAUSE]
>> Thank you, that was awesome.
3:34
Thanks, we're like peer programming
with 30, 40 people [LAUGH].
3:37
>> [LAUGH]
>> [INAUDIBLE]
3:41
>> [LAUGH] That's true [LAUGH].
3:44
Okay, so-
>> So
3:48
why are you having i
start at 0 [INAUDIBLE].
3:49
>> You're right!
3:53
Ken wow, [INAUDIBLE] double fail, wow!
3:55
>> [APPLAUSE]
>> Thanks.
3:57
Thank you.
4:00
Would've been messy.
4:01
All right here we go [LAUGH], I think it's
gonna work we've had a human compiler run-
4:03
>> [LAUGH]
4:08
>> [INAUDIBLE]
4:09
>> Guys, what am I doing?
4:14
>> [LAUGH]
>> You don't need me up here.
4:15
All right, so,
[LAUGH] I was gonna start at 99.
4:18
>> [LAUGH]
>> While i is greater than 0,
4:21
we're going to decrement it and
keep going.
4:25
Let's see if that works,
I bet it will [LAUGH].
4:28
>> [LAUGH]
>> Can we edit that?
4:30
Can we edit that that part?
4:33
Hey, there it is!
4:34
>> [APPLAUSE]
>> All right, wow.
4:36
Okay, so we initialized and
4:40
when this was false we terminated.
4:44
And then each time we decremented through.
4:49
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up