1
00:00:00,450 --> 00:00:02,160
OK, welcome back, everybody.

2
00:00:02,730 --> 00:00:09,150
So we are continuing on to have this discussion about while loops and just loops in general.

3
00:00:09,570 --> 00:00:15,930
So what we're going to be talking about in this lecture is something called a dual loop and something

4
00:00:15,930 --> 00:00:17,310
called an accumulator variable.

5
00:00:17,310 --> 00:00:18,570
Those are the two main topics.

6
00:00:18,570 --> 00:00:20,390
I'm not going to go into a slide show today.

7
00:00:20,400 --> 00:00:24,000
We're just going to hang out in the ED and kind of mess around with some stuff.

8
00:00:24,330 --> 00:00:31,320
But before I get into those two new topics, I am going to kind of go through the code that we wrote

9
00:00:31,320 --> 00:00:35,280
in the last lecture and kind of clean it up a little bit.

10
00:00:35,280 --> 00:00:39,060
So we're going to talk about some things that may be redundant.

11
00:00:39,360 --> 00:00:46,160
Some ways for us to minimize the amount of code that we have and talk about kind of just coming up with

12
00:00:46,170 --> 00:00:52,410
a solution and throwing everything you know at something and solving the problem and afterwards going

13
00:00:52,410 --> 00:00:54,690
back and kind of like making stuff look better.

14
00:00:54,690 --> 00:00:59,570
So that's what I'm going to do right now because our program actually ran fine.

15
00:00:59,580 --> 00:01:02,820
It did what we wanted it to, but it kind of has some excess stuff in here.

16
00:01:03,030 --> 00:01:05,130
And so we're going to talk about how to get rid of that excess.

17
00:01:06,210 --> 00:01:06,510
All right.

18
00:01:06,510 --> 00:01:07,710
So let's go ahead and get started.

19
00:01:08,460 --> 00:01:14,250
So one thing that I am going to point out right off the bat is that the other day I went ahead and in

20
00:01:14,250 --> 00:01:18,930
that last lecture, I wrapped this whole while live in this.

21
00:01:18,930 --> 00:01:24,810
If so, this while loop is now nested in the scope of my.

22
00:01:24,820 --> 00:01:31,320
If so, this indentation layer and these brackets primarily in C++, it's about whatever is inside the

23
00:01:31,320 --> 00:01:34,650
brackets would be considered to be in the scope of those.

24
00:01:35,280 --> 00:01:43,020
So this highlighted stuff is in the scope of the if it's inside of the if you may have been wondering,

25
00:01:43,020 --> 00:01:48,300
well, why did he put this input not equal to password?

26
00:01:48,300 --> 00:01:51,180
You notice that when highlighting this, it's highlighting it right here.

27
00:01:51,190 --> 00:01:56,010
So we already have this same condition being checked in a while.

28
00:01:56,010 --> 00:02:02,040
Yet I still wrap this issue around here, and that's a good observation because it's completely redundant.

29
00:02:02,280 --> 00:02:08,940
One thing I wanted to illustrate in the past couple of lectures was just about, you know, using the

30
00:02:08,940 --> 00:02:10,890
stuff that we've learned so far.

31
00:02:11,070 --> 00:02:16,440
And the big thing was being you knowing that you can actually put things inside of other things, the

32
00:02:16,440 --> 00:02:22,080
fact that you can put a while and then if you can put another if in an if you can put a F and an F and

33
00:02:22,080 --> 00:02:27,930
an F and an f over and over and over again, as many layers as you like, you can put all these things

34
00:02:27,930 --> 00:02:29,820
inside of each other over and over again.

35
00:02:30,660 --> 00:02:31,920
So that is possible.

36
00:02:32,640 --> 00:02:33,810
And I wanted to point that out.

37
00:02:34,140 --> 00:02:37,860
But as you notice when I highlight this, we're checking the same condition.

38
00:02:38,250 --> 00:02:41,450
This condition is already here.

39
00:02:41,460 --> 00:02:47,730
We don't need to check it once and then immediately go to this line and then check it again just to

40
00:02:47,730 --> 00:02:49,510
be able to run this code.

41
00:02:49,530 --> 00:02:51,660
We could just only check it this one time.

42
00:02:52,170 --> 00:02:55,320
And then if you know, if this is true and this is true.

43
00:02:56,300 --> 00:02:57,470
Doing condition on this coat.

44
00:02:58,590 --> 00:03:01,740
Right, because if the user enters, you know, cue to quit.

45
00:03:03,430 --> 00:03:11,490
Like or their you know, they enter the right password, so it's not not equal to then we would just

46
00:03:11,500 --> 00:03:14,040
bail anyway, so we don't need a double check for this, is what I'm saying.

47
00:03:14,050 --> 00:03:16,090
So let's go ahead and get rid of that right off the bat, right?

48
00:03:17,910 --> 00:03:20,010
So we're going to clean this stuff up, you know that.

49
00:03:22,510 --> 00:03:26,060
It's good to come up with some sort of naive brute force solution.

50
00:03:26,200 --> 00:03:30,080
If you're just trying to solve a problem, you just want to get it done right.

51
00:03:30,100 --> 00:03:36,070
Just like do whatever needs to be done to solve the problem, but then afterwards, you can go through

52
00:03:36,070 --> 00:03:41,830
and make it more efficient, and this is something that we are going to focus heavily on in the next

53
00:03:42,100 --> 00:03:46,810
section of the course, the second part of the course, which is on algorithms and stuff like that.

54
00:03:47,760 --> 00:03:53,250
So we're not only going to make our code more efficient in terms of how it looks and how many lines

55
00:03:53,250 --> 00:03:58,540
there are, but we're actually going to go over the efficiency of how fast our programs run.

56
00:03:58,560 --> 00:04:00,420
So that is something we'll get into later.

57
00:04:00,900 --> 00:04:03,030
Right now, we're just trying to clean the code up.

58
00:04:03,390 --> 00:04:05,410
We don't need redundant stuff.

59
00:04:06,720 --> 00:04:07,950
So we got rid of that.

60
00:04:08,720 --> 00:04:09,960
Um, let's see.

61
00:04:09,960 --> 00:04:11,850
There's still some other things we can get rid of.

62
00:04:12,210 --> 00:04:12,780
One of them.

63
00:04:12,900 --> 00:04:14,880
I mean, this is kind of annoying, right?

64
00:04:15,900 --> 00:04:21,180
We had to add this stuff in here because what we had before was just this.

65
00:04:22,680 --> 00:04:23,550
We had that.

66
00:04:24,740 --> 00:04:29,270
And we didn't have that, you know, we didn't have this stuff down here, we had to add it because

67
00:04:29,270 --> 00:04:36,200
the problem was this when the user pressed Q. Of course, it stopped doing the while loop, and it just

68
00:04:36,200 --> 00:04:38,060
continued on past the while loop.

69
00:04:38,060 --> 00:04:41,900
And then it just said, you guessed it, but we didn't want to say you guessed it right.

70
00:04:42,230 --> 00:04:44,780
Because if they press Q, they didn't actually guess it.

71
00:04:45,590 --> 00:04:47,770
So that's why we ended up wrapping it in this.

72
00:04:47,780 --> 00:04:51,680
If and then we put this elsewhere, if they didn't guess it, then we just said, thank you.

73
00:04:51,680 --> 00:04:52,250
Come again.

74
00:04:54,280 --> 00:04:58,810
It would be a lot nicer if we could just stop right away if they pressed Q.

75
00:04:58,810 --> 00:05:03,700
I mean, if the user presses Q, it says Q to quit and honestly, the program should just stop.

76
00:05:04,390 --> 00:05:05,770
We don't need to do anything else.

77
00:05:06,040 --> 00:05:11,160
We should just straight up stop it right away so we can actually do that.

78
00:05:11,190 --> 00:05:16,540
So what I'm going to do is put an f here, so I'm going to check to see if the user entered Q.

79
00:05:17,080 --> 00:05:22,870
So I'm going to say and I'm actually going to put that after this get line because we want to make sure

80
00:05:22,870 --> 00:05:28,370
that we actually get that line in and see what it is.

81
00:05:28,390 --> 00:05:38,440
So right here, I will say if input is equal to Q, then we can use this special function.

82
00:05:38,440 --> 00:05:41,040
Just like that line, I'm introducing another function.

83
00:05:41,050 --> 00:05:46,570
Yep, I know I've introduced a lot and we still haven't gone over how to make our own functions, but

84
00:05:47,260 --> 00:05:52,600
we need to know about the built in functions to do a few things before we get into making our own.

85
00:05:53,290 --> 00:05:57,730
So this function is called exit, and it does exactly what you would think it does.

86
00:05:57,730 --> 00:05:59,590
It exits the program immediately.

87
00:05:59,590 --> 00:06:02,230
Once it hits this, it stops.

88
00:06:04,180 --> 00:06:05,920
So pretty cool, right?

89
00:06:06,670 --> 00:06:11,260
So what we can do is similar to the return zero.

90
00:06:11,470 --> 00:06:13,960
We can put some sort of number in here.

91
00:06:14,230 --> 00:06:17,560
You remember how I was talking about returns zero from Maine.

92
00:06:17,920 --> 00:06:23,680
When the program stops, it kind of returns control back to the operating system and it gives this zero

93
00:06:23,680 --> 00:06:25,540
as like a successful exit code.

94
00:06:25,870 --> 00:06:28,690
We can put an exit code in this exit function.

95
00:06:29,110 --> 00:06:33,520
Sometimes if something goes wrong, you put like a negative one or some other number in here.

96
00:06:33,520 --> 00:06:38,650
It's really up to you, and I'm going to go ahead and put this zero in here and we finish it off with

97
00:06:38,650 --> 00:06:39,310
the semicolon.

98
00:06:39,610 --> 00:06:41,130
Let's go ahead and check this out now.

99
00:06:41,610 --> 00:06:46,960
As I commented this stuff out here, we should not see a, you guessed it, print if we enter a queue.

100
00:06:48,400 --> 00:06:56,170
So I'm going to go ahead and compile this and clear this, pull it up and we'll go ahead and run it.

101
00:06:57,220 --> 00:07:01,210
So I'm going to enter past just the wrong thing right away.

102
00:07:01,210 --> 00:07:07,540
So it goes into the wild loop, at least says try again, so try again or Typekit to quit now and then.

103
00:07:07,540 --> 00:07:14,740
What I'm going to do is I'm actually going to press Q and you notice it just stops.

104
00:07:14,980 --> 00:07:16,660
It does not do anything pass.

105
00:07:16,660 --> 00:07:19,720
Then, you know, I could put anything after this.

106
00:07:19,720 --> 00:07:25,390
I can just even say, like, see our test, you know, something like that.

107
00:07:26,050 --> 00:07:31,720
I save this compile run again, cast queue.

108
00:07:31,960 --> 00:07:33,970
It doesn't even print this thing.

109
00:07:34,060 --> 00:07:40,060
So that's that's one way that you know, that it for sure is not reaching anywhere below this exit.

110
00:07:42,370 --> 00:07:44,960
So pretty cool, right, except there's another way to do it.

111
00:07:44,980 --> 00:07:46,870
We don't have to put an exit.

112
00:07:47,170 --> 00:07:55,120
We can actually just use this because Return Zero returns to the control of the program back to the

113
00:07:55,120 --> 00:07:55,480
US.

114
00:07:55,480 --> 00:07:57,320
Basically, it's done at that point, right?

115
00:07:57,340 --> 00:08:01,840
The main function is the entry point into the program, and it's also kind of like the exit point here

116
00:08:01,840 --> 00:08:02,440
at the end.

117
00:08:02,470 --> 00:08:08,290
Everything that matters happens in between here and here.

118
00:08:08,290 --> 00:08:08,720
Like this?

119
00:08:08,870 --> 00:08:12,820
This zone is where the other stuff that we tell it to do happens.

120
00:08:14,580 --> 00:08:20,760
So what I can do is I can just kind of prematurely exit as a press cue, I don't have to do anything

121
00:08:20,760 --> 00:08:27,840
else, and I can do that by just returning the control from the function immediately here so I can just

122
00:08:27,840 --> 00:08:30,420
put a return zero here, and it would do the same thing.

123
00:08:31,020 --> 00:08:36,360
So let me go ahead and save this and then compile it and run it.

124
00:08:36,810 --> 00:08:40,740
And so I'll say pass, and then I say Q, and you notice it's the same thing.

125
00:08:40,740 --> 00:08:42,030
It just stops right away.

126
00:08:44,170 --> 00:08:49,720
So pretty cool right now we can actually get rid of a lot of this stuff.

127
00:08:50,110 --> 00:08:52,390
It looks a lot cleaner if I just delete this.

128
00:08:53,820 --> 00:08:58,450
You know, it's our code is quite a bit more compact now.

129
00:09:00,250 --> 00:09:03,910
So we can actually even get better than this.

130
00:09:04,240 --> 00:09:09,130
And that is using this new concept called a dual loop.

131
00:09:09,130 --> 00:09:21,730
So in this while loop, you notice that we actually had to do this first to be able to check the condition

132
00:09:21,730 --> 00:09:22,180
here.

133
00:09:24,470 --> 00:09:32,030
So basically before anything inside the wire loop like this stuff, all of this stuff happens.

134
00:09:33,250 --> 00:09:35,230
We're having to check a condition.

135
00:09:35,830 --> 00:09:41,940
And so that's why we have to the the thing is that the condition is based on the input, right?

136
00:09:42,730 --> 00:09:47,380
It's just declared here, but we don't have any input until we get input here.

137
00:09:47,920 --> 00:09:53,980
So that's why we actually have to ask the user to type something and then get it before we can actually

138
00:09:53,980 --> 00:09:55,810
check the condition for the while it.

139
00:09:57,520 --> 00:10:02,830
So it would be cool if we could just not have a try again, like if we could just have one line that

140
00:10:02,830 --> 00:10:07,360
just said like enter the password of Typekit to quit, and it just kept saying that over and over again

141
00:10:07,360 --> 00:10:08,650
for you to enter your password.

142
00:10:09,860 --> 00:10:14,360
We could make it even more concise, and we wouldn't have to have a get line here and get lying here.

143
00:10:16,600 --> 00:10:22,930
So there is a way we can do this, and it is something called a dual loop, and basically it just does

144
00:10:23,740 --> 00:10:33,100
kind of the same thing as the wow loop, except it lets you do the stuff inside the while loop and then

145
00:10:33,100 --> 00:10:38,950
checks a condition below that to see if it's going to continue doing it rather than immediately checking

146
00:10:38,950 --> 00:10:39,780
the condition.

147
00:10:41,210 --> 00:10:46,850
Which this is contingent upon, you know, that while it won't be entered unless this is satisfied,

148
00:10:46,850 --> 00:10:52,790
but what if we wanted to like ask the user to enter our password and then get a line?

149
00:10:53,990 --> 00:11:01,100
And then we wanted to say, OK, if they entered the wrong password, I want to go back above this and

150
00:11:01,100 --> 00:11:05,090
just start and keep repeating it as long as they keep entering the wrong password.

151
00:11:05,510 --> 00:11:06,980
This is what the dude does.

152
00:11:07,490 --> 00:11:09,100
So we can actually change this.

153
00:11:09,110 --> 00:11:14,300
I'm going to actually just comment this out for now.

154
00:11:14,840 --> 00:11:16,250
Another block comment.

155
00:11:16,550 --> 00:11:21,120
Kind of like you saw down there before when I commented out these, if that's something I showed you

156
00:11:21,120 --> 00:11:24,950
already, remember these slash star in the star slash on the other end of it.

157
00:11:26,390 --> 00:11:29,170
So let's go ahead and do this do well.

158
00:11:29,170 --> 00:11:39,020
So I'm going to say do like this, and then I just put a bracket and then I press this enter key here.

159
00:11:39,620 --> 00:11:42,620
And then what we're going to do is this stuff, right?

160
00:11:44,560 --> 00:11:46,090
So we actually want.

161
00:11:48,590 --> 00:11:50,720
Kind of something like.

162
00:11:52,030 --> 00:11:53,200
I'm actually just going to do this.

163
00:11:53,290 --> 00:11:57,460
Let's see if they enter the password

164
00:12:00,070 --> 00:12:01,420
and then we'll get line.

165
00:12:02,980 --> 00:12:05,650
And then we probably also want this right here.

166
00:12:10,860 --> 00:12:16,260
So if it's cue, we can just, like, return immediately and then what we do with the wow condition

167
00:12:16,260 --> 00:12:21,930
is it comes right here after this close bracket, so we say do and then while.

168
00:12:23,390 --> 00:12:25,070
And then it's going to be this stuff right here.

169
00:12:27,710 --> 00:12:30,710
So I'm going to copy this and paste it in here.

170
00:12:30,830 --> 00:12:32,090
And that's our condition, right?

171
00:12:39,580 --> 00:12:42,190
OK, so pretty cool, pretty cool.

172
00:12:43,360 --> 00:12:47,360
So what we do is we actually can run this right away.

173
00:12:48,570 --> 00:12:52,380
To get at the initial time, if it's cue, we immediately bail.

174
00:12:54,220 --> 00:12:58,030
Otherwise, if it's not cute, you know, we're going to go down here and we're going to end up checking

175
00:12:58,030 --> 00:13:04,720
this condition, well, you know, we'll be like, Yeah, is it not equal to password or is it equal

176
00:13:04,720 --> 00:13:05,340
to password?

177
00:13:05,350 --> 00:13:08,380
And we, of course, don't want it to queue to be queue.

178
00:13:08,830 --> 00:13:13,360
This actually would not even be 100 percent necessary at this point because it would just.

179
00:13:16,220 --> 00:13:21,620
Already break out of it so we can even reduce this and just say, well, if it's not equal to password

180
00:13:21,620 --> 00:13:27,500
because it's already returning right here immediately, and then we just have the, you guessed it,

181
00:13:27,500 --> 00:13:28,010
down here.

182
00:13:29,000 --> 00:13:30,410
So let's go ahead and compile this.

183
00:13:37,630 --> 00:13:43,690
All right, so all looks pretty good, we go ahead and run it, so says into the password or Typekit

184
00:13:43,720 --> 00:13:52,600
quit, let's say S8 Pass says enter the password Typekit a quick past W password and it says, you guessed

185
00:13:52,600 --> 00:13:53,980
it, let's go ahead and run again.

186
00:13:54,430 --> 00:13:57,190
So I'm going to first queue it just immediately quit.

187
00:13:57,640 --> 00:13:58,810
I'm going to run it again.

188
00:13:59,440 --> 00:14:06,010
And we actually it know I want to steer clear and then I'm going to run it again here.

189
00:14:06,460 --> 00:14:08,710
So cue immediately made it quit.

190
00:14:11,550 --> 00:14:12,480
So that stopped it.

191
00:14:13,410 --> 00:14:17,850
And then if we just do password right away, it just says, you guessed it, so, you know, it has

192
00:14:17,850 --> 00:14:25,420
the same functionality and it's a lot more it's a simplified quite a bit more now, so pretty nice.

193
00:14:25,440 --> 00:14:26,970
Now we have something pretty succinct.

194
00:14:26,970 --> 00:14:29,340
So let me like delete this stuff here.

195
00:14:32,520 --> 00:14:33,720
I'm going to delete this.

196
00:14:37,190 --> 00:14:40,130
I mean, look how small our program is now.

197
00:14:41,670 --> 00:14:43,200
Compared to how it was before.

198
00:14:44,640 --> 00:14:46,800
Like this is pretty, pretty compact.

199
00:14:47,820 --> 00:14:49,680
So the dual is something pretty cool.

200
00:14:49,710 --> 00:14:52,050
The main thing I wanted to point out about it.

201
00:14:53,060 --> 00:14:56,030
Is the fact that it lets you kind of.

202
00:14:57,250 --> 00:15:02,050
It kind of flips it around if it flips like the the control flow style.

203
00:15:03,220 --> 00:15:07,810
So it basically lets you do something before the condition gets checked.

204
00:15:08,530 --> 00:15:14,050
So it's like, OK, go ahead and do this, and it's like telling the program to do something, it's

205
00:15:14,050 --> 00:15:15,520
like, Okay, do this.

206
00:15:15,890 --> 00:15:17,980
It's like, All right, I'm going to do this.

207
00:15:19,250 --> 00:15:22,950
And then it gets to the bottom and it says, like, OK, we'll keep doing it.

208
00:15:22,970 --> 00:15:26,540
This is kind of instead of while it's kind of saying like, keep doing.

209
00:15:27,790 --> 00:15:32,290
Keep doing as long as this is true.

210
00:15:33,760 --> 00:15:38,800
Right, as long as input is not able to password, then keep doing the stuff that's inside the brackets

211
00:15:38,800 --> 00:15:39,580
of the do.

212
00:15:41,740 --> 00:15:44,770
So that's, you know, something that's pretty cool.

213
00:15:47,260 --> 00:15:53,350
Not necessarily like always going to be something that you're going to use like a whole lot.

214
00:15:54,100 --> 00:15:56,950
Some people love do loops and they're just using them constantly.

215
00:15:57,220 --> 00:15:59,080
I don't think it's necessary all the time.

216
00:15:59,090 --> 00:16:01,150
You can definitely use a while loop.

217
00:16:02,720 --> 00:16:09,770
Quite often, and of course, you notice it wasn't necessary to solve this particular problem, like

218
00:16:10,340 --> 00:16:15,290
we were able to solve it with the while, this just kind of helped us light compact our code a little

219
00:16:15,290 --> 00:16:15,590
bit.

220
00:16:16,250 --> 00:16:21,740
There will be some situations in which the duo will make it a lot, a lot easier and will make a lot

221
00:16:21,740 --> 00:16:22,430
more sense.

222
00:16:23,270 --> 00:16:28,460
So you might encounter situations like that where you definitely want to be able to at least execute

223
00:16:28,790 --> 00:16:32,030
some chunk of code once before having to check the condition.

224
00:16:32,990 --> 00:16:38,270
This could depend on some sort of variable here that gets initialized to something and you want something

225
00:16:38,270 --> 00:16:41,750
to happen and maybe the variable gets changed in here.

226
00:16:42,710 --> 00:16:47,510
And so you want it to at least happen once first, rather than relying on a while where it might not

227
00:16:47,510 --> 00:16:52,510
execute the while loop at all because of the condition of the variable or something like that.

228
00:16:52,520 --> 00:16:57,440
Whatever was stored in the variable was contingent on like this thing passing, so it wouldn't go into

229
00:16:57,440 --> 00:17:02,060
the well, that's like kind of maybe an example where you used to do well, you'd need to use it.

230
00:17:03,950 --> 00:17:06,440
So that's pretty cool and all.

231
00:17:06,770 --> 00:17:12,680
So now I think that we're going to move on to something called an accumulator variable, so this is

232
00:17:12,680 --> 00:17:19,130
something when we modify our variable to kind of make the loop based on that.

233
00:17:19,130 --> 00:17:22,190
So that's kind of what we're doing already, though, right?

234
00:17:22,430 --> 00:17:24,980
We are modifying this input because.

235
00:17:26,360 --> 00:17:33,290
We asked the person for the input kind of over and over, and so the variable called input changes like,

236
00:17:33,860 --> 00:17:39,230
you know, it was it was passed and then it was like passed W.

237
00:17:39,230 --> 00:17:45,440
And then finally I entered my password and it actually stopped and went down here and said, you guessed

238
00:17:45,440 --> 00:17:45,560
it.

239
00:17:46,810 --> 00:17:51,670
So that means that this variable had stored each one of those at some point in time, which is overwriting

240
00:17:51,670 --> 00:17:51,770
it.

241
00:17:51,790 --> 00:17:53,830
This is something I mentioned in the last lecture to.

242
00:17:56,300 --> 00:18:00,780
But we can do some other things, too, like let's take a sample.

243
00:18:02,150 --> 00:18:03,590
Let's take a sample problem.

244
00:18:04,010 --> 00:18:10,040
So I'll leave this do well here, and let's change this problem up a bit.

245
00:18:11,870 --> 00:18:19,420
In fact, what I'm going to do is I'm going to copy this over to another file.

246
00:18:20,090 --> 00:18:21,650
So I'll actually say

247
00:18:24,050 --> 00:18:24,900
C.P..

248
00:18:25,520 --> 00:18:26,050
Wow.

249
00:18:26,570 --> 00:18:30,470
Stratigraphy, maybe I hopefully I can do this in PowerShell.

250
00:18:32,360 --> 00:18:34,190
This is something I would normally do in Bash.

251
00:18:35,630 --> 00:18:38,490
So and I'm going to call this a see.

252
00:18:38,520 --> 00:18:40,610
Wow, let's just call it wow.

253
00:18:40,610 --> 00:18:42,950
Looks to CBP.

254
00:18:45,780 --> 00:18:46,530
OK, cool.

255
00:18:46,740 --> 00:18:49,150
So it seems like maybe I can open this now.

256
00:18:49,170 --> 00:18:50,280
I'll go ahead and.

257
00:18:51,780 --> 00:18:53,310
Open this file.

258
00:18:56,000 --> 00:18:59,180
So I'm going to make this other file is basically a copy of it.

259
00:19:02,110 --> 00:19:05,500
And then I'm going to modify it a little bit.

260
00:19:11,550 --> 00:19:18,150
So what we're going to do is instead of having the user enter a string.

261
00:19:20,300 --> 00:19:25,340
Let's go ahead and have the user actually enter a.

262
00:19:28,570 --> 00:19:39,940
No, I actually keep entering numbers for that matter, so I'm actually going to say and no like this.

263
00:19:41,900 --> 00:19:47,390
And what are we going to want to do is like here, I'll put the problem statement here, so we'll say

264
00:19:47,720 --> 00:20:01,550
make a program that takes input from the user as an integer repeatedly.

265
00:20:04,500 --> 00:20:17,310
And stops when a specific sum has been reached.

266
00:20:20,960 --> 00:20:31,280
So let's say we have some thing here this like and some equals 60 or something like that.

267
00:20:33,130 --> 00:20:38,680
And then basically, we're just going to have the user enter numbers.

268
00:20:40,110 --> 00:20:42,540
Over and over again until.

269
00:20:43,590 --> 00:20:45,060
We reach 60.

270
00:20:46,550 --> 00:20:48,770
And then we want the program to stop.

271
00:20:50,010 --> 00:20:54,150
So pretty simple program not, you know, doesn't really do a whole lot of interesting stuff, but how

272
00:20:54,150 --> 00:20:55,320
would we solve this?

273
00:20:55,860 --> 00:21:02,460
So right here, I made some variable called some variable that hold 60.

274
00:21:03,090 --> 00:21:06,330
I've also declared a number variable that has no value yet.

275
00:21:08,590 --> 00:21:12,700
And so now we're going to be reading into this number, so what do I do instead of?

276
00:21:14,660 --> 00:21:17,920
Get lean, I'm just going to see an end to you.

277
00:21:18,890 --> 00:21:19,350
No.

278
00:21:22,500 --> 00:21:32,610
And so we should loop as long as the something, you know, the Sun or whatever, you know, the users

279
00:21:32,610 --> 00:21:35,070
entering is, is and hasn't reached 60 yet.

280
00:21:35,370 --> 00:21:36,960
So how would we do that, though?

281
00:21:37,260 --> 00:21:41,040
So why don't you go ahead and pause?

282
00:21:42,580 --> 00:21:45,310
So I'm going to go ahead and get rid of this.

283
00:21:51,430 --> 00:21:55,420
Let's save this something, we get rid of that stuff there.

284
00:21:55,660 --> 00:22:00,820
Why don't you pause the video and see if you can actually figure this out?

285
00:22:04,110 --> 00:22:08,160
So I'll go ahead and try and solve it now.

286
00:22:08,190 --> 00:22:09,710
If you figured it out, that's great.

287
00:22:09,720 --> 00:22:12,390
Otherwise I will go ahead and kind of walk through it.

288
00:22:13,380 --> 00:22:14,610
So what would we do?

289
00:22:15,150 --> 00:22:16,230
What do we do for this?

290
00:22:16,380 --> 00:22:23,440
Well, we have the Sun set to 60, so we don't really want to change that variable.

291
00:22:24,330 --> 00:22:28,680
But what we can do is actually use this concept of an accumulator variable.

292
00:22:28,680 --> 00:22:33,000
So accumulator variable is kind of like they take that word as it is.

293
00:22:34,160 --> 00:22:37,970
It accumulates stuff, you know, you're kind of adding more and more to it.

294
00:22:38,180 --> 00:22:41,600
It's kind of like a snowball rolling down a hill and getting bigger.

295
00:22:41,600 --> 00:22:43,300
It's accumulating more mass.

296
00:22:44,180 --> 00:22:49,640
So I'm going to do something like that in the form of a variable that actually holds the currents.

297
00:22:49,940 --> 00:22:51,410
And I'm going to call that total.

298
00:22:52,520 --> 00:22:57,070
And what I'm going to do is I'm going to set that equal to zero to start off with.

299
00:22:57,080 --> 00:22:59,210
So Total is going to have the value of zero.

300
00:23:00,150 --> 00:23:07,110
And then my strategy is going to be make this wildly based on this variable.

301
00:23:09,120 --> 00:23:12,120
And we're just going to keep adding the user's numbers to it.

302
00:23:12,120 --> 00:23:25,050
And what I will say in the wild part is I will say if I will say while some so not some so well, total

303
00:23:25,650 --> 00:23:29,010
is less than some.

304
00:23:31,140 --> 00:23:36,720
So I'm going to keep going as long as the total is less than the sum that we've defined.

305
00:23:36,720 --> 00:23:39,450
So the users will enter these numbers.

306
00:23:40,740 --> 00:23:46,650
And then what I'm going to do is after they enter it in here, when we get the scene in and we read

307
00:23:46,650 --> 00:23:54,570
it into, no, I'm basically going to say total plus equals.

308
00:23:54,570 --> 00:23:58,410
So we're using this plus equal operator to add to total.

309
00:23:58,410 --> 00:24:05,740
It's the same thing as saying total equals total plus something MSA Total Plus equals no.

310
00:24:07,590 --> 00:24:13,290
So we are adding this number to total, and so we just keep adding to it.

311
00:24:13,300 --> 00:24:16,230
It's basically like we're summing up all the numbers.

312
00:24:17,740 --> 00:24:22,930
That they're entering, and we're going to keep doing that as long as it doesn't, you know?

313
00:24:23,860 --> 00:24:28,090
Get to the same amount as or exceed some.

314
00:24:30,970 --> 00:24:32,110
So pretty cool.

315
00:24:33,220 --> 00:24:39,670
And I'm not going to say into the password or Typekit to quit, I'm just going to say, let's just say

316
00:24:40,510 --> 00:24:45,820
insert a number into a number of Typekit to quit.

317
00:24:48,550 --> 00:24:52,700
So let's see what this will do, so we have our son, which is 60, right?

318
00:24:52,720 --> 00:24:58,720
Then we have our number, which is just declared and not initialized, and we have this total initialized

319
00:24:58,720 --> 00:24:59,200
to zero.

320
00:24:59,710 --> 00:25:03,040
We go in here, we say, Hey, enter a number Typekit you to create.

321
00:25:03,040 --> 00:25:11,770
Let's say the user enters a number like 10 or something and reads in to the number and total is now

322
00:25:12,580 --> 00:25:16,030
zero plus equals 10.

323
00:25:16,030 --> 00:25:17,470
So that's saying zero.

324
00:25:18,810 --> 00:25:23,000
Plus, ten total equals total plus 10.

325
00:25:23,190 --> 00:25:25,500
Now it's gone from zero to 10.

326
00:25:26,250 --> 00:25:28,290
They have an intern Q So we don't return.

327
00:25:28,770 --> 00:25:30,110
It's total less than some.

328
00:25:30,120 --> 00:25:30,660
Yes, it is.

329
00:25:30,660 --> 00:25:33,810
So we go back and we ask again.

330
00:25:35,600 --> 00:25:41,300
So let's go ahead and check this out, so I'll go ahead and save it, and then I'm going to just do

331
00:25:41,300 --> 00:25:47,660
this while loops to that speed so that we are able to see.

332
00:25:50,480 --> 00:25:58,040
So I put in extra parenthesis on here, it looks like it's an expected prime primary expression before

333
00:25:58,040 --> 00:25:58,730
that token.

334
00:25:59,120 --> 00:26:01,700
That just means that I had an extra parentheses.

335
00:26:01,700 --> 00:26:06,440
You notice it's pointing to this right here should not have had that there, so I'm going to save it

336
00:26:06,440 --> 00:26:07,460
and compile it again.

337
00:26:09,320 --> 00:26:10,480
We have another problem.

338
00:26:11,020 --> 00:26:15,570
Input is not declared, so I need to change this to no.

339
00:26:16,090 --> 00:26:21,070
And we're actually reading in an integer, so I shouldn't really I mean, the chars and integers are

340
00:26:21,070 --> 00:26:25,120
fine, but I probably shouldn't really have that.

341
00:26:25,960 --> 00:26:30,610
So what I'm going to do is, let's just say.

342
00:26:33,470 --> 00:26:34,310
Or

343
00:26:38,060 --> 00:26:39,650
Typekit zero to quit.

344
00:26:39,920 --> 00:26:40,860
How about we do that?

345
00:26:41,900 --> 00:26:46,070
So we're not going to like add zero to it, so we'll see if no equals zero, then it will quit.

346
00:26:46,310 --> 00:26:46,910
How about that?

347
00:26:51,070 --> 00:26:55,990
It's just to demonstrate, you know, this this program, I know it doesn't make a lot of sense, but

348
00:26:56,860 --> 00:27:04,720
you know, it'll be nice to Typekit, but I don't want to get into how to, you know, read in these

349
00:27:04,840 --> 00:27:06,940
characters and convert them to integers.

350
00:27:06,940 --> 00:27:09,070
Quite yet, that is something we're going to do in the future.

351
00:27:09,100 --> 00:27:14,410
So for now, I'm just going to say enter a number or type zero to quit.

352
00:27:14,680 --> 00:27:20,830
So I will say, let's just say enter a positive number or type zero to quit.

353
00:27:23,670 --> 00:27:29,380
So we'll say that I'm actually going to go compile that, and now it compiles.

354
00:27:29,400 --> 00:27:35,610
OK, so let's go ahead and run this, so into positive number of times here to quit.

355
00:27:35,910 --> 00:27:38,010
What if I just enter 60 right away?

356
00:27:39,630 --> 00:27:40,770
So you notice it just stops.

357
00:27:41,250 --> 00:27:48,690
Let's try this again at five 10, 10 five five thirty eight quits.

358
00:27:51,000 --> 00:27:52,830
So pretty cool.

359
00:27:53,070 --> 00:28:02,670
These are actually these accumulated variables are very useful for a lot of the kind of slightly more

360
00:28:02,670 --> 00:28:06,360
trivial but still important problems in computer science.

361
00:28:07,650 --> 00:28:12,900
So a good thing to know when you're basing a loop on a variable.

362
00:28:13,260 --> 00:28:21,090
This is just kind of good strategy, like if you have some question that talks about changing the state

363
00:28:21,090 --> 00:28:28,260
of some variable or needing to keep track of a sum or something like that, you can then immediately

364
00:28:28,590 --> 00:28:35,760
think about the fact that you have a tool, which is a wow loop and you're like, OK, I can use a loop

365
00:28:35,760 --> 00:28:40,350
and then I can make the loop based on that variable that changes.

366
00:28:42,290 --> 00:28:47,180
And then I just need to think of a condition, right, like I need to keep doing stuff as long as this

367
00:28:47,180 --> 00:28:50,720
variable hasn't reached a certain value or something like that.

368
00:28:51,170 --> 00:28:54,470
So you keep adding to it, maybe you keep subtracting from it.

369
00:28:54,860 --> 00:28:58,760
Maybe you're concatenating to a string.

370
00:28:58,760 --> 00:29:06,290
Or maybe you're putting a bunch of stuff in an array or something like that, just like it's a really

371
00:29:06,290 --> 00:29:10,880
good problem solving strategy to have in your bag this accumulator variable.

372
00:29:10,880 --> 00:29:17,240
So we'll be doing a lot more problems with this, and we'll come up with some slightly more complex

373
00:29:17,240 --> 00:29:20,090
ones so you can practice and get better.

374
00:29:22,010 --> 00:29:25,520
So that's kind of all I had for this lecture.

375
00:29:26,090 --> 00:29:30,530
We're going to continue on with loops, but we're not only going to look at well lives, we're going

376
00:29:30,530 --> 00:29:32,960
to start talking about another type of loop.

377
00:29:33,290 --> 00:29:37,880
And we're also going to involve arrays in this as well.

378
00:29:39,380 --> 00:29:42,170
So with that, I will see you in the next lecture.
