Alright let's finish our for loop
tutorial and look at the do-while
statement in column. Now the do-while
loop is very similar to the while loop
but the conditions tested at the end of
the loop rather than at the beginning.
Now if you remember when I was talking
about the while loop I said that if the
condition initially evaluates to false
then the code in the loop won't be
executed the difference with the
do-while loop is that the code will
always execute at least once so let's
see that in action and we're going to go
back to our main function which I've got
on screen here so we left it fighting
the battle 10 times so we're going to
take out that code for the for loop and
then we're also going to delete all the
commented code at the style of the file
just to keep things a little bit clearer
I'd lead all this coming to that code
we're going to remove the for loop to
remove the right curly brace down here
for the code block so it's actually a
good idea after doing something like
I've just done to reformat the code so
the indentations put right at the moment
our codes are dented too far to the
right because I've commented out the for
loop and the codes basically in that
code block still in what was the code
block now Android studio will do that
for you automatically then go to the
code menu and select reformat code and
you can see Android studio is helpfully
move that cone over for us and if you're
going to do that a lot there's a
shortcut which is command alt L on a Mac
and on a PC it's ctrl alt L which works
fine on Windows but unfortunately only
bunchy Linux that's also the sequence to
lock your screen so you'll have to use
the menu all right so we've got a while
loop there that will keep bashing
Dracula until he runs away then run that
just to make sure that this wallet is
still working you can see that's working
quite successfully he's managed to run
away eventually at the bottom but what
happens if Dracula has known lives left
when we got to that bit of code we can
actually set Dracula's life to zero to
see what would happen
so we're printing out Dracula down here
or up here but it's changes put Dracula
dot lives equal zero
before the while loop and run that so
this time as you can see there's no
battle because the condition was false
at the start of the while loop on line 9
and because of that the code in the loop
isn't executed at all
so let's see how a do-while it behaves
differently it's very easy to change so
we can just cut out this while statement
make that a do you come down here to the
closing code block or closing right
curly brace and we're going to paste the
wallet back there so if you run this now
you can see that this time the code is
executed once this case you got away the
first time so I'll run it again run away
will try to continually run it to the
images to dodge
there we go so Dracula Dodgers so
basically Dracula is either dodging or
running away so each time the loop is
executed at least once and that's
because the condition Dracula lives
greater than zero down here in line
nineteen isn't tested until the end of
the loop so if it evaluates to true then
the loop goes around again otherwise the
program continues with the code after
the loop and prints the dashed line and
of course it never evaluates to true
here because director has got no lives
left because we set that to zero on line
seven and what we could do is see it
going around several times by giving
Dracula life back
each Tommy Dodgers so we can come back
here to the Dodge before the continue
line and put Dracula dot lives plus
equals one the obviously there's going
to be a random element here but if I run
it a few times we should see this code
executed directly run away Dodgers and
then he ran away so you can see that it
was executed more than once there so
that's showing that table viously this
code he worked and increased the last by
one and then because of the code that
we're testing for run away the last was
still listened to and that code was
executed in the loop was broken out of
so the loop actually went through twice
they can take a few guys because we need
two consecutive Dodgers before he stands
in the fight so we can try running that
again and see that food outcome so let's
just try doing that I think this isn't
right there we go there's a random
element here you see that he dodged
twice and therefore we got two lives
which meant that the runaway condition
was evaluated as false and he was able
to take damage and go through that fight
until eventually he lost laughs and then
ran away. So that's the do-while loop
it's not used as often as the while loop
but sometimes you will want to make sure
I live executed at least once and that's
where do-while comes in useful. I'll see
you in the next video.