in the previous video we looked at
looping to repeat a block of code using
the while loop. In this video we're going
to review another kind of loop called a
for loop. Now a for loop does a similar
job to a while loop but it's generally
used when you have some sort of limit
that determines how many times you need
to repeat a block of code so we're a
while loop we'll keep looping as long as
the condition remains true a for loop
will execute a set number of times now
we've already looked at the for loop
when we created a show inventory
function in our player class it's a very
basic loop that just iterates over each
item in the array list and prints the
items details so let's actually go ahead
and make it do a bit more now s our
players wander around killing monsters
they'll pick up loot throughout the game
so let's get the loop to calculate a
score by adding up the value of all
their loot at the end of the previous
video they just chase the Dracula away
from his hoard which means there should
be a fair bit of loot for them to
collect what I'm gonna do is comment out
the world loop in our main function
to keep the app with a bit clearer and
then we'll get the player to pick up
Dracula's hoard well so what we'll do is
we'll also comment out all this code up
here the troll code as well so comment
all that out and on the wallet that we
added in the last video let's go ahead
and add a new player so Val Kernan
equals player parentheses after the
double and double quotes Conan Conan dot
get loot parentheses loot parentheses
invisibility comma loop type dot armor
comma 4.0 let's copy at this line I'll
make a few more changes there's another
nine items but obviously change them as
we go through so we'll do invisibility
for the first one mithril for the second one
third one being of speed, north one red
potion cursed shield brass ring
well mostly missing game type mixing
game types here some of this stuff is
from different games but that's a run at
all served well as an example chain
maille gold ring health potion and
silver ring is the last one for what we
need to change the item top so which was
also am i sorry you can leave that one
third one ring of spain is obviously a
ring red potion obviously is a potion
curse shield well that's obviously armor
got a brass ring obviously a ring
chainmail armor gold ring is a ring
health potion potion and silver ring is
obviously a ring the last thing I need
to do now is give the values to them so
invisibility that should have been a
potion as wealth has changed that
obviously the visibility's not armor
okay so 4.04 the first one we're going
to permit your alarm we're going to give
that 183 it's the score ring of speed
let's make that 25 okay red potion will
go with to curse shield that's a bad
thing so we're going to go a negative
eight brass ring value of 1 chainmail
let's go with a bail of 4 for that solid
then on the default gold ring let's
change the fourth over 12 health potion
let's go with 3 for that then the silver ring
lastly we'll go with a value of 6 okay
and then lastly as to a conan dots show
inventory okay let's run that
you can see there that Conan is they've
got quite a stash of loot but what's all
the loot worth
so let's modify the for loop to loop
through all the items in Conan's stash
and work out the total value so we're
gonna go back to the show inventory
function in the player class ok and what
I'm gonna do start out by making a
mistake here because it's such a common
mistake to make and it's a minor thing
so as long as you can spawn it quickly
you can get back to writing code without
losing your train of thought so what
we're going to do after the printing of
the items will actually print learn of
the item we're going to put total plus
equals item value but we also need to
add that variable so we add that at the
first line of the supplementary function
they're total equals zero so we come
down here and then a cup of lines total
score is : dollar total so you can see
that on the first line line 47 I started
out by creating gather variable total
and it's set it initially to zero will
be accumulating the total value of the
loot in this variable but the mistake I
made was to initialize it to zero this
is the code here on line 47 by doing
that I'll make it an int so we get an error
we try to add a double value to it down
here you can see their type mismatch
quiet int found double even though the
error is showing here the actual problem
was caused by us initializing it to zero
chance of to 0.0 you can see the error
and then disappears so that's a very
common mistake to make as I said so
watch out for that and that a very easy
solution is you can see up there just
making sure that you're initializing it
to the correct type all right so each
time around the loop we had the items
value to total that way we build up the
total value of our inventory and when
the loop terminates been printing out
the total value of all the loot so we
actually run this now
you can see we got the total score the
total loot value if you will of 232
that's the total value of all the loot
now being of a suspicious disposition
Conan examined that cursed shield
carefully and decided to leave it in the
Dracula's lair so I'm going to comment
that line out and I've actually run this
again we should fire that there's less
items now in the inventory fewer items
and the loop goes around 9 times instead
of 10 and the score has now gone up to
240 of course because we've removed the
curse shield which was adding negative 8
to our total so that's a for loop in
Kotlin other languages such as c plus
possible jar i have another form of for
loop in fact this form is called the for
each in those languages but Kotlin
doesn't have another form in Kotlin of
for loop works by iterating over a set
of values if you don't have a set of
values to iterate over i just want to
repeat a block of code a certain number
of times what you can do is create a
range of values so to see what I mean
let's comment out all this conan code
you know main method our main function
let's put back the Dracula slaying code
so just as code here that starts with
the vampire king dracula will uncomment
that we're also going to change it in
line 23 that was take was inflicting 12
points of damage has changed that number
to 80 then we actually run this she'll
be able to see the damage being
inflicted and directly eventually runs
away so you can see now there's a random
element to it when Dracula dodges so you
may want to simulate the battle a few
times you can see in this particular
case he didn't dodge at all so we try
running it again this time he did manage
to do some dodging
okay eventually runs away now we could
run it repeatedly but just to show what
I mean about creating a range I'm gonna
get the program to run all this code ten
times only so what we need to do to do
that is we want to wrap all this code in
a for loop so I'm going to go ahead and
create the for loop so for parentheses I
in one space two dots two periods in
other words ten and if you create a code
block I'm going to put all that code in
that code block okay cut that paste it
into that code block we created for the
for loop and what I'm going to do is
just add a row of dashes just to make it
easier to see when each batter wins so
I'm going to put that in here before the
closing for loop printing this would add
some dashes here so now when I run the
program we get ten runs of the battle
with Dracula should be dodging at
different times in each one you can see
each batter was separated by the dashes
that we added
and so on run up to the top so we had to
provide a range of values for the for
loop to iterate over we did that on line
13 because there's no list to loop
through. Alright so that's the for loop
so I'm going to stop the video here and
in the next one we'll look at the third
top of loop, we'll be using the do-while
loop, so I'll see you in the next video.