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 Loops!
You have completed Java Loops!
Preview
Also known as the For Each Loop, itβs a simple way to process each item in a group without worrying about counters or indexes.
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
Hey there.
0:01
Are you feeling a little loopy after that
last one?
0:02
Don't worry.
0:04
We're going to cover a loop
that's pretty handy when you want to go
0:05
through an entire set of things,
one by one,
0:08
without worrying about the details
of a conditional and a stopping point.
0:11
You provide a list of things,
and it goes through them all.
0:16
The loop is called the enhanced for loop,
or the for each loop.
0:20
The syntax is clean and simple,
but there's one small issue.
0:24
This loop will only work with something
that is iterable,
0:28
meaning it could be iterated over.
0:32
So it requires a group of values
of some kind, and since arrays
0:34
are likely in your near future,
I'll provide some of those for us to use.
0:37
Sounds a fortune cookie.
0:41
"I sense a raise in your near future."
0:43
Anyways, let's open up our workspace.
0:45
Okay, let's open up foreach.java.
0:49
If you're wondering why these class files
are here now and weren't before,
0:53
I got pulled away for a moment by my kid
and had to refresh the workspace.
0:56
You should have them too,
1:00
but they don't show up automatically
after compiling your Java files.
1:01
You can right click in the sidebar here
and choose refresh and they'll pop up.
1:06
Not important though.
1:10
You can see here
I've provided you with two arrays,
1:12
an array of strings
and an array of integers.
1:14
If you're already familiar with arrays,
fantastic, you know what's going on.
1:17
If not, this is basically a super handy
way of storing
1:21
multiple values
of the same data type into one variable.
1:25
I know it looks a little weird, but please
don't worry about this just yet.
1:29
You'll learn about arrays soon enough.
1:33
For now, just focus on
how the loop picks out each item,
1:35
one at a time,
and runs the code inside the loop with it.
1:38
Let's play with this string one first.
1:42
What I have
here is a list of the final name options
1:44
my wife and I had to decide from to name
our kid.
1:47
Most of these were my ideas.
1:51
Let's loop through them
with an enhanced for loop.
1:53
Here's the syntax.
1:55
We type for, some parentheses,
1:57
and our curly braces, exactly last time.
I know you're probably having nightmare
2:00
flashbacks but don't worry. The difference is
what happens in these parentheses here.
2:05
I'm going to comment out this integer
array down here for now.
2:10
I don't want these distractions
becoming distractions.
2:13
We provide a local variable here,
which will contain
2:18
the currently
iterated over item in our list.
2:20
So we give the type, a variable name,
and then we put a colon.
2:24
On the other side of
2:30
the colon, we provide the iterable
we want to loop over.
2:31
Then, as always, in the body we provide
2:35
what we want to happen to each item.
2:37
So let's change this type here to string,
2:41
because these values are of type string.
2:43
And let's just use
name as our variable name.
2:48
We can name this anything we want,
but let's keep it relevant and readable.
2:51
This is a list of names, right?
2:55
Over here we add what we want to loop over,
2:57
so let's add the baby names array. Cool!
3:00
As usual let's just print a line.
3:04
System dot out dot printf
3:08
let's do percent s
and a percent n for a new line
3:12
then we'll use our name variable.
3:17
Let's run this and then we'll go over it.
3:20
So we got
3:23
clear and javac for each dot java
3:23
and java for each.
3:27
Cool,
3:31
we got all of our names printed out
nice and clean.
3:32
So on the first run of the loop,
our variable name held the value
3:35
of the first value in this array,
which in this case was Bruce Wayne.
3:39
After the code block ran, it went
and grabbed the next item in the array
3:44
and plugged it into our name
variable, Donatello.
3:48
Great name. And so on.
3:51
So anytime we use a for each loop,
it will run through everything
3:54
you give it without stopping
until there's nothing left.
3:57
But we can also use those break
and continue keywords.
4:01
In fact, you can use those keywords
in all of the loops we gone over
4:05
in this course.
4:08
Let's reenact history
and break out of the loop
4:10
the moment we get to the
name my wife liked the best.
4:12
So let's add a conditional.
4:17
If name equals Enzo
4:19
System.out.printf
4:25
It's %s and that's final,
4:30
plugging in our name variable.
4:33
And break after that.
4:35
Let's place this in an else
and add a question
4:39
mark to the end of this one down here too.
4:42
Let's run it.
4:50
I'll use the up arrow
4:53
to get back to the last command.
4:54
Awesome!
4:59
As soon as we hit
Enzo, it broke out of the loop.
5:00
And we were just
getting into the good ones too.
5:03
Oh well, at least nicknames are a thing.
5:05
So far, we've been primarily
just printing lines to the console,
5:09
just to keep things clear
as they're happening, and to not bring on
5:12
any extra potential confusion
with complex code,
5:16
as we're trying to learn the syntax
and functionality of these loops here.
5:19
But, I'll give one more simple example,
showing that you can really do
5:23
anything in these loops.
5:26
Let's play with this integer one.
5:28
Since the kiddo had distracted me
just now,
5:31
I decided to log how many times a day
my kid pulls me away from my work.
5:33
So this would be Monday, five times.
5:38
Ugh. Alright.
5:40
Tuesday was only once. Not bad.
5:42
He was out of the house that day.
5:44
326! Alright,
5:46
that one's an exaggeration,
but that's how it feels sometimes.
5:49
You parents know what I'm talking about.
5:53
What I'd like to do
is add all these up into one big total so
5:55
I can use it against him when he's older
and he's saying that I'm bothering him.
5:58
Now we could grab a calculator
or our phones,
6:03
but I don't want to be sitting here
for 20-30 seconds pushing buttons.
6:06
Let's make Java
do it for us with a for each loop.
6:10
Let's get the structure set up.
6:14
For and we use int this time as our type
6:16
Let just say num short for number, colon.
6:21
And we want to iterate
over this distractions array this time.
6:25
Real quick, let's create a variable up
top here to keep our growing total.
6:29
We'll say int total equals zero.
6:34
Cool.
6:38
Now let's work all this into the loop.
6:39
Within the body,
we'll say total plus equals num.
6:41
If you're unfamiliar,
that's the same as saying
6:45
total equals total plus num.
6:47
After that, let's system dot out
6:52
dot print f percent d
6:54
and a new line, dropping total in there.
6:57
So we should see the new total each
step of the way.
7:00
Let's try it.
7:03
I'll hit the up arrow.
7:05
Cool.
7:10
We started with 0,
and you can see it added 5, 1,
7:11
then 326, and so on.
7:15
Once it ran out of numbers to loop over,
it automatically terminated the loop
7:18
with no problems. Fantastic.
7:22
Wow, that's a big number.
7:25
I'd better go teach Lord
StinkyPants how to chill out!
7:28
So the enhanced for loop is a neat way
to say, for each item in this group,
7:32
do something, without having to manage
a counter or worry about when to stop it.
7:36
It's clean, simple,
and less likely to cause mistakes.
7:41
This is your best friend
if you have a group of something
7:45
and you need to loop over every item
in it.
7:47
Don't forget about the break keyword
if it's unnecessary to keep going
7:50
passed a certain point.
7:53
I hope this has
7:56
sparked ideas for you
on all the amazing things you can do with
7:57
loops in your programs and all of the time
that's going to save you.
8:00
They're an absolute game changer.
8:04
And as I said, when you start to learn
about arrays, lists, and things that,
8:06
you're going to be using both of these
for loops a lot more,
8:11
and you're going to get super comfortable
with them, and fast.
8:14
So don't worry if they seem
a little overwhelming right now.
8:17
I hope you all enjoyed this,
and for each student,
8:21
in viewers,
system.out.print, thanks for watching!
8:23
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