1
00:00:04,730 --> 00:00:06,589
Alright let's finish our for loop

2
00:00:06,589 --> 00:00:08,690
tutorial and look at the do-while

3
00:00:08,690 --> 00:00:11,690
statement in column. Now the do-while

4
00:00:11,690 --> 00:00:13,129
loop is very similar to the while loop

5
00:00:13,129 --> 00:00:15,740
but the conditions tested at the end of

6
00:00:15,740 --> 00:00:17,630
the loop rather than at the beginning.

7
00:00:17,630 --> 00:00:19,880
Now if you remember when I was talking

8
00:00:19,880 --> 00:00:21,890
about the while loop I said that if the

9
00:00:21,890 --> 00:00:23,660
condition initially evaluates to false

10
00:00:23,660 --> 00:00:25,910
then the code in the loop won't be

11
00:00:25,910 --> 00:00:28,009
executed the difference with the

12
00:00:28,009 --> 00:00:29,540
do-while loop is that the code will

13
00:00:29,540 --> 00:00:32,630
always execute at least once so let's

14
00:00:32,630 --> 00:00:33,860
see that in action and we're going to go

15
00:00:33,860 --> 00:00:35,239
back to our main function which I've got

16
00:00:35,239 --> 00:00:39,740
on screen here so we left it fighting

17
00:00:39,740 --> 00:00:41,690
the battle 10 times so we're going to

18
00:00:41,690 --> 00:00:43,700
take out that code for the for loop and

19
00:00:43,700 --> 00:00:45,380
then we're also going to delete all the

20
00:00:45,380 --> 00:00:46,850
commented code at the style of the file

21
00:00:46,850 --> 00:00:50,380
just to keep things a little bit clearer

22
00:00:50,380 --> 00:00:54,070
I'd lead all this coming to that code

23
00:00:54,070 --> 00:00:57,399
we're going to remove the for loop to

24
00:00:57,399 --> 00:01:01,220
remove the right curly brace down here

25
00:01:01,220 --> 00:01:02,930
for the code block so it's actually a

26
00:01:02,930 --> 00:01:04,849
good idea after doing something like

27
00:01:04,849 --> 00:01:07,369
I've just done to reformat the code so

28
00:01:07,369 --> 00:01:09,799
the indentations put right at the moment

29
00:01:09,799 --> 00:01:11,569
our codes are dented too far to the

30
00:01:11,569 --> 00:01:13,159
right because I've commented out the for

31
00:01:13,159 --> 00:01:15,799
loop and the codes basically in that

32
00:01:15,799 --> 00:01:17,600
code block still in what was the code

33
00:01:17,600 --> 00:01:19,999
block now Android studio will do that

34
00:01:19,999 --> 00:01:21,709
for you automatically then go to the

35
00:01:21,709 --> 00:01:26,420
code menu and select reformat code and

36
00:01:26,420 --> 00:01:27,950
you can see Android studio is helpfully

37
00:01:27,950 --> 00:01:30,049
move that cone over for us and if you're

38
00:01:30,049 --> 00:01:31,219
going to do that a lot there's a

39
00:01:31,219 --> 00:01:33,920
shortcut which is command alt L on a Mac

40
00:01:33,920 --> 00:01:36,829
and on a PC it's ctrl alt L which works

41
00:01:36,829 --> 00:01:38,779
fine on Windows but unfortunately only

42
00:01:38,779 --> 00:01:40,880
bunchy Linux that's also the sequence to

43
00:01:40,880 --> 00:01:42,770
lock your screen so you'll have to use

44
00:01:42,770 --> 00:01:45,170
the menu all right so we've got a while

45
00:01:45,170 --> 00:01:47,479
loop there that will keep bashing

46
00:01:47,479 --> 00:01:49,999
Dracula until he runs away then run that

47
00:01:49,999 --> 00:01:51,259
just to make sure that this wallet is

48
00:01:51,259 --> 00:01:55,759
still working you can see that's working

49
00:01:55,759 --> 00:01:57,649
quite successfully he's managed to run

50
00:01:57,649 --> 00:01:59,719
away eventually at the bottom but what

51
00:01:59,719 --> 00:02:01,490
happens if Dracula has known lives left

52
00:02:01,490 --> 00:02:04,189
when we got to that bit of code we can

53
00:02:04,189 --> 00:02:05,899
actually set Dracula's life to zero to

54
00:02:05,899 --> 00:02:06,770
see what would happen

55
00:02:06,770 --> 00:02:08,899
so we're printing out Dracula down here

56
00:02:08,899 --> 00:02:12,430
or up here but it's changes put Dracula

57
00:02:12,430 --> 00:02:16,890
dot lives equal zero

58
00:02:16,890 --> 00:02:24,720
before the while loop and run that so

59
00:02:24,720 --> 00:02:26,040
this time as you can see there's no

60
00:02:26,040 --> 00:02:27,990
battle because the condition was false

61
00:02:27,990 --> 00:02:30,420
at the start of the while loop on line 9

62
00:02:30,420 --> 00:02:32,730
and because of that the code in the loop

63
00:02:32,730 --> 00:02:34,410
isn't executed at all

64
00:02:34,410 --> 00:02:36,450
so let's see how a do-while it behaves

65
00:02:36,450 --> 00:02:39,960
differently it's very easy to change so

66
00:02:39,960 --> 00:02:43,550
we can just cut out this while statement

67
00:02:43,550 --> 00:02:48,630
make that a do you come down here to the

68
00:02:48,630 --> 00:02:51,209
closing code block or closing right

69
00:02:51,209 --> 00:02:53,160
curly brace and we're going to paste the

70
00:02:53,160 --> 00:02:59,030
wallet back there so if you run this now

71
00:02:59,030 --> 00:03:01,380
you can see that this time the code is

72
00:03:01,380 --> 00:03:04,080
executed once this case you got away the

73
00:03:04,080 --> 00:03:12,020
first time so I'll run it again run away

74
00:03:12,020 --> 00:03:14,550
will try to continually run it to the

75
00:03:14,550 --> 00:03:19,750
images to dodge

76
00:03:19,750 --> 00:03:23,000
there we go so Dracula Dodgers so

77
00:03:23,000 --> 00:03:24,470
basically Dracula is either dodging or

78
00:03:24,470 --> 00:03:26,989
running away so each time the loop is

79
00:03:26,989 --> 00:03:29,209
executed at least once and that's

80
00:03:29,209 --> 00:03:31,310
because the condition Dracula lives

81
00:03:31,310 --> 00:03:33,110
greater than zero down here in line

82
00:03:33,110 --> 00:03:35,660
nineteen isn't tested until the end of

83
00:03:35,660 --> 00:03:38,450
the loop so if it evaluates to true then

84
00:03:38,450 --> 00:03:39,980
the loop goes around again otherwise the

85
00:03:39,980 --> 00:03:41,959
program continues with the code after

86
00:03:41,959 --> 00:03:44,209
the loop and prints the dashed line and

87
00:03:44,209 --> 00:03:45,860
of course it never evaluates to true

88
00:03:45,860 --> 00:03:48,200
here because director has got no lives

89
00:03:48,200 --> 00:03:50,540
left because we set that to zero on line

90
00:03:50,540 --> 00:03:53,180
seven and what we could do is see it

91
00:03:53,180 --> 00:03:54,680
going around several times by giving

92
00:03:54,680 --> 00:03:55,940
Dracula life back

93
00:03:55,940 --> 00:03:57,829
each Tommy Dodgers so we can come back

94
00:03:57,829 --> 00:04:00,590
here to the Dodge before the continue

95
00:04:00,590 --> 00:04:04,760
line and put Dracula dot lives plus

96
00:04:04,760 --> 00:04:07,190
equals one the obviously there's going

97
00:04:07,190 --> 00:04:08,750
to be a random element here but if I run

98
00:04:08,750 --> 00:04:10,280
it a few times we should see this code

99
00:04:10,280 --> 00:04:17,930
executed directly run away Dodgers and

100
00:04:17,930 --> 00:04:19,130
then he ran away so you can see that it

101
00:04:19,130 --> 00:04:21,380
was executed more than once there so

102
00:04:21,380 --> 00:04:22,639
that's showing that table viously this

103
00:04:22,639 --> 00:04:24,289
code he worked and increased the last by

104
00:04:24,289 --> 00:04:26,120
one and then because of the code that

105
00:04:26,120 --> 00:04:27,710
we're testing for run away the last was

106
00:04:27,710 --> 00:04:30,289
still listened to and that code was

107
00:04:30,289 --> 00:04:31,910
executed in the loop was broken out of

108
00:04:31,910 --> 00:04:34,000
so the loop actually went through twice

109
00:04:34,000 --> 00:04:36,500
they can take a few guys because we need

110
00:04:36,500 --> 00:04:38,780
two consecutive Dodgers before he stands

111
00:04:38,780 --> 00:04:40,160
in the fight so we can try running that

112
00:04:40,160 --> 00:04:44,690
again and see that food outcome so let's

113
00:04:44,690 --> 00:04:47,090
just try doing that I think this isn't

114
00:04:47,090 --> 00:04:48,139
right there we go there's a random

115
00:04:48,139 --> 00:04:49,789
element here you see that he dodged

116
00:04:49,789 --> 00:04:51,470
twice and therefore we got two lives

117
00:04:51,470 --> 00:04:53,510
which meant that the runaway condition

118
00:04:53,510 --> 00:04:55,460
was evaluated as false and he was able

119
00:04:55,460 --> 00:04:56,930
to take damage and go through that fight

120
00:04:56,930 --> 00:04:59,090
until eventually he lost laughs and then

121
00:04:59,090 --> 00:05:01,430
ran away. So that's the do-while loop

122
00:05:01,430 --> 00:05:03,800
it's not used as often as the while loop

123
00:05:03,800 --> 00:05:06,229
but sometimes you will want to make sure

124
00:05:06,229 --> 00:05:09,260
I live executed at least once and that's

125
00:05:09,260 --> 00:05:12,050
where do-while comes in useful. I'll see

126
00:05:12,050 --> 00:05:14,770
you in the next video.

