Bummer! You must be logged in to access this page.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Java Arrays!
You have completed Java Arrays!
Preview
Let's explore the compound expression for loop from yesteryear.
Copy and Paste
int[] briansScoreCard = {1, 2, 1, 5, 2, 4, 4, 4, 3, 6, 1, 2, 5, 4, 3, 2, 6, 3};
Resources
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
Introducing you to the Enhanced For Loop
before showing off a traditional For Loop
0:01
might seem the wrong order to teach in.
0:05
The Enhanced For Loop
is the way that you, as a Java developer,
0:08
are encouraged to loop over an iterable,
an object that can be iterated on.
0:12
It was released in 2004,
so it's been around
0:16
for quite a while now
and is considered standard.
0:19
Before that, Java encouraged you
to use the standard For Loop.
0:22
That's because for loops are ideal to use
when you know how many times
0:26
you want your code to execute.
0:29
Arrays are great for this, right?
0:31
We know how many elements
are in the array,
0:33
and we can access the elements
easily by their index.
0:36
So the enhanced for loop
is the way to do things now.
0:39
Remember, you aren't
0:43
always going to be working
in the latest code base.
0:44
You will see regular for
loops used to loop over an array
0:47
in its entirety in projects
that you work on.
0:50
You'll see it in online
examples, in books.
0:53
It's an important loop to understand.
0:56
And second, for loops
give you a lot more power
0:58
and information
than the enhanced for loops do.
1:01
You might need to keep track
of the current iteration
1:04
count in your code,
and this is where for loops shine.
1:07
Again, I'm going to assume
that you've seen the Java loops video
1:11
that covers this for loop,
1:14
and you're already familiar
with its kind of intimidating syntax.
1:16
There's a link in the teacher's
notes below if you need a refresher.
1:20
So let's check out arrays
with the standard for loop.
1:23
Okay, here's our enhanced for loop.
1:28
Let's go ahead and mark that as such.
1:30
So we'll say
this is the enhanced for loop.
1:31
And now let's recreate that same loop
but in a standard format.
1:35
The structure is actually the same.
1:41
It's a for, then our parentheses, and
then our opening and closing curly braces.
1:42
For loops are very versatile
and can be used for
1:48
all sorts of looping needs,
not just working with iterable objects.
1:51
So now, onto our three
intimidating expressions.
1:54
So first,
what we want to do is create an integer
1:58
that will store the index to our array
on each loop through.
2:01
Typically, in for loops, this is named i.
2:05
You can think of it as short for index.
2:08
And yes,
that is a single letter variable name,
2:10
and yes, in most cases,
that is bad practice.
2:13
But that doesn't mean it isn't common.
2:17
We want our loop
to start on our first friend
2:19
right?
That is the first element of the array.
2:21
So we use the first index of the array.
And what do we know about array indices
2:24
and where they start?
They start at zero, that's right.
2:28
So now we've initialized
a variable named i, which will be used
2:32
to keep track of our index.
2:35
And then we finish our expression
with a semicolon.
2:37
Now we're to the second
of the three expressions, the conditional.
2:40
What's expected here is a condition,
much like you'd put in an if statement.
2:44
If this expression returns true,
the loop will continue.
2:48
If it's false, the loop will stop.
2:52
We want to process
every single element in this array,
2:54
so we want this loop to continue
until our index reaches the final index.
2:57
Because we start at a zero index,
that's one less than the total number
3:02
of elements.
3:05
The last index of this friends
array is two, right?
3:06
Because it's zero, one, two.
3:09
But there are three values.
3:11
So the last index is the length minus one.
3:13
So as long as i is less than
friends.length, the loop should continue.
3:16
2 is less than 3, so the loop continues.
3:21
But 3 is not less than 3,
so the loop will not continue.
3:23
Again, if this condition returns false,
the loop is terminated.
3:27
This conditional expression is also
3:31
sometimes referred to as the termination
expression.
3:33
We'll add a semicolon
to complete our expression.
3:36
The final expression that completes
3:39
this trifecta is one that is known
as the increment or stepping expression.
3:41
This one is run after every completed run
through the loop,
3:46
and we want to move our index
to the next one.
3:49
The typical way to do this is to use
the post increment operator,
3:52
so we would just do i++, right?
3:56
That moves i to the next one.
3:58
So the loop runs,
and then this expression runs.
4:01
Sometimes this expression
is also referred to as the afterthought.
4:03
So again, we have the initialization,
which happens the very first time
4:07
the loop is processed.
4:11
Then the condition or termination,
which is checked before each iteration.
4:13
If it's false, things get terminated.
4:17
Finally, after each iteration,
this afterthought runs.
4:20
So let's use it.
4:24
We'll pull the current
iteration's friend out of our array.
4:26
We'll say string friend
4:29
equals friends,
4:33
and then we'll just use that variable
i that's in our loop.
4:35
That's why we're setting it.
4:38
Now we can just copy this and paste it.
4:39
And wow, if you look at them side by side
4:46
like this, this one sure is more concise right?
But like I said, this is common enough
4:49
and you're going to see it everywhere
still. Let's run it and do a
4:54
walkthrough.
4:57
All right, so let's walk this.
5:05
Our initialization expression runs first.
5:06
First, it declares and initializes
a new variable
5:09
named i for the index to 0.
5:12
Then our condition expression runs.
5:15
It says, is i, which is 0,
less than friends.length, which is 3.
5:17
0 is less than 3, so our code block runs.
5:22
In this loop, we use our variable
i, which is currently 0,
5:25
to pull the current element out,
friends 0.
5:29
The first element in our array
is Brian, right?
5:32
Then we print out that line.
5:35
Now that we've
5:38
gotten to the end of our loop,
we run our afterthought expression.
5:38
It says i++, so now i is equal
5:42
to i plus 1, so i is 1.
5:45
Next,
we run the conditional expression again.
5:48
It says, is 1 less than 3?
5:50
Indeed it is, so we can keep going.
5:52
So we run it again,
5:55
we pull out friends index
1, which is Rohald, and we print it out.
5:56
We do an afterthought, and now it's
2, and 2 is less than 3, so we continue,
5:59
and then we pull out
our last element of Laura here.
6:04
Then we increment our expression,
which bumps it up to 3,
6:08
and our condition checks.
6:11
Hey, is 3 less than 3?
6:12
No, it's not, so the loop ends. Oof!
6:14
I definitely see
6:18
why the enhanced for loop
is the suggested way, right?
6:19
Now if that is fuzzy,
feel free to rewind me and watch me again.
6:22
It's okay if it didn't
sink in the first time.
6:26
That is a complex expression,
and there are a bunch of things going on.
6:28
Okay, there is one more commonly used case
for this style of the for loop.
6:33
Sometimes you need to know how many times
through the loop you have gone.
6:37
This index really helps.
6:41
Now, for instance, in the teacher's notes,
I've included a golf course array.
6:43
So I'm going to copy it from the teacher's
notes below.
6:47
Here I have it.
6:49
And I'm going to come down here
and I'm going to paste it.
6:51
Okay, so these are Brian's golf scores.
6:54
There are 18 scores there.
6:57
So the way that we talk about
golf is by hole number, I think.
6:59
I'm actually not a golf player,
but unfortunately,
7:03
golf courses don't number their holes
the same way that our array does, right?
7:05
The first hole is 1 in golf land
but our first index here is 0. So
7:09
we need to account for that. So let's do this.
Let's loop through Brian's scorecard array.
7:13
So let do that's
7:19
using the for loop.
So we say for and just like above,
7:20
we'll initialize a variable called i,
and we'll start it at zero.
7:24
And then we'll go up until the length.
7:29
It'll stop at the end,
which is what we want.
7:30
So we'll say i, as long as i is
less than Brian's scorecard dot length.
7:32
And then we'll increment our index, right?
7:38
So it's common, you'll get used to it.
7:42
It's a bit of a muscle memory thing there.
7:44
So now we'll print out the hole number.
7:47
So we'll say system.out.printf.
7:49
And that's going to be an integer, right?
7:53
So we'll say hole number,
and then the string formatting.
7:54
And then we'll say
the score again, percent d.
7:58
And let's not forget our new line.
8:02
We said that our index is zero,
and we don't want to display that
8:04
to the users of our application
because they probably don't understand
8:07
the zero based indexing,
which is what we are understanding now.
8:11
So the index is going to be zero,
but that's really hole one.
8:15
So what we want to do is we want to say
8:18
i plus one for the first variable there.
8:21
And then we want to use that index
to pull it out.
8:24
So we'll say Brian's scorecard,
8:27
and then we want to pull out index i.
8:29
Cool, right?
8:32
Let's close the for loop there.
8:33
Let's make sure to save the file
and okay, let's give that a run.
8:36
I'm going to press the up
8:40
arrow again and then I'm going to pipe
this into a program called less,
8:41
which will allow us to scroll
a little bit easier with the arrow keys.
8:45
So if you do pipe the thing above return
on most keyboards, and then the word less.
8:48
We have hole number one, two, three, four.
8:57
Let's see.
9:00
It's counting up properly. Awesome.
9:01
You can hit Q to back out of this program.
9:03
Now one thing you might have noticed
is that we have a similar variable problem
9:06
that got us into thinking about arrays
in the first place.
9:10
If I was playing a golf game
in this current way, each time I needed
9:13
to track someone's scorecard,
I would need to make a new array
9:16
and name it with the person's
name, just I did Brian's scorecard.
9:19
Well, after seeing the power of arrays,
this feels dorky, right?
9:23
We should be able to do better than that.
9:28
It seems you should be able
to make an array of arrays.
9:30
Well, good news, you can.
9:33
They're called multidimensional arrays,
and they solve this dorky problem.
9:35
Let's get to it
right after this quick break.
9:39
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