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
Itβs time to check out the standard for loop, the classic tool for repeating actions a specific number of times using a counter.
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
Alright, I hope your brain isn't stuck
0:01
in an infinite loop
trying to process those last two videos.
0:03
I hope they're sinking in
and making sense,
0:06
but if your head feels it's
going to explode, warning.
0:09
This loop we're about to cover might light
the fuse.
0:12
Just remember, these videos
are always here for you to rewatch,
0:15
and you could even slow me way down
if I'm going too fast, okay?
0:19
It's time to meet the classic, the legend,
0:24
the loop you'll see everywhere in Java,
the The standard for loop.
0:27
The standard for loop,
or simply just the for loop, is your go-to
0:32
when you want to repeat something
a certain amount of times
0:36
and you need to keep track
of how many times it's been repeated.
0:39
You tell it where to start, when to stop,
and how to count,
0:43
and it takes care of the rest.
0:46
The syntax might look a
0:49
little intimidating at first,
but it's actually pretty logical.
0:50
Let's torture our brains a bit, shall we?
0:54
Okay, go ahead and launch the workspace
again if you don't have it open still.
0:57
Let's open up For.java.
1:01
Let's go into the main method here,
and here's the basic structure.
1:03
We type for, some parentheses
1:07
for the condition,
and curly braces for the body.
1:10
Just the normal while loop, right?
1:13
That's not so bad.
1:15
The reason this tends to be overwhelming
at first is this conditional section
1:17
here actually ends up holding
1:20
three separate expressions,
and they need to be in a specific order.
1:22
Let's just get right into it.
1:26
For this example,
let's recreate what we did in that first
1:28
while loop, you know,
where we were sneaking all those cookies.
1:31
I'm kind of hungry for tacos now, though.
1:35
The first of the
1:38
three expressions is what is known
as the initialization expression.
1:39
The variable you initialize
here is only in the for loop's scope.
1:43
So what we want to do is create,
or initialize,
1:47
an integer
that will work as our counter variable.
1:50
For loops really shine when used
with arrays and other collections,
1:54
and usually you'll see this variable named
I, short for index.
1:58
And it will usually start at zero
since arrays have a zero-based index.
2:02
Now, don't worry about all that stuff
for now, you'll learn about arrays
2:06
soon enough and you'll likely experience
the true power of this loop.
2:09
For now, let's name
our integer tacosEaten.
2:13
And like we did with the
cookies before, let's set it
2:17
equal to zero, because we just sat down,
we haven't eaten anything yet.
2:19
We end this
2:24
first expression with a semicolon.
2:25
Okay, now the second expression,
the conditional.
2:28
This is where we tell the loop
when it stops.
2:31
This is going to be checked
before every cycle of this loop.
2:34
So real quick, let's create a variable up
top here
2:38
and say int total tacos equals 3.
2:41
This is the max amount
we're allowed to eat.
2:45
Bummer, I know.
2:47
Alright, after the first expression's
semicolon, we'll write tacos eaten
2:49
less than total tacos
and add another semicolon.
2:54
So far so good, right?
2:59
Let's keep pushing
and we'll break it all down again later.
3:01
Okay, the final condition is the increment
or stepping expression.
3:04
This is run after every completed run
through the loop.
3:09
We want to increment our counter here,
so we'll say
3:12
tacosEaten++, and we don't need
a semicolon after this one.
3:15
Now this is
3:20
very long, and that's partially my fault.
3:20
No need to follow me here but real quick
I'll show you what you typically see with
3:24
for loops out there in the wild.
Usually you have int i equals zero
3:27
i is less than something,
usually an array's
3:34
length, i plus plus.
3:37
So it's typically much shorter,
but I'm trying to be verbose
3:40
and very clear here
for your first introduction.
3:43
Okay, inside the body,
I'll just go system.out.println.
3:46
Ooh, a taco.
3:50
Om, nom, nom, nom, nom.
3:52
Eating sounds there, you know.
3:54
All right, let's break
this whole thing down again.
3:56
We write our for.
3:59
Then as our first expression,
we initialize
4:01
a counter variable as an integer,
and we start it at 0.
4:03
This expression
only runs once, before anything else.
4:07
The second expression is then checked.
4:11
Is 0 less than 3?
4:13
Yes, it is. So let's run our code below.
4:15
We'll print that line, and afterwards,
4:18
we come back up to this third expression,
incrementing our variable.
4:21
So now it's equal to 1.
4:25
We go back here and check again.
4:27
Is 1 less than 3? Yep.
4:29
Let's run the code again.
4:31
Prints this line, goes up here,
and now our variable is 2.
4:33
Is 2 less than 3?
4:37
Indeed it is.
4:38
Let's print that line, increment
our variable, and now it's 3.
4:39
Is 3 less than 3? Nope.
4:44
Let's terminate this for loop
now, and it's done.
4:47
I hope that's clear,
and at least slightly makes sense.
4:50
Like I said, rewatch this video anytime
you need to, and when we get into arrays,
4:54
you'll get much more practical practice
with this.
4:58
Let's run this bad boy.
5:01
Clear and javac For.java
5:04
and java For. Okay.
5:08
Ooh a taco, three times. Perfect!
5:11
I really
want to drive home the syntax here
5:17
so let's do another example
in a slightly different way
5:19
to really show what you can do with these.
We'll be decrementing this time
5:22
and showing that variable as i,
like you'll see more often than not.
5:26
Let's pretend we work for a space station
and we're
5:30
counting down our space rocket launch.
5:33
Let's say for,
5:37
and this time we'll say int i equals 5.
5:39
For the condition,
we'll check before every loop
5:43
if i is greater than 0.
5:45
Lastly, we'll decrement this time with
5:49
i minus minus.
5:51
In the body, we'll say system.out.println
5:55
and just provide
i, which is our counter variable here.
5:58
So every loop,
it should be going down by 1 from 5.
6:01
After the loop closes,
we'll say system.out.print, lift off.
6:05
Alright, let's run this
6:11
again and see if it works.
6:12
Awesome.
6:17
We started at 5, went to 4, 3, 2, 1,
6:18
and after that loop, it decremented
i to 0.
6:21
Then it checked, is 0 greater than 0?
6:24
It realized
it wasn't and terminated the loop.
6:27
Whew!
6:30
That probably felt a lot, I know.
6:31
Try not to let this overwhelm
you too much.
6:33
As I stated earlier,
these become really useful when working
6:36
with arrays and other collections,
which you'll get into soon.
6:39
Now when you do get there,
this will look familiar
6:43
and it won't be such a brain shock
while also trying to learn arrays.
6:46
In the next video, we're going
to check out another way of iterating
6:50
over a collection,
but in a much simpler syntax.
6:53
I'll see you there.
6:56
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