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
Let's explore the compound expression for loop from yesteryear.
Copy and Paste
int[] bensScoreCard = {1, 2, 1, 5, 2, 4, 4, 4, 3, 6, 1, 2, 5, 4, 3, 2, 6, 3};
Learn More
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 standard for
0:00
loop might seem like
the wrong order to teach in.
0:05
The enhanced for loop is the way
that you as the Java developer
0:08
are encouraged to loop over an interable,
an object that can be iterated on.
0:12
Java 5 introduce the enhanced for loop.
0:17
Now before that, Java encouraged
you to use the plain old for loop.
0:20
And that's because for
0:24
loops are ideal to use when you know how
many times you want your code to execute.
0:26
Which, arrays are great for, right?
0:31
We know how many elements
are in the array and
0:33
we can access the elements
easily by their index.
0:36
So the enhanced for
loop is the way to do things now.
0:40
Why are you even being taught the for
loop?
0:43
Well, great question.
0:46
And I am glad you asked.
0:47
The answer is two fold.
0:48
Now first, you aren't always going
to be working in latest code based.
0:50
You will see regular for
loops used to loop over an array and
0:53
its entirety in projects that you work on.
0:56
You'll see it in online examples and
books.
0:58
It's an important piece
of history to understand.
1:01
Second, for
loops give you a lot more power and
1:04
information than enhanced for loops do.
1:07
Now you might need to keep track of the
current iteration count in your code and
1:09
this is where for loops shine.
1:13
So what do you say we go get to
know yield and enhanced for loop.
1:16
So, here is our enhanced for loop.
1:20
Let's go ahead and
let's mark that as such.
1:23
So we'll say,
this is the Enhanced for loop.
1:25
And now, let's recreate that same loop but
in a standard format.
1:31
So, the structure actually looks the same,
right.
1:36
So it's a for, and then our expression and
then opening and closing.
1:39
Now for loops are very versatile and can
be used for all sorts of looping needs,
1:45
not just working with iterable objects.
1:50
So, before we dive in here,
1:53
a bit of warning, this next bit is
known to overwhelm people a little bit.
1:55
So I'm gonna take it slow then
I'm gonna go over it again.
2:00
But don't worry, we'll get it.
2:03
Just be patient with me and yourself.
2:05
What makes this seem so intense is
that this expression block right here
2:07
is actually going to end
up being three expressions.
2:12
So the first expression of the three
2:16
is what is known as
the initialization expression.
2:19
Before the loop starts,
2:22
you use this space to initialize things
that you want to use in your looping code.
2:24
The variables that you initialize
here are only in the for
2:31
loop's scope, okay, it's only in here.
2:34
So what we want to do
is to create an integer
2:38
that will store the index to our array for
each loop through.
2:44
So typically, in for
loops this is named i.
2:48
So which you can kinda think of as index,
and yes that is a single
2:53
letter variable name and
yes in most cases that is bad practice.
2:58
But that doesn't mean
that it isn't common.
3:03
Check the teacher's notes for
more information.
3:05
So we want our loop to start
on our first friend, right?
3:08
So that is the first element of the array.
3:12
So what we would do is to use
the first index of the array.
3:15
So, what do we know about array
indices and where they start?
3:20
They started zero, that's right.
3:24
So, now we've initialized a variable named
i which will be used to keep track of our
3:27
index, and then we finish our
expression with a semi colon.
3:32
Okay, so now we are to the second of
three expressions, the conditional.
3:39
So what's expected here is a condition,
much like you'd put in an if statement.
3:46
If this expression returns true,
the loop will continue.
3:51
If it's false the loop will stop.
3:56
So, we want to process every
single element in this array.
3:59
So we want this loop to continue until
our index reaches the final index,
4:04
which because we start at a zero index,
4:09
is one less than the total
number of elements, right?
4:12
So, the last index of this friend's
array is what, it's two right?
4:16
Cuz it's 0, 1, 2.
4:21
There's tree values.
4:25
So the last index is the length minus 1.
4:26
So as long as I say that, as long as i
4:29
is less than friends.length,
the loop should continue, right?
4:34
So 2 is less than 3, so
that would continue the loop but
4:41
3 is not less than 3, so
the loop will not continue.
4:45
So again, if this condition returns false,
the loop will be terminated.
4:49
This conditional expression is
also sometimes referred to as
4:54
the termination expression.
4:58
So we'll add a semicolon
to complete our expression.
4:59
And our final expression that
complete this expression trifactor
5:05
is one that is known as the increment or
stepping expression.
5:10
Now what happens if this
expression is run after
5:14
every completed run through the loop.
5:18
So what we wanna do is move
our index to the next one.
5:21
Now the typical way to do this is
to use the post increment operator.
5:26
So we would do i plus plus, right,
that would move i to the next one.
5:31
So the loop runs,
the loop goes through and
5:35
then it runs i++, so then i would be 1.
5:38
So the loop runs and then this expression.
5:42
So sometimes this expression is also
referred to as the afterthought.
5:46
So again, we have the initialization
5:51
which happens the very first
time that the loop is processed.
5:55
And then we have the condition or
the termination,
5:58
which is checked before each iteration.
6:01
If this is false, things get terminated.
6:04
And finally, after each iteration,
this afterthought runs.
6:07
[SOUND] So let's use it, right?
6:13
So, we will pull the current iteration's
friend out of our array, right?
6:18
So we're gonna say string friend
= to friends, and then we're
6:23
just gonna use that variable i that is in
our loop, that's why we're setting it.
6:28
So now we can just copy this and
paste it, and wow if
6:35
you look at them side by side like this,
this one sure is more a concise, right?
6:41
But like I said, this is common enough and
you are going to see it everywhere still.
6:48
So, let's run it and
then do a walk through.
6:54
Let's just go ahead and
we'll clear javac explorer,
6:57
java, and java explorer and run that.
7:02
Make sure the file's saved.
7:06
Got an error, what did I do wrong?
7:08
I bet that I left the trailing,
trailing brace.
7:11
There we go, so
let's run that one more time, up arrow.
7:24
There we go,
you can see it does the exact same thing.
7:28
All right, so let's walk this.
7:31
Our initialization expression runs.
7:33
First, it declares and
initializes a new variable named i for
7:36
index to 0, and
then our condition expression run.
7:40
So it says is i which is 0 less
than friends.length which is 3.
7:45
0 is less than 3, so our code block runs.
7:50
So in this loop we use our variable
i which is currently 0 to pull
7:55
the current element out.
7:59
So, friends.0, the first element
in our array is Ben, right, and so
8:01
here it is Ben.
8:06
Then we print out that line.
8:09
Now that our loop has ended, right, so
we've gotten to the end of our loop,
8:12
we're here, we now run our
afterthought expression which is here,
8:18
so it says i++, so
now i is equal to i plus 1, so i is 1.
8:24
Next we run the conditional,
8:28
expression says 1 less than 3,
indeed it is so we can keep going.
8:30
So we run it again, we pull out
friends index 1 which is Alena and
8:35
we print it out, we do a afterthought,
and now it's 2 and 2 is less than 3 so
8:40
we continue and then we pull out
our last element of Pasan here.
8:44
And then we increment our expression which
is bumps at the 3 in our condition checks,
8:49
hey is 3 less than 3?
8:54
No, it's not, so the loop ends.
8:57
Yay!
8:59
I definitely see why the enhanced for
loop is the suggested way, right?
9:00
Now, if that is fuzzy,
feel free to rewind me and watch me again.
9:04
It's okay if it didn't
sink in the first time.
9:09
That is a complex expression,
there are a bunch of things going on.
9:11
Okay there is one more common used
case for this style of the for.
9:15
Sometimes you need to know how many
times through the loop you have gone,
9:19
this index really helps.
9:23
Now for instance, in the teacher's notes,
I've included a golf's course array.
9:25
Copy it and paste it up here.
9:30
So I'm gonna copy it from the features's
note, here I have it, and
9:31
I come down here and I'm gonna paste it.
9:36
Okay so these are Ben's golf scores,
there's 18 scores there.
9:38
So the way that we talk about
golf is by hole number, I think.
9:42
I'm actually not a golf player but
unfortunately golf courses
9:46
don't number their fields the same
way that our array does, right?
9:50
The first whole is 1 in golf land but
our first index here is 0.
9:54
So we'll need do a count for that.
9:59
So let's do this, let's loop
through the Ben's scoreCard array.
10:01
So let's use that using the for
loops, so we'll say for and
10:05
just like above we'll
initialize a variable called i,
10:11
and we'll start it at 0.
10:16
And then we'll go up until the length,
10:19
it'll stop at the end
which is what we want.
10:21
So we'll say i, as long as i is
less than benaScoreCard.length,
10:23
and then finally we'll
increment our index, right?
10:32
So it's a common, you get used to it.
10:34
It's a little bit of
a muscle memory thing there.
10:36
So, now we'll print out the whole number.
10:38
So we'll say, System.out.printf,
10:40
and we'll say hole, and
that's gonna be an integer, right?
10:47
So we'll say hole number and
the string formatting if we do %d for
10:50
digit and
then we'll say the score again %d.
10:57
And let's not forget our new line,
and then what do we wanna print there?
11:02
We want to print the first one here.
11:07
We said that our index is 0,
11:10
and we don't want to display that to users
of our application because they probably
11:11
don't understand the 0 based indexing,
which is what we are understanding now.
11:15
So the index is gonna be 0 but
really that's hole 1.
11:20
So what we wanna do is we wanna say i
+ 1 for the first variable there, and
11:23
then we wanna use that
index to pull it out.
11:27
So we'll say bensScoreCard and
we wanna pull out index i.
11:30
Cool, right closes the for loop there.
11:39
Okay, so let's give that a run.
11:41
I'm gonna press the up arrow again, and
then I'm gonna pipe this into a program
11:43
called less, which will allow us to scroll
a little bit easier with the arrow keys.
11:47
So if you do pipe, the thing above return
on these keyboards and then the word less.
11:53
Now, you'll notice I did the common
mistake of not saving my file,
12:04
see the red dot there?
12:08
I'm gonna save it.
12:09
There we go, and
then I'm gonna try to run that again.
12:11
And I spelled system wrong.
12:15
There we go.
I guess we have hole number 1, 2, 3, 4,
12:21
let's see it's counting up properly.
12:23
Awesome nice.
12:26
Now one thing you might have noticed is
that we have a similar variable problem
12:27
that got us to thinking about
arrays in the first place.
12:31
If I was playing a golf game in this
current way, each time I needed to track
12:35
someones scoreCard,
I would need to make a new array and
12:38
name it with the person's name
just like I did Ben's scoreCard.
12:42
Well, after seeing the power of arrays,
this feels dorky, right?
12:46
We should be able to do better than that.
12:50
It seems like you should be able
to make an array of arrays.
12:53
Well, good news, you can.
12:56
They're called multi-dimensional arrays
and they solve this dorky problem.
12:58
Let's get to it right
after this quick break.
13:02
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