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
Foreach loops are special loops intended for collections (like arrays).
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
We learned about for
loops in the previous video.
0:00
We wrote this for a loop so
0:03
that we could do something with all of
the invaders in the invaders array.
0:04
It turns out that looping through all the
items of a collection such as an array is
0:08
so common that there is yet another type
of loop that's designed to do just that.
0:12
It's called a for each loop.
0:18
I'll write what that looks like
below our existing four a loop, so
0:21
that we can compare the two loops.
0:24
With the four each loop we get rid of
the loop counter variable entirely.
0:30
This loop will always run if there's
at least one item in the array.
0:35
Each time through the loop,
0:40
the invader variable will be set
to the next item in the array.
0:41
So the first time through the loop,
0:45
invader will contain the first
invader in the array.
0:47
The second time through the loop it
will contain the second item, and so on.
0:51
Pretty neat?
0:56
Using for
0:57
each loops makes it clear that we want to
loop through every item in the collection.
0:58
We'll use this for each loop to look
at each invader in the array and
1:03
determine if it's in range of the tower.
1:06
We already know where the invader and
1:09
the towers are located because they
both have map location member variables.
1:11
We can use the distance to method to see
how far they are from each other and
1:16
then check if the distance is less
than the shooting range of the tower.
1:21
Why don't we write a method that
does this comparison for us?
1:25
We could put this method in the tower or
the invader classes.
1:29
However if we consider that this
method is really only determining if
1:33
two map locations are within
a certain distance of each other
1:37
I think it's best to put it
in the map location class.
1:41
Let's call this method in range of.
1:46
It will take another map location as
a parameter and an integer range.
1:48
Here we can call the distance to method we
already wrote to get the distance between
1:56
the map location object this method was
called on and the one that's passed in.
2:01
Then we'll check if that distance is less
than or equal to the range passed in.
2:07
We'll return the result
of this comparison.
2:12
Now back here in the tower
class we can write if our
2:14
location is in range of
2:20
the invaders location
using a range of one.
2:25
This one here is the width
of one grid square.
2:33
If the invader's in range,
we'll decrease its health.
2:38
This shows that the tower was able
to cause some damage to the invader.
2:40
And we'll have the invaders
health decreased by one.
2:44
As the code stands right now, the towers
can fire on every invader in range,
2:48
even if they've already
been neutralized or
2:53
even if they've made it
to the end of the path.
2:55
Let's fix this by only allowing them to
shoot at invaders that are still active
2:58
by checking the is active property we
previously added to the invader class.
3:02
So now the towers can only
decrease the health of an invader
3:07
if they're both active and
they're in range.
3:11
Finally let's handicap the towers a bit
3:14
by only letting them shoot
at one tower at a time.
3:17
We can do this by forcing the foreach
loop to exit after it's found and
3:21
shot one enemy.
3:25
For loops and for each loops just like
while loops, can use the break and
3:27
continue statements.
3:31
Remember the continue statement
like this causes the loop
3:33
to immediately go back to the beginning.
3:36
In the case of a for each loop,
the continue statement
3:39
causes the loop to move on to the next
item in the array or collection.
3:42
We don't want to use the continue
statement right here though.
3:48
I'm just showing it here, so
that you can see how it works.
3:50
Instead we want to break out of
the loop using the break statement.
3:53
There is nothing after the loop here.
3:58
So the method returns to
the code that called it.
4:00
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