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
Some while loops can be written more succinctly as for loops.
This video doesn't have any notes.
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
This pattern of using an integer that
increases each time the loop runs, is so
0:00
common that there's a special
kind of loop just for that.
0:04
It's called a for loop.
0:08
I'll write what that looks like lower
existing loops so we can compare the two.
0:10
This is doing
0:15
exactly the same
0:22
thing the while loop, up here does.
0:32
We can break our while loop
up here into for parts.
0:38
First, we declare index as an integer and
initialize it to 0.
0:41
Second, we check to see if we’ve looped
through all the indexes of the array.
0:46
Third, we do something with each of the
array items Fourth, we increment index so
0:51
that the next time through the loop we're
getting the next item in the array.
0:57
For loops also have the same for
parts only there written more concisely.
1:03
Here's where we declare index as
an integer and initialize it to zero.
1:09
Here's where we check to see if we've loop
through all the indexes of the array.
1:13
The body of the loop is used for
doing something with the array item.
1:18
And here's where we increment index so
1:22
that the next time through the loop we're
getting the next item in the array.
1:25
Just think of for loops
as condensed while loops.
1:30
Now that we have this for loop we
don't need this while loop anymore.
1:34
For loops aren't only used for
looping through a race.
1:39
They're also really handy when we want
to count from one number to another.
1:42
We can set the starting number here and
it can be any number we want.
1:47
We decide where to stop counting here.
1:52
This can be any condition that eventually
stops the loop by evaluating to false.
1:55
Each time the loop runs,
index will count up by one.
2:00
In fact, this variable is
often called a loop counter.
2:05
We could also count by more than
one by using plus equals like this.
2:09
This will count up by two.
2:14
We want to count by one so
I'll change this back to index plus plus.
2:17
Because this variable index
is being declared right here,
2:22
its scope is limited
to inside of the loop.
2:26
That means that it can only
be used inside the 4 loop.
2:30
If we want to access the loop counter
variable after the 4 loop has ended,
2:34
we'd have to move the declaration
up here above the 4 loop somewhere.
2:38
[PAUSE] It may look strange, but
this is perfectly valid code.
2:42
When reading code, you'll notice that
most for loops use the single letter I
2:52
as the name of the loop counter variable.
2:57
This tradition goes all the way back to
the first programming languages created.
3:00
You can name it whatever makes sense for
your code though.
3:05
Just to maintain tradition I'll
shorten index to I for us.
3:08
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