1
00:00:04,730 --> 00:00:06,979
In the next few videos, we're going to

2
00:00:06,979 --> 00:00:09,410
look at loops in Kotlin. Now we've

3
00:00:09,410 --> 00:00:11,570
already seen the for loop, but Kotlin has

4
00:00:11,570 --> 00:00:14,030
other kinds of loops, and in the next few

5
00:00:14,030 --> 00:00:15,139
videos we're going to take a look at

6
00:00:15,139 --> 00:00:16,910
these, as well as revisiting the for loop,

7
00:00:16,910 --> 00:00:18,860
briefly. Now we're going to start off

8
00:00:18,860 --> 00:00:21,110
with the while loop. Now the while

9
00:00:21,110 --> 00:00:23,450
statement executes code continually, as

10
00:00:23,450 --> 00:00:26,300
long as some expression is true. So in

11
00:00:26,300 --> 00:00:28,009
other words, while an expression is true,

12
00:00:28,009 --> 00:00:31,189
the loop will continue looping. Now when

13
00:00:31,189 --> 00:00:32,750
the condition becomes false, the loop

14
00:00:32,750 --> 00:00:35,870
terminates. So looking at our example in

15
00:00:35,870 --> 00:00:39,290
our demo class, our Main.kt, we've

16
00:00:39,290 --> 00:00:41,060
got one of the super vampire objects -

17
00:00:41,060 --> 00:00:42,620
this one down the bottom here, Dracula.

18
00:00:42,620 --> 00:00:44,360
And you can see that I've run the

19
00:00:44,360 --> 00:00:46,370
program to remind us what it's doing, and

20
00:00:46,370 --> 00:00:48,230
we can see that Dracula, being created

21
00:00:48,230 --> 00:00:50,329
with 140 hit points - you can see that

22
00:00:50,329 --> 00:00:52,489
down here in the output - and three lives.

23
00:00:52,489 --> 00:00:55,160
And we're then inflicting twelve points of

24
00:00:55,160 --> 00:00:57,440
damage, but being a King vampire, Dracula

25
00:00:57,440 --> 00:00:59,210
only takes a quarter of that - three

26
00:00:59,210 --> 00:01:01,730
points of damage - and he's also got three

27
00:01:01,730 --> 00:01:04,938
lives. Now the Players in this game will

28
00:01:04,938 --> 00:01:06,320
have to find some pretty impressive

29
00:01:06,320 --> 00:01:08,600
weapons to deal with this monster, but

30
00:01:08,600 --> 00:01:10,549
we're going to take him out using a

31
00:01:10,549 --> 00:01:13,249
while loop. Now as I said, a while loop

32
00:01:13,249 --> 00:01:15,259
can be used to keep repeating a section,

33
00:01:15,259 --> 00:01:17,210
or block of code, until some condition

34
00:01:17,210 --> 00:01:20,450
becomes false. So let's wrap the call to

35
00:01:20,450 --> 00:01:22,549
take damage in a while loop, and

36
00:01:22,549 --> 00:01:24,799
repeatedly inflict damage on Dracula,

37
00:01:24,799 --> 00:01:27,560
until we slay him. Now the condition we want

38
00:01:27,560 --> 00:01:30,200
to test is that Dracula isn't dead. When

39
00:01:30,200 --> 00:01:31,459
Dracula becomes dead,

40
00:01:31,459 --> 00:01:33,469
we can stop looping. So what we want to

41
00:01:33,469 --> 00:01:36,259
do is keep our loop repeating, while

42
00:01:36,259 --> 00:01:38,119
Dracula still has lives left.

43
00:01:38,119 --> 00:01:40,369
When Dracula's lives property reaches

44
00:01:40,369 --> 00:01:42,770
zero, we can stop. So let's have a look at

45
00:01:42,770 --> 00:01:44,329
that code. So we're just going to come

46
00:01:44,329 --> 00:01:47,599
down here, after the println. We're

47
00:01:47,599 --> 00:01:50,349
going to put a while in there; while

48
00:01:50,349 --> 00:01:56,029
dracula.lives is greater than zero.

49
00:01:56,029 --> 00:01:58,069
Then open up a code block, and we'll put

50
00:01:58,069 --> 00:02:01,549
the takeDamage in that code block. So in

51
00:02:01,549 --> 00:02:02,869
other words, so it becomes part of the

52
00:02:02,869 --> 00:02:09,280
loop. If we run this. Now

53
00:02:09,280 --> 00:02:11,510
you can see, looking at the output, it

54
00:02:11,510 --> 00:02:14,060
took quite some time - in other words,

55
00:02:14,060 --> 00:02:15,379
quite a lot of attacks - before we get

56
00:02:15,379 --> 00:02:19,159
Dracula's lives down to zero. So a while

57
00:02:19,159 --> 00:02:20,900
statement starts off with the word while,

58
00:02:20,900 --> 00:02:23,599
as you can see on line 15, and then in

59
00:02:23,599 --> 00:02:25,060
brackets, there's a boolean expression.

60
00:02:25,060 --> 00:02:27,290
And what I mean by that is, it has to

61
00:02:27,290 --> 00:02:30,650
evaluate to true or false. So while the

62
00:02:30,650 --> 00:02:32,780
condition is true, the code in the

63
00:02:32,780 --> 00:02:34,129
brackets, the parentheses, will

64
00:02:34,129 --> 00:02:36,560
continually be executed until either the

65
00:02:36,560 --> 00:02:39,049
boolean expression becomes false - and you

66
00:02:39,049 --> 00:02:41,090
set that in the code, perhaps - or a break

67
00:02:41,090 --> 00:02:42,919
is found in the code. And I'm going to

68
00:02:42,919 --> 00:02:44,389
show you an example of a break as well.

69
00:02:44,389 --> 00:02:46,340
So while is very useful for doing

70
00:02:46,340 --> 00:02:48,650
things like cycling or waiting for a

71
00:02:48,650 --> 00:02:51,019
particular event to happen - for example,

72
00:02:51,019 --> 00:02:52,510
while there's still data in a file,

73
00:02:52,510 --> 00:02:55,579
continue reading from that file. It's

74
00:02:55,579 --> 00:02:57,109
very useful to continually be checking

75
00:02:57,109 --> 00:02:58,750
something until a particular condition

76
00:02:58,750 --> 00:03:01,760
changes. Now in our example, Dracula's

77
00:03:01,760 --> 00:03:03,379
lives are checked each time around the

78
00:03:03,379 --> 00:03:06,139
loop. If Dracula still has lives left, the

79
00:03:06,139 --> 00:03:08,989
code inside the loop gets executed. Once

80
00:03:08,989 --> 00:03:11,060
all the code's executed - we only have

81
00:03:11,060 --> 00:03:12,650
one line of code in here, which you can see on

82
00:03:12,650 --> 00:03:15,260
line 16, in our loop, but there could

83
00:03:15,260 --> 00:03:17,060
be many more. Once all that code's

84
00:03:17,060 --> 00:03:19,639
executed, the code's checked again to see

85
00:03:19,639 --> 00:03:21,799
if it's still true. Now

86
00:03:21,799 --> 00:03:23,060
Dracula may have been a very powerful

87
00:03:23,060 --> 00:03:25,400
enemy, with three lives and 140 hit

88
00:03:25,400 --> 00:03:27,439
points, but the outcome wasn't in

89
00:03:27,439 --> 00:03:29,510
question. Our wallet will keep going

90
00:03:29,510 --> 00:03:31,760
around until Dracula's lives drop to

91
00:03:31,760 --> 00:03:34,340
zero. So the condition in our loop is

92
00:03:34,340 --> 00:03:37,370
this here, in parentheses; dracula dot

93
00:03:37,370 --> 00:03:39,290
lives greater than zero.

94
00:03:39,290 --> 00:03:41,780
While that evaluates to true, we keep

95
00:03:41,780 --> 00:03:44,720
looping and inflict more damage. And you

96
00:03:44,720 --> 00:03:48,199
can see as I scroll up in the output, the

97
00:03:48,199 --> 00:03:49,930
first life lasted quite a while -

98
00:03:49,930 --> 00:03:52,069
basically, as the hit points dropped from

99
00:03:52,069 --> 00:03:54,919
140 by three each time. Now when the hit

100
00:03:54,919 --> 00:03:56,930
points Dracula has left would have

101
00:03:56,930 --> 00:03:58,849
dropped below zero, he loses a life

102
00:03:58,849 --> 00:04:01,009
instead. So the last two lives were lost,

103
00:04:01,009 --> 00:04:03,739
consequently, very quickly. The important

104
00:04:03,739 --> 00:04:05,209
thing, though, is that the code kept

105
00:04:05,209 --> 00:04:07,759
executing, as long as Dracula still had

106
00:04:07,759 --> 00:04:09,169
at least one life left.

107
00:04:09,169 --> 00:04:12,019
Once the lives became zero, the condition

108
00:04:12,019 --> 00:04:14,299
is no longer true, and the while loop

109
00:04:14,299 --> 00:04:17,329
terminates. So that's a while loop. As

110
00:04:17,329 --> 00:04:19,250
long as the condition evaluates to true,

111
00:04:19,250 --> 00:04:22,820
all this code between these curly braces -

112
00:04:22,820 --> 00:04:24,950
in this case, it's just a single line, but

113
00:04:24,950 --> 00:04:27,320
it could again, be multiple lines - all

114
00:04:27,320 --> 00:04:28,940
that code between those curly braces

115
00:04:28,940 --> 00:04:31,610
keeps executing. When something happens

116
00:04:31,610 --> 00:04:33,560
to make the condition evaluate to false,

117
00:04:33,560 --> 00:04:36,350
the while loop stops looping and

118
00:04:36,350 --> 00:04:38,830
terminates. Now the code between the curly

119
00:04:38,830 --> 00:04:41,510
braces is called a block. So whenever I

120
00:04:41,510 --> 00:04:43,010
talk about a code block or a block of

121
00:04:43,010 --> 00:04:46,880
code, I mean code inside curly braces. So

122
00:04:46,880 --> 00:04:49,130
looking at this entire main method, you

123
00:04:49,130 --> 00:04:50,840
can see that if I select all that

124
00:04:50,840 --> 00:04:52,700
code there between these starting and

125
00:04:52,700 --> 00:04:55,580
closing curly braces, is also a block

126
00:04:55,580 --> 00:04:57,800
of code. So you can see that a block of

127
00:04:57,800 --> 00:04:59,630
code, or a code block can contain other

128
00:04:59,630 --> 00:05:01,000
code blocks.

129
00:05:01,000 --> 00:05:03,290
Okay, so there's another way to get out

130
00:05:03,290 --> 00:05:05,030
of a while loop, and that's to break out

131
00:05:05,030 --> 00:05:06,920
of it. In fact, you can break out of any

132
00:05:06,920 --> 00:05:09,230
loop, but we'll look at this first with

133
00:05:09,230 --> 00:05:11,450
our while loop. So to demonstrate break,

134
00:05:11,450 --> 00:05:13,520
we're going to give Dracula another

135
00:05:13,520 --> 00:05:15,620
method - or more accurately, we're going to

136
00:05:15,620 --> 00:05:17,390
give the VampyreKing class another

137
00:05:17,390 --> 00:05:19,820
method. Now, you don't get to live as long

138
00:05:19,820 --> 00:05:21,440
as Dracula without having a good sense

139
00:05:21,440 --> 00:05:23,480
of self-preservation, so we're going to

140
00:05:23,480 --> 00:05:26,320
give our VampyreKings a runAway method.

141
00:05:26,320 --> 00:05:28,670
So let's start off - we go to our Vampyre

142
00:05:28,670 --> 00:05:30,560
King. We're going to write this function

143
00:05:30,560 --> 00:05:32,030
in a naive way, and then we're going to

144
00:05:32,030 --> 00:05:34,730
change it to more Kotlin-like code. So

145
00:05:34,730 --> 00:05:36,560
come down here below the takeDamage for

146
00:05:36,560 --> 00:05:39,290
Vampyreking, and we're going to create

147
00:05:39,290 --> 00:05:42,550
this function; fun runAway

148
00:05:42,550 --> 00:05:49,520
parenthesis colon Boolean, left and right

149
00:05:49,520 --> 00:05:51,430
curly braces. We're going to put if

150
00:05:51,430 --> 00:05:56,600
parentheses lives is less than 2, return

151
00:05:56,600 --> 00:06:00,320
true - and have an else outside of the

152
00:06:00,320 --> 00:06:01,730
code block and create another code block

153
00:06:01,730 --> 00:06:05,210
with it after the else - return false. So

154
00:06:05,210 --> 00:06:07,040
it's a very simple method, that returns

155
00:06:07,040 --> 00:06:09,470
true if the number of lives drops below

156
00:06:09,470 --> 00:06:13,130
2, and false otherwise. Now as I

157
00:06:13,130 --> 00:06:14,240
mentioned, I've written that code in

158
00:06:14,240 --> 00:06:16,310
quite a long way. In fact, Android Studio,

159
00:06:16,310 --> 00:06:20,570
if we come over here, is actually

160
00:06:20,570 --> 00:06:23,270
complaining a little bit, to Remove the

161
00:06:23,270 --> 00:06:25,730
redundant if statement. So what we can do

162
00:06:25,730 --> 00:06:27,500
is use that light bulb to convert this

163
00:06:27,500 --> 00:06:29,330
into a more idiomatic form.

164
00:06:29,330 --> 00:06:32,630
So we could do, click on that, and that

165
00:06:32,630 --> 00:06:34,220
does exactly the same thing but in a

166
00:06:34,220 --> 00:06:36,889
less verbose way. So if lives is less than 2,

167
00:06:36,889 --> 00:06:39,710
it'll return true, otherwise it returns

168
00:06:39,710 --> 00:06:41,389
false, which does exactly the same thing.

169
00:06:41,389 --> 00:06:44,030
So the condition, lives less than 2,

170
00:06:44,030 --> 00:06:46,400
evaluates to true or false. So we can

171
00:06:46,400 --> 00:06:48,349
just return the result of evaluating the

172
00:06:48,349 --> 00:06:50,060
expression, instead of having to

173
00:06:50,060 --> 00:06:52,460
explicitly test it. Now if you prefer to

174
00:06:52,460 --> 00:06:54,469
write code like this longhand, until you

175
00:06:54,469 --> 00:06:55,969
get the hang of it - that's that first

176
00:06:55,969 --> 00:06:57,710
example, before we just converted it to

177
00:06:57,710 --> 00:06:59,930
this more idiomatic format - that's fine,

178
00:06:59,930 --> 00:07:01,699
but be aware that a lot of code you read

179
00:07:01,699 --> 00:07:03,050
will do it this shorter way, as I've

180
00:07:03,050 --> 00:07:04,250
shown on line 15.

181
00:07:04,250 --> 00:07:07,069
Okay, so Dracula now runs away if he

182
00:07:07,069 --> 00:07:09,169
loses too many lives. So let's test that

183
00:07:09,169 --> 00:07:14,000
in our while loop. So we're gonna go back to Main. So

184
00:07:14,000 --> 00:07:15,560
we've still got our while dracula's lives is

185
00:07:15,560 --> 00:07:17,240
greater than zero, and what we're going

186
00:07:17,240 --> 00:07:20,240
to do here before the takeDamage, we're

187
00:07:20,240 --> 00:07:23,409
going to put if, then parenthesis,

188
00:07:23,409 --> 00:07:31,159
dracula.runAway. And then add

189
00:07:31,159 --> 00:07:35,599
a code block, so println Dracula

190
00:07:35,599 --> 00:07:37,120
ran away.

191
00:07:37,120 --> 00:07:40,789
We'll put else, then we'll wrap the take

192
00:07:40,789 --> 00:07:44,379
Damage in another code block as well,

193
00:07:44,379 --> 00:07:47,629
like so. So let's just try running this

194
00:07:47,629 --> 00:07:51,950
and confirm that it works.

195
00:07:51,950 --> 00:07:53,840
Now notice that what's happening

196
00:07:53,840 --> 00:07:56,120
here; Dracula runs away when his

197
00:07:56,120 --> 00:07:58,610
lives drops below 2, but once he's run

198
00:07:58,610 --> 00:08:00,530
away the condition can never become true,

199
00:08:00,530 --> 00:08:02,810
because we can't inflict any more damage.

200
00:08:02,810 --> 00:08:04,730
So I'm gonna stop the program, at this point,

201
00:08:04,730 --> 00:08:06,140
by clicking on this red square in the

202
00:08:06,140 --> 00:08:08,390
top left-hand corner. So you can see that

203
00:08:08,390 --> 00:08:09,800
the loop has got out of control and it's just

204
00:08:09,800 --> 00:08:11,540
looping around and around forever. So

205
00:08:11,540 --> 00:08:14,660
I'll scroll up a bit - quite

206
00:08:14,660 --> 00:08:15,920
a bit actually, because we let it run for

207
00:08:15,920 --> 00:08:18,170
quite a while - right up towards the top.

208
00:08:18,170 --> 00:08:20,300
In fact, it's actually cleared out the

209
00:08:20,300 --> 00:08:21,530
run buffer so we can't see anything

210
00:08:21,530 --> 00:08:23,390
above that. Basically, it's gone around

211
00:08:23,390 --> 00:08:25,400
hundreds of times, while I was talking

212
00:08:25,400 --> 00:08:27,680
here. So this is called an infinite loop,

213
00:08:27,680 --> 00:08:30,530
and our program was stuck in it. Now

214
00:08:30,530 --> 00:08:31,670
because there was no way for the

215
00:08:31,670 --> 00:08:33,799
condition to become false, it just kept

216
00:08:33,799 --> 00:08:36,080
on looping. So what we want to do is

217
00:08:36,080 --> 00:08:38,150
somehow break out of the loop, when

218
00:08:38,150 --> 00:08:40,820
Dracula runs away. So we can do that with

219
00:08:40,820 --> 00:08:43,700
a break statement. So after the code that

220
00:08:43,700 --> 00:08:45,620
says, or that actually prints out the fact

221
00:08:45,620 --> 00:08:47,810
that Dracula ran away, we're going to add

222
00:08:47,810 --> 00:08:50,180
a break there. So then a break on that

223
00:08:50,180 --> 00:08:53,650
next line. And now if we run this again,

224
00:08:53,650 --> 00:08:55,760
we should finally no longer get an

225
00:08:55,760 --> 00:08:58,280
infinite loop. And you can see what

226
00:08:58,280 --> 00:09:00,080
happened was; lost a life, lost a life and

227
00:09:00,080 --> 00:09:02,390
at that point, lives was less than 2. So

228
00:09:02,390 --> 00:09:03,980
Dracula ran away and the program now

229
00:09:03,980 --> 00:09:05,660
exits because we've added this break

230
00:09:05,660 --> 00:09:08,330
statement on line 18. So basically, Kotlin

231
00:09:08,330 --> 00:09:09,800
jumped out of the loop at that point, and

232
00:09:09,800 --> 00:09:11,240
prevented us from getting stuck going

233
00:09:11,240 --> 00:09:13,790
round and round forever. So you can see

234
00:09:13,790 --> 00:09:15,890
that break's very useful, when you need to

235
00:09:15,890 --> 00:09:17,720
terminate a loop without waiting for the

236
00:09:17,720 --> 00:09:20,570
condition to become false. Now that can

237
00:09:20,570 --> 00:09:22,070
happen for all sorts of reasons. I

238
00:09:22,070 --> 00:09:23,870
mentioned using a while loop to keep

239
00:09:23,870 --> 00:09:25,520
reading from a file, as long as there's data

240
00:09:25,520 --> 00:09:28,100
available, as an example. Now if we were

241
00:09:28,100 --> 00:09:29,720
searching for something in the

242
00:09:29,720 --> 00:09:31,340
file, you wouldn't want to keep reading

243
00:09:31,340 --> 00:09:33,230
the data after finding it. So in that

244
00:09:33,230 --> 00:09:35,510
case, you could use break to terminate

245
00:09:35,510 --> 00:09:36,710
the loop, instead of having to read

246
00:09:36,710 --> 00:09:39,080
through to the end of the file. Now we'll

247
00:09:39,080 --> 00:09:40,610
be seeing break used often in the

248
00:09:40,610 --> 00:09:41,870
course, so you'll see plenty of examples

249
00:09:41,870 --> 00:09:44,810
of it in real use. Now there's something else

250
00:09:44,810 --> 00:09:46,130
you can do in a loop, and that's to

251
00:09:46,130 --> 00:09:48,920
continue. So basically, any time you

252
00:09:48,920 --> 00:09:50,870
execute a continue statement, the code

253
00:09:50,870 --> 00:09:52,970
goes back to the top of the loop, without

254
00:09:52,970 --> 00:09:54,890
executing any more of the code in the

255
00:09:54,890 --> 00:09:57,320
loop. So continue isn't used as often as

256
00:09:57,320 --> 00:09:58,850
break, but it can be useful in some

257
00:09:58,850 --> 00:10:01,880
situations. To see how we could use

258
00:10:01,880 --> 00:10:04,640
continue, let's give Dracula the ability

259
00:10:04,640 --> 00:10:06,050
to dodge some blows.

260
00:10:06,050 --> 00:10:08,000
Now games such as this are normally

261
00:10:08,000 --> 00:10:09,620
played using dice, so we need to

262
00:10:09,620 --> 00:10:11,540
introduce a bit of chance into the game.

263
00:10:11,540 --> 00:10:13,640
So I'm going to use another Java class

264
00:10:13,640 --> 00:10:15,830
called Random, which we'll use to give

265
00:10:15,830 --> 00:10:18,260
Dracula a chance to dodge a blow. So what

266
00:10:18,260 --> 00:10:19,520
we're going to do is add a dodges

267
00:10:19,520 --> 00:10:21,980
function to the VampyreKing class, and

268
00:10:21,980 --> 00:10:23,450
that's going to return true if the King

269
00:10:23,450 --> 00:10:25,880
vampire dodges successfully, and false

270
00:10:25,880 --> 00:10:28,220
otherwise. So going back to VampyreKing,

271
00:10:28,220 --> 00:10:32,210
below the runAway function, add a

272
00:10:32,210 --> 00:10:35,260
definition for dodges, function dodges,

273
00:10:35,260 --> 00:10:39,560
parentheses colon Boolean, left and right

274
00:10:39,560 --> 00:10:41,720
curly braces. Then we're going to type

275
00:10:41,720 --> 00:10:45,320
val rand equals, Random with a capital R

276
00:10:45,320 --> 00:10:49,640
in parentheses. Then val chance equals 

277
00:10:49,640 --> 00:10:53,830
rand.nextInt and 3 in parentheses.

278
00:10:53,830 --> 00:10:56,480
Then we're going to put, actually, what we'll

279
00:10:56,480 --> 00:10:57,710
do there is we'll make that a six, not a

280
00:10:57,710 --> 00:11:01,340
three - we'll talk about this shortly. Now on

281
00:11:01,340 --> 00:11:06,050
the next line, if parentheses chance is

282
00:11:06,050 --> 00:11:09,140
greater than three, and we're going to print an

283
00:11:09,140 --> 00:11:11,120
output, the fact that Dracula dodges;

284
00:11:11,120 --> 00:11:17,980
println Dracula dodges, return true.

285
00:11:17,980 --> 00:11:20,960
Otherwise, outside of the if code block,

286
00:11:20,960 --> 00:11:23,800
we're going to do a return false.

287
00:11:23,800 --> 00:11:26,120
Okay, so there's our dodges

288
00:11:26,120 --> 00:11:28,040
function. And what should have happened

289
00:11:28,040 --> 00:11:30,890
when you typed Random, Android Studio

290
00:11:30,890 --> 00:11:32,570
should have automatically imported the

291
00:11:32,570 --> 00:11:34,730
java.util package, and that's where

292
00:11:34,730 --> 00:11:36,770
the Random class is defined. If for some

293
00:11:36,770 --> 00:11:38,060
reason it hasn't, and you can see in my case

294
00:11:38,060 --> 00:11:40,370
it has - import java.util.* -

295
00:11:40,370 --> 00:11:42,200
but if we're getting an error there, just type

296
00:11:42,200 --> 00:11:44,780
that in as line one, and that will get that

297
00:11:44,780 --> 00:11:47,270
error to disappear. Alright, so

298
00:11:47,270 --> 00:11:49,940
this Random class has a nextInt method

299
00:11:49,940 --> 00:11:53,360
that is being used here on line 22, and

300
00:11:53,360 --> 00:11:55,760
that returns a number between 0 and 5. So

301
00:11:55,760 --> 00:11:58,280
it's like a six-sided dice, but labelled

302
00:11:58,280 --> 00:12:01,310
starting at zero rather than 1. Now we

303
00:12:01,310 --> 00:12:03,200
can specify the range by passing an int

304
00:12:03,200 --> 00:12:05,600
argument to nextInt. So if we wanted to

305
00:12:05,600 --> 00:12:08,000
emulate a 20-sided dice, for example, we'd

306
00:12:08,000 --> 00:12:10,580
pass 20 as the argument. Remember that

307
00:12:10,580 --> 00:12:13,100
computers count the 0 as a number, so

308
00:12:13,100 --> 00:12:15,230
we'd actually get a value from 0 to 19,

309
00:12:15,230 --> 00:12:17,570
in that scenario, returned instead. And

310
00:12:17,570 --> 00:12:19,690
here we're using 6 as the argument, and

311
00:12:19,690 --> 00:12:21,550
we're gonna get a value returned between

312
00:12:21,550 --> 00:12:24,850
zero to five. And consequently, with the if

313
00:12:24,850 --> 00:12:26,530
code, we're doing if chance is greater

314
00:12:26,530 --> 00:12:28,780
than 3, so the VampyreKing has got a one

315
00:12:28,780 --> 00:12:32,140
in three chance of dodging a blow. So going

316
00:12:32,140 --> 00:12:35,140
back to our Main class, we can now modify

317
00:12:35,140 --> 00:12:37,000
the while loop to check if Dracula

318
00:12:37,000 --> 00:12:39,940
dodges. And just below the while dracula

319
00:12:39,940 --> 00:12:42,370
dot lives greater than 0, so the

320
00:12:42,370 --> 00:12:44,140
first line before the dracula.run

321
00:12:44,140 --> 00:12:47,340
Away, we're gonna put if and parentheses,

322
00:12:47,340 --> 00:12:54,250
dracula.dodges. Then add a code block -

323
00:12:54,250 --> 00:12:56,620
we're going to type continue and leave

324
00:12:56,620 --> 00:12:58,270
it like that, and leave the other

325
00:12:58,270 --> 00:13:01,960
code as is. So now if Dracula dodges, the

326
00:13:01,960 --> 00:13:03,580
continue statement should get executed,

327
00:13:03,580 --> 00:13:05,830
and that should cause the rest of the

328
00:13:05,830 --> 00:13:08,110
loop's block to be skipped, and the code

329
00:13:08,110 --> 00:13:09,580
goes back to the while statement and

330
00:13:09,580 --> 00:13:11,710
tests the condition again. I'm going to

331
00:13:11,710 --> 00:13:13,390
run the program, and keep in mind that this is

332
00:13:13,390 --> 00:13:14,770
now a bit random, so we'll get different

333
00:13:14,770 --> 00:13:17,500
results each time we run the program. We

334
00:13:17,500 --> 00:13:19,420
should be able to see Dracula dodging

335
00:13:19,420 --> 00:13:21,370
some of the blows. You can see some

336
00:13:21,370 --> 00:13:24,930
dodges there, as well as taking damage.

337
00:13:24,930 --> 00:13:27,160
And you can see that you've got two dodges

338
00:13:27,160 --> 00:13:28,930
there but if I run this again, the results

339
00:13:28,930 --> 00:13:31,680
will be entirely random.

340
00:13:31,680 --> 00:13:33,760
And you can see we've got different output

341
00:13:33,760 --> 00:13:36,460
each time. So that's a quick overview of

342
00:13:36,460 --> 00:13:38,560
the while loop. They're great when you

343
00:13:38,560 --> 00:13:40,060
want to keep repeating a block of code,

344
00:13:40,060 --> 00:13:42,100
as long as the condition remains true.

345
00:13:42,100 --> 00:13:44,500
Once the condition becomes false, the

346
00:13:44,500 --> 00:13:46,960
loop terminates. Now we can also break

347
00:13:46,960 --> 00:13:48,550
out of the loop, as we saw, as well as

348
00:13:48,550 --> 00:13:50,890
skipping back to the beginning without

349
00:13:50,890 --> 00:13:52,270
executing the rest of the code in the

350
00:13:52,270 --> 00:13:54,640
loop, by using a using a continue

351
00:13:54,640 --> 00:13:56,830
statement. I've also shown you the

352
00:13:56,830 --> 00:13:58,900
Random class. Now you could modify the

353
00:13:58,900 --> 00:14:00,670
program, so that instead of giving,

354
00:14:00,670 --> 00:14:02,260
always giving Dracula 20 points of

355
00:14:02,260 --> 00:14:04,510
damage, you get a new random number each

356
00:14:04,510 --> 00:14:06,250
time you call the random.nextInt

357
00:14:06,250 --> 00:14:08,320
method. So you could emulate a 20-sided

358
00:14:08,320 --> 00:14:09,970
dice and make the program work a bit more

359
00:14:09,970 --> 00:14:12,580
like Dungeons & Dragons. Alright, so in

360
00:14:12,580 --> 00:14:13,600
the next video, we're gonna have a look

361
00:14:13,600 --> 00:14:18,510
at the for loop. See you in the next video.

