1
00:00:08,760 --> 00:00:10,368
So we've made it to the end of

2
00:00:10,368 --> 00:00:14,690
Module ten, about iteration, what some
people call looping.

3
00:00:14,690 --> 00:00:18,360
So, let's look at the module learning
objectives.

4
00:00:22,200 --> 00:00:26,460
So last time, we finished our discussion
about for and for each loops.

5
00:00:26,460 --> 00:00:29,115
The loops that we use when we know how
many times we're

6
00:00:29,115 --> 00:00:32,590
going to loop when we get to the loop as
the program executes.

7
00:00:32,590 --> 00:00:34,900
This time, we're going to look at while
loops.

8
00:00:34,900 --> 00:00:36,524
Which are loops that we use when we

9
00:00:36,524 --> 00:00:39,660
don't know how many times we're going to
iterate.

10
00:00:39,660 --> 00:00:42,348
So, just to see if you've paid attention
to what I

11
00:00:42,348 --> 00:00:46,260
said over the last 30 seconds, here's an
in-lecture quiz for you.

12
00:00:48,550 --> 00:00:53,730
So, I've started a program that we're
going to start looking at.

13
00:00:53,730 --> 00:00:57,058
Before I do, I want to talk about sort of

14
00:00:57,058 --> 00:01:01,654
a standard problem that we solve using
while loops.

15
00:01:01,654 --> 00:01:06,230
So, input validation means making sure
that whoever is using

16
00:01:06,230 --> 00:01:10,370
our software has entered valid data when
we ask for it.

17
00:01:10,370 --> 00:01:13,714
And the reason we need a while loop, here
rather than

18
00:01:13,714 --> 00:01:16,374
a for, or for each loop, or even just an
if

19
00:01:16,374 --> 00:01:20,890
statement is because we don't know how
stupid the user is.

20
00:01:20,890 --> 00:01:26,460
They could enter invalid inputs 10 times
before they finally enter a valid one.

21
00:01:26,460 --> 00:01:31,210
And so a while loop is a great choice for
doing input validation.

22
00:01:31,210 --> 00:01:36,190
So, I'll open up our project.
I've already added a little bit of code.

23
00:01:36,190 --> 00:01:39,339
So this is a console application, darned
no graphics or

24
00:01:39,339 --> 00:01:44,550
explosions, or anything like that.
So we have a menu here.

25
00:01:44,550 --> 00:01:48,670
And the user can enter J for Jump, C for
Crouch, or Q for Quit.

26
00:01:50,420 --> 00:01:53,720
We have the standard prompt and reading in
that we've done before.

27
00:01:53,720 --> 00:01:57,330
I will point out that the console read
line gives us a string.

28
00:01:58,710 --> 00:02:04,974
And I'm calling the ToUpper Method on that
string, so that we turn all the characters

29
00:02:04,974 --> 00:02:09,890
in the string to upper case.
And hopefully, that's only one character.

30
00:02:09,890 --> 00:02:14,280
because we're going to do a char.Parse on
it, and put it into this choice variable.

31
00:02:14,280 --> 00:02:17,980
So we know, when we get here, as long as
the user doesn't do

32
00:02:17,980 --> 00:02:23,336
something super stupid, we're going to end
up with an upper case single character.

33
00:02:23,336 --> 00:02:27,340
So I'm going to Ctrl+F5 to run this, just
to show that I can do that.

34
00:02:27,340 --> 00:02:29,980
But I can also enter, you know, D and it's
okay

35
00:02:29,980 --> 00:02:31,820
it gets to the end.

36
00:02:31,820 --> 00:02:33,885
So for this example, we're not actually

37
00:02:33,885 --> 00:02:36,380
going to do anything with their menu
choice.

38
00:02:36,380 --> 00:02:41,930
We're just going to make sure that they've
entered the valid one so that when we get

39
00:02:41,930 --> 00:02:44,555
to this point in the code where this
cursor

40
00:02:44,555 --> 00:02:47,720
is we know that we have a valid input.

41
00:02:47,720 --> 00:02:50,200
So how do we validate input?

42
00:02:50,200 --> 00:02:55,156
Well, what we need to do is we need to
make sure that, as long as the user

43
00:02:55,156 --> 00:02:57,750
has entered invalid input.

44
00:02:57,750 --> 00:03:00,975
That we can in fact give them an error
message, give

45
00:03:00,975 --> 00:03:04,610
them the menu again and get a new input
from them.

46
00:03:04,610 --> 00:03:07,550
And so, we're going to use a while loop
for that.

47
00:03:07,550 --> 00:03:14,849
And I put the structure in first.
Like that.

48
00:03:14,849 --> 00:03:20,645
So, what goes here is a Boolean expression
that when it evaluates to true,

49
00:03:20,645 --> 00:03:26,370
we want to go into the loop body and do
whatever happens in there.

50
00:03:26,370 --> 00:03:30,873
So, let's say, we know what the valid
choices are.

51
00:03:30,873 --> 00:03:34,288
The valid choices are capital J, C, or Q.

52
00:03:34,288 --> 00:03:40,129
So, the Boolean expression here needs to
evaluate to true when that,

53
00:03:40,129 --> 00:03:45,765
when the input is invalid.
Which means that

54
00:03:45,765 --> 00:03:50,935
choice is not equal to J and

55
00:03:50,935 --> 00:03:56,575
choice is not equal to C, and

56
00:03:56,575 --> 00:04:01,560
choice is not equal to Q.

57
00:04:01,560 --> 00:04:05,824
So, if it's the case, that they haven't
entered a J and they

58
00:04:05,824 --> 00:04:10,990
haven't entered a C, and they haven't
entered a Q, then we know they've

59
00:04:10,990 --> 00:04:15,710
given us invalid input.
And we need to get valid input from them.

60
00:04:15,710 --> 00:04:18,390
So, the first thing we'll do is print an
error message.

61
00:04:20,670 --> 00:04:24,129
And you can be as nice or as mean with
this error message as you want.

62
00:04:25,540 --> 00:04:31,430
I'll be reasonably nice and just say

63
00:04:31,430 --> 00:04:36,940
invalid input, must be J, C or Q.

64
00:04:38,210 --> 00:04:40,450
You can column name if you want to and so
on.

65
00:04:40,450 --> 00:04:42,880
We're going to add a couple of lines in

66
00:04:42,880 --> 00:04:45,796
the output to set the error message off
from

67
00:04:45,796 --> 00:04:49,320
the menu and from where they entered their
choice.

68
00:04:55,440 --> 00:05:00,840
And now I need to print the menu again and
get their choice again.

69
00:05:00,840 --> 00:05:04,410
And I'm going to copy and paste, even
though I hate doing that.

70
00:05:04,410 --> 00:05:08,067
Once we learn how to write our own
methods, we'll see that this

71
00:05:08,067 --> 00:05:12,750
whole copy paste thing doesn't really need
to be done anymore by us.

72
00:05:12,750 --> 00:05:15,690
And of course I have to not declare the
choice variable again.

73
00:05:16,950 --> 00:05:20,674
So now, what will happen is, I'm going to
print the menu and

74
00:05:20,674 --> 00:05:22,670
get their choice.

75
00:05:22,670 --> 00:05:24,350
And then I'm going to check to see if

76
00:05:24,350 --> 00:05:27,470
their choice is invalid with this Boolean
expression.

77
00:05:27,470 --> 00:05:32,000
If it is, I'm going to print the err,
error message.

78
00:05:32,000 --> 00:05:35,280
I'm going to give them the menu and ask
for their choice again.

79
00:05:35,280 --> 00:05:40,897
And then when I we, When I reach this
closing curly brace,

80
00:05:40,897 --> 00:05:46,326
I'm going to loop back to the top of the
while loop and I'm going to evaluate

81
00:05:46,326 --> 00:05:49,570
this Boolean expression again.

82
00:05:49,570 --> 00:05:51,880
And if it's true again, because the user
is

83
00:05:51,880 --> 00:05:54,870
an idiot, then I'm going to go into the
loop again.

84
00:05:54,870 --> 00:05:59,059
So I will keep looping through this while
loop until they've entered a

85
00:05:59,059 --> 00:06:03,810
valid input, at which point I'll get here
and I'll know it's valid.

86
00:06:03,810 --> 00:06:10,109
So let's Ctrl+F5.
I'll enter some invalid

87
00:06:10,109 --> 00:06:12,550
inputs, D, D, D.

88
00:06:12,550 --> 00:06:17,356
But then when I finally enter a valid one
J, it gets past the while

89
00:06:17,356 --> 00:06:22,680
loop, because this Boolean expression here
now evaluates to false.

90
00:06:23,680 --> 00:06:26,500
And so it gets past the while loop and I
get to the end of my program.

91
00:06:27,510 --> 00:06:35,460
So, before we continue, you should go do
an in-lecture quiz, that's, talks about or

92
00:06:35,460 --> 00:06:38,360
asks you about infinite loops.

93
00:06:39,610 --> 00:06:44,110
And now you should do another in-lecture
quiz that asks you

94
00:06:44,110 --> 00:06:49,190
about an acronym that can help us solve
the infinite loop problem.

95
00:06:50,860 --> 00:06:51,590
Great.

96
00:06:51,590 --> 00:06:54,550
Now, infinite loops are not really
infinite.

97
00:06:54,550 --> 00:06:54,880
Right?

98
00:06:54,880 --> 00:06:58,580
At some point the sun will explode, or our
computer will burn up.

99
00:06:58,580 --> 00:07:00,532
Or, or we'll get so angry that we'll

100
00:07:00,532 --> 00:07:03,200
you know, throw it against the wall or
something.

101
00:07:03,200 --> 00:07:05,479
But they're really called infinite loops
because

102
00:07:05,479 --> 00:07:07,690
they don't terminate when they really
should.

103
00:07:07,690 --> 00:07:10,430
You know, with sort of normal behavior.

104
00:07:10,430 --> 00:07:15,880
So, let's show how initialize, test, and
modify both,

105
00:07:15,880 --> 00:07:21,980
bold, all three matter when avoiding
infinite loops.

106
00:07:21,980 --> 00:07:26,110
It actually turns out that in this
particular case, the initializing

107
00:07:26,110 --> 00:07:30,840
doesn't lead to an infinite loop, it just
leads to nasty behavior.

108
00:07:30,840 --> 00:07:35,350
So, let's say that I have in fact declared
my choice variable and

109
00:07:35,350 --> 00:07:41,330
initialized it to some invalid choice at
the beginning of the main method.

110
00:07:41,330 --> 00:07:46,000
And then I don't actually initialize it by
getting input from the user.

111
00:07:46,000 --> 00:07:51,550
What happens when I, when I run, is that
it immediately says that I'm a moron.

112
00:07:51,550 --> 00:07:54,310
And gives me the message again, which is
really bad for users.

113
00:07:54,310 --> 00:07:54,580
Right?

114
00:07:54,580 --> 00:07:56,800
That's not the appropriate way to do it.

115
00:07:56,800 --> 00:08:00,400
That's kind of a poor example of how
initialize leads

116
00:08:00,400 --> 00:08:04,710
to an infinite loop, because in fact it
does not here.

117
00:08:04,710 --> 00:08:11,370
So just trust me that sometimes initialize
is the problem.

118
00:08:11,370 --> 00:08:13,821
Let's actually look at test and modify,
because those

119
00:08:13,821 --> 00:08:16,728
are way better examples here in this
particular example that

120
00:08:16,728 --> 00:08:20,730
we're working through.
So let's say we got this test wrong.

121
00:08:20,730 --> 00:08:24,210
Let's say we've gotten confused about and
versus or.

122
00:08:25,290 --> 00:08:28,530
And so here we've put or's instead of
ands.

123
00:08:28,530 --> 00:08:29,980
So we've messed up the test.

124
00:08:29,980 --> 00:08:32,500
The test is invalid for the loop.

125
00:08:32,500 --> 00:08:34,600
And this is going to lead us to an
infinite loop.

126
00:08:34,600 --> 00:08:38,950
So I'll Ctrl+F5, and I will enter an
invalid input.

127
00:08:38,950 --> 00:08:41,770
And I say, oh, darn, I should have

128
00:08:41,770 --> 00:08:46,180
read the menu and I enter j.
And wow.

129
00:08:46,180 --> 00:08:47,110
I'm looping again.

130
00:08:47,110 --> 00:08:49,611
So, like any good programmer, I'll do
exactly

131
00:08:49,611 --> 00:08:52,290
the same thing and expect a different
result.

132
00:08:52,290 --> 00:08:54,560
That's the sign of madness.

133
00:08:54,560 --> 00:08:56,720
Except, in the context of software, it's
not

134
00:08:56,720 --> 00:08:59,320
the sign of madness, because something is
different.

135
00:08:59,320 --> 00:09:03,100
The state of the computer, the memory,
something is in fact different.

136
00:09:03,100 --> 00:09:07,480
We're mad as programmers, but not because
we try the same thing multiple times.

137
00:09:07,480 --> 00:09:09,020
Okay.
So now I'm stuck.

138
00:09:09,020 --> 00:09:09,290
Right?

139
00:09:09,290 --> 00:09:12,750
Even though I'm entering valid inputs, I
can't get out.

140
00:09:12,750 --> 00:09:16,790
So I'll use the red X.
Here's what's happening.

141
00:09:16,790 --> 00:09:19,870
When I entered J, this part of the Boolean

142
00:09:19,870 --> 00:09:23,630
expression, J not equal to J, was in fact
false.

143
00:09:23,630 --> 00:09:24,386
[INAUDIBLE]

144
00:09:24,386 --> 00:09:29,450
good.
The problem is, then I got to this part.

145
00:09:29,450 --> 00:09:35,120
And J not equal to C is true.
And the way that or's work.

146
00:09:35,120 --> 00:09:35,530
Right?

147
00:09:35,530 --> 00:09:37,900
Is that any of them can be true to make

148
00:09:37,900 --> 00:09:42,070
the whole thing true, which pushes us into
the loop again.

149
00:09:42,070 --> 00:09:45,424
So that's how a test can, an in, an
incorrect

150
00:09:45,424 --> 00:09:49,552
test in our Boolean expression for the
while loop can lead

151
00:09:49,552 --> 00:09:51,880
to an infinite loop.

152
00:09:51,880 --> 00:09:56,630
Let's hit the M part of the acronym, so if
we forgot to modify.

153
00:09:56,630 --> 00:09:58,020
So I'll, I'll comma this out.

154
00:09:59,050 --> 00:10:03,768
If we forgot to modify the loop control
variable, let's watch what happens.

155
00:10:03,768 --> 00:10:08,350
I'll Ctrl+F5, and now I'll enter a bad
one, D.

156
00:10:08,350 --> 00:10:14,180
And oh dear, loop, loop, loop, loop, loop
forever until I reach quit.

157
00:10:14,180 --> 00:10:14,648
So what's

158
00:10:14,648 --> 00:10:18,050
happening there, I'm going to fix this
before I forget.

159
00:10:18,050 --> 00:10:21,445
What's happening there is, if I got into

160
00:10:21,445 --> 00:10:26,280
the loop, this Boolen expression evaluated
to true.

161
00:10:26,280 --> 00:10:30,900
That's the only way I got into the loop
body was because this is true.

162
00:10:30,900 --> 00:10:36,240
So if I don't change choice inside the
loop.

163
00:10:36,240 --> 00:10:39,724
When I get to the end of the loop here,
and I come back around and

164
00:10:39,724 --> 00:10:43,090
check it again, it's true again, because
nothing changed.

165
00:10:43,090 --> 00:10:45,060
I've never changed choice.

166
00:10:45,060 --> 00:10:48,102
So, I'm stuck in this infinite loop
because I

167
00:10:48,102 --> 00:10:52,140
didn't modify the variable that I'm using
in my test.

168
00:10:52,140 --> 00:10:56,230
Sometimes we have multiple variable in our
test, but the concept still applies.

169
00:10:56,230 --> 00:10:59,930
At least one of those variables has to get
changed inside the

170
00:10:59,930 --> 00:11:05,110
loop body, otherwise we're guaranteed to
end up with an infinite loop.

171
00:11:05,110 --> 00:11:06,908
Because, if we got into the loop body

172
00:11:06,908 --> 00:11:10,370
the first time, we're going to get into it
forever.

173
00:11:10,370 --> 00:11:11,100
Okay.

174
00:11:11,100 --> 00:11:14,850
So one last.
That's, that's it for the code for today.

175
00:11:14,850 --> 00:11:17,688
Those are while loops, and input
validation tends

176
00:11:17,688 --> 00:11:20,210
to be the most common place we use them.

177
00:11:20,210 --> 00:11:23,180
But there are other places we find to use
them as well.

178
00:11:23,180 --> 00:11:28,570
So go do one more in-lecture quiz about
actually testing while loops.

179
00:11:31,770 --> 00:11:32,310
Okay.

180
00:11:32,310 --> 00:11:36,622
So, when we test while loops, it makes
sense to make

181
00:11:36,622 --> 00:11:41,080
it, to run it so that the loop body
executes 0 times.

182
00:11:41,080 --> 00:11:42,200
And note, I did that.

183
00:11:43,246 --> 00:11:45,358
And may not have done that, but I meant

184
00:11:45,358 --> 00:11:48,590
to do that by entering a valid input right
away.

185
00:11:48,590 --> 00:11:53,078
So then the while, the Boolean expression
in the while loop evaluated to false

186
00:11:53,078 --> 00:11:57,360
when we got to it the first time so we
just skip the loop body.

187
00:11:57,360 --> 00:12:00,267
You also should execute it once, which we
saw numerous times

188
00:12:00,267 --> 00:12:03,750
where it enters D and the J and something
like that.

189
00:12:03,750 --> 00:12:05,860
And then you should execute it multiple
times.

190
00:12:05,860 --> 00:12:10,110
Try a few invalid input followed by a
valid one.

191
00:12:10,110 --> 00:12:16,182
The reason for this is that the sort of
most common errors that people find in the

192
00:12:16,182 --> 00:12:23,120
while loops that they write, occur because
they got the test not quite right.

193
00:12:23,120 --> 00:12:25,334
And so when you go for a zero run,

194
00:12:25,334 --> 00:12:28,990
basically, you end up going into the loop
instead.

195
00:12:28,990 --> 00:12:30,279
And so you modify your test.

196
00:12:31,800 --> 00:12:35,045
You might, trying it once, we'd certainly
catch the you forgot

197
00:12:35,045 --> 00:12:37,840
to modify it thing and you end up with an
infinite loop.

198
00:12:37,840 --> 00:12:40,765
So if you have a test case that runs that
while loop body

199
00:12:40,765 --> 00:12:45,350
loop that goes into the while loop or body
once, you'll catch that.

200
00:12:45,350 --> 00:12:47,750
And sometimes much more rarely.

201
00:12:47,750 --> 00:12:48,206
We have

202
00:12:48,206 --> 00:12:52,310
a few iterations of the loop and then the
loop breaks.

203
00:12:52,310 --> 00:12:55,690
But, that is more rare, certainly, than
the first two.

204
00:12:55,690 --> 00:12:58,665
So that's a way to actually test of, to do

205
00:12:58,665 --> 00:13:02,650
a good test, of code that has a while
loop.

206
00:13:02,650 --> 00:13:08,128
So to recap for today, we actually covered
how we can use a while loop to iterate

207
00:13:08,128 --> 00:13:13,274
an unknown number of times, because it's
based on some condition rather

208
00:13:13,274 --> 00:13:14,570
than a count.

209
00:13:14,570 --> 00:13:17,035
It's not how many things are in the

210
00:13:17,035 --> 00:13:20,660
collection, it's how times is the user a
moron.

211
00:13:22,000 --> 00:13:25,950
That's the end of module ten.
So we're all done with iteration.

212
00:13:25,950 --> 00:13:28,220
Again, what a lot of people call looping.

213
00:13:28,220 --> 00:13:32,252
We're all done with iteration, and you now
have all three control

214
00:13:32,252 --> 00:13:36,950
structures you need to solve all kinds of
problems in your game development.

215
00:13:37,990 --> 00:13:38,670
Next time,

216
00:13:38,670 --> 00:13:42,070
we're going to move on to the next module,
where we're going to

217
00:13:42,070 --> 00:13:46,140
start building, designing and building a
customed class of our own.