1
00:00:01,140 --> 00:00:03,370
OK, so really exciting moment.

2
00:00:03,390 --> 00:00:10,230
We are getting to the point in our programming journey where our programs are about to get a lot, a

3
00:00:10,230 --> 00:00:11,700
lot more interesting.

4
00:00:11,940 --> 00:00:13,400
So why is that?

5
00:00:13,410 --> 00:00:17,570
Because we are talking about loops and loops are really cool.

6
00:00:17,580 --> 00:00:22,740
And the first thing that I'm going to introduce you to is the while loop.

7
00:00:22,740 --> 00:00:25,600
So this will be the first type of loop that we talk about.

8
00:00:25,890 --> 00:00:26,940
So let's get right into it.

9
00:00:28,230 --> 00:00:30,660
So let's look at a familiar problem.

10
00:00:30,660 --> 00:00:33,090
So this is something we've been messing around with.

11
00:00:33,090 --> 00:00:38,490
It is this password problem where we're just trying to guess the password.

12
00:00:38,490 --> 00:00:43,110
So a simpler version of this problem is right here.

13
00:00:43,120 --> 00:00:47,010
We have tackled something that was a little bit even more difficult than this.

14
00:00:47,910 --> 00:00:52,890
But the thing is, is that this is kind of annoying in general as a problem.

15
00:00:52,890 --> 00:00:57,660
But the most annoying thing about it is that we have to keep running it over and over and over again,

16
00:00:57,660 --> 00:01:00,150
right to be able to guess the password again.

17
00:01:00,630 --> 00:01:05,490
Each of these little password programs we made, you had to basically rerun it, so you could then be

18
00:01:05,490 --> 00:01:07,110
asked to enter the password again.

19
00:01:08,490 --> 00:01:09,690
So that's super annoying.

20
00:01:09,840 --> 00:01:13,700
So what if we can have a program that would just keep asking for us?

21
00:01:13,710 --> 00:01:15,470
We didn't have to run the program again.

22
00:01:15,480 --> 00:01:17,490
The program would just keep asking us.

23
00:01:17,490 --> 00:01:18,870
We'd only have to run it once.

24
00:01:19,770 --> 00:01:24,480
Well, that is possible, and it's something called a while loop.

25
00:01:24,480 --> 00:01:29,520
And that's the good news is that we can actually make a program that uses this while loop and does this.

26
00:01:29,520 --> 00:01:35,250
But I guess the kind of neutral that it neutral is, it's kind of confusing that might be good or bad

27
00:01:35,250 --> 00:01:36,080
news for you.

28
00:01:36,090 --> 00:01:39,450
I don't know, but you might not think it's confusing.

29
00:01:39,450 --> 00:01:40,780
You might think it's confusing.

30
00:01:40,800 --> 00:01:41,940
Let's see what it's all about.

31
00:01:43,080 --> 00:01:45,940
So we can use this thing called a wire loop.

32
00:01:45,960 --> 00:01:52,230
It is a programming construct that keeps repeating whatever you put inside of it until a certain condition

33
00:01:52,230 --> 00:01:53,010
is false.

34
00:01:53,310 --> 00:01:56,670
So vice versa, you know, it keeps going.

35
00:01:56,670 --> 00:01:59,810
As long as the condition is true, it starts when the condition is false.

36
00:01:59,820 --> 00:02:01,110
So yes, you heard that right?

37
00:02:01,110 --> 00:02:06,840
I said condition we can use conditions exactly like the ones we've been using in our conditional statements,

38
00:02:06,840 --> 00:02:08,340
like if else, if and else.

39
00:02:09,060 --> 00:02:15,750
So just like if, for example, all the stuff that's in the parentheses it evaluates to either true

40
00:02:15,750 --> 00:02:16,500
or false.

41
00:02:17,340 --> 00:02:21,960
And if it's true, then we do the stuff in between the curly braces, right?

42
00:02:22,920 --> 00:02:28,230
While loop is the same thing, except we don't only just do the stuff in the curly braces and move on.

43
00:02:28,380 --> 00:02:31,380
If this is true in between these parentheses.

44
00:02:32,610 --> 00:02:34,920
We're going to keep going as long as it's true.

45
00:02:34,950 --> 00:02:41,550
We're going to keep repeatedly doing the stuff in here, hence the term, while while this stuff in

46
00:02:41,550 --> 00:02:42,360
here is true.

47
00:02:42,660 --> 00:02:49,470
Whatever this statement is available, as long as it evaluates to true anything in between the races

48
00:02:49,470 --> 00:02:50,490
will be Iran.

49
00:02:51,300 --> 00:02:53,820
And what'll happen is, let's say this is true.

50
00:02:53,820 --> 00:02:54,990
It runs the stuff in here.

51
00:02:54,990 --> 00:02:56,670
It gets to the bottom, it comes back up.

52
00:02:56,670 --> 00:02:58,260
It says, Is this still true?

53
00:02:58,270 --> 00:02:59,110
Oh, it's still true.

54
00:02:59,130 --> 00:03:02,040
Yeah, I go do this again and get to the bottom and come back up.

55
00:03:02,340 --> 00:03:03,480
Is this still true?

56
00:03:03,510 --> 00:03:04,230
Yes, it is.

57
00:03:04,230 --> 00:03:04,980
I do this again.

58
00:03:04,980 --> 00:03:06,510
I go to the bottom, I come back up.

59
00:03:06,510 --> 00:03:11,490
It just keeps doing that over and over until this thing in here.

60
00:03:11,490 --> 00:03:18,360
In between the parentheses evaluates to false, in which case it will then move past the close curly

61
00:03:18,360 --> 00:03:23,940
bracket and continue on with the rest of your code if there are other things in your program.

62
00:03:25,440 --> 00:03:27,820
So pretty cool.

63
00:03:27,870 --> 00:03:35,190
Let's go back to this program and see if we can change it up to use a while loop and have it ask us

64
00:03:35,700 --> 00:03:40,650
multiple times with one run of the program without us having to rerun it all the time, right?

65
00:03:41,430 --> 00:03:44,220
So I have this pulled up in Visual Studio here.

66
00:03:44,520 --> 00:03:45,960
Here's that program.

67
00:03:46,440 --> 00:03:49,290
So let's go ahead and just run it right now.

68
00:03:49,300 --> 00:03:55,740
I think I've actually compiled it already, so I'm going to go ahead and run it, and I'll just say,

69
00:03:56,250 --> 00:03:58,860
you know, pass and it's like, try again.

70
00:03:58,860 --> 00:04:04,200
And it's like, Oh, I was wrong, I got it running again and I'm like, half W. and try again.

71
00:04:04,200 --> 00:04:05,040
And I ran again.

72
00:04:05,040 --> 00:04:06,810
And then I say, pass word.

73
00:04:07,800 --> 00:04:09,060
And you guessed it.

74
00:04:10,110 --> 00:04:11,000
Well, you know what?

75
00:04:11,040 --> 00:04:12,000
That's kind of annoying.

76
00:04:12,030 --> 00:04:16,050
We're going to put a while loop in here and we are going to fix this up.

77
00:04:16,050 --> 00:04:19,050
So how do we change this to have a while loop?

78
00:04:19,090 --> 00:04:25,410
Well, we got to think, what is the stuff that we want to happen inside the loop?

79
00:04:25,410 --> 00:04:28,630
So this stuff, right, repeatedly do stuff in here.

80
00:04:28,650 --> 00:04:32,100
What is the repeatedly do stuff in here that we want?

81
00:04:33,320 --> 00:04:36,020
Well, we want to keep asking the user.

82
00:04:36,380 --> 00:04:36,840
Right.

83
00:04:36,860 --> 00:04:44,420
So that's one thing, keep asking the user for a password and we want to get that stuff from the user

84
00:04:44,420 --> 00:04:45,590
when they type it in, right?

85
00:04:46,710 --> 00:04:51,700
So keep asking the user right here, get stuff from the user.

86
00:04:52,380 --> 00:04:52,770
Right.

87
00:04:53,100 --> 00:04:54,450
So pretty simple.

88
00:04:54,960 --> 00:04:56,760
That's what we want to keep doing.

89
00:04:56,760 --> 00:05:00,900
So we're going to have these two lines inside of our wow loop.

90
00:05:02,980 --> 00:05:03,610
So.

91
00:05:04,780 --> 00:05:07,190
We don't necessarily have to deal with all of this.

92
00:05:07,210 --> 00:05:15,550
I'm going to go ahead and delete this, and I'm going to get rid of this f right here.

93
00:05:17,920 --> 00:05:24,610
And what I'm going to do is I'm going to say I'm actually I move this down and I'm going to say while

94
00:05:25,510 --> 00:05:26,350
and what was this?

95
00:05:26,350 --> 00:05:36,910
While input is not equal to password, then what's the stuff that we do into this stuff right here?

96
00:05:36,930 --> 00:05:42,010
I'm going to copy this and paste it in here and dent it.

97
00:05:42,640 --> 00:05:44,410
This is the stuff that we're going to do.

98
00:05:44,620 --> 00:05:48,100
It's just going to say into the password.

99
00:05:50,230 --> 00:05:55,330
So I can also put a little initial check here where I can say.

100
00:05:58,780 --> 00:06:05,080
You know, into the password, I can like, ask them once and then I can use it, get line.

101
00:06:09,440 --> 00:06:13,220
To get it and then what I can do is I can just say if

102
00:06:16,070 --> 00:06:25,940
input not equal to the password, then I'll go ahead and do this while.

103
00:06:30,090 --> 00:06:31,230
So I didn't that.

104
00:06:31,650 --> 00:06:35,940
So basically, what's going on is that I asked the user for a password.

105
00:06:36,450 --> 00:06:38,490
I get that password from them.

106
00:06:39,490 --> 00:06:42,310
And then I checked to see if they have the right thing.

107
00:06:43,680 --> 00:06:49,320
And what this really is is it's not just checking to see what's right as the right thing is basically

108
00:06:49,530 --> 00:06:51,080
based on it being wrong.

109
00:06:51,090 --> 00:06:58,320
So if it's wrong, if it's not the password, then we go in here and we say while it's wrong.

110
00:06:59,930 --> 00:07:01,460
Keep asking them for the password.

111
00:07:03,100 --> 00:07:07,180
So we can actually change this, maybe actually to try again, try again.

112
00:07:10,170 --> 00:07:19,110
And then we'll keep we'll just keep getting it, and once it's actually correct, so once input is equal

113
00:07:19,110 --> 00:07:26,670
to password, so this would be false, then it'll basically break out of this wow loop and then it will

114
00:07:26,670 --> 00:07:27,690
go down here.

115
00:07:27,930 --> 00:07:33,210
It's already this condition has already been decided, so it hits this.

116
00:07:33,210 --> 00:07:36,600
And once it's done with this, it just continues on past this curly brace.

117
00:07:38,010 --> 00:07:40,560
And that means that they would have guessed it right.

118
00:07:40,560 --> 00:07:45,120
So we can just leave this down here because that means that in fact they did.

119
00:07:45,990 --> 00:07:49,980
They must have guessed it because otherwise it wouldn't be done with the wow look, right?

120
00:07:50,400 --> 00:07:53,280
We'd still be in the while loop if they hadn't guessed it yet.

121
00:07:54,860 --> 00:08:04,760
So let's go ahead and try this out and see how it goes, so I'm going to compile it and I'm going to

122
00:08:06,200 --> 00:08:07,040
go ahead and run it.

123
00:08:07,520 --> 00:08:13,630
So enter the password pass, try in your training in the past.

124
00:08:13,640 --> 00:08:19,730
Well, training in we're training in password, you guessed it.

125
00:08:21,080 --> 00:08:23,900
Cool, so pretty neat, right?

126
00:08:25,220 --> 00:08:32,300
So let's continue on learning a bit more about these lives, because there are some strange things that

127
00:08:32,300 --> 00:08:37,550
can happen due to this nature of how it works with the condition inside here.

128
00:08:39,230 --> 00:08:41,960
So hop back over to the presentation here.

129
00:08:44,690 --> 00:08:47,540
So what about this?

130
00:08:49,270 --> 00:08:57,670
I want you to make a new program that has a main function, and then it just only has this code in here.

131
00:08:59,020 --> 00:09:01,360
So you're going to tape while true.

132
00:09:02,800 --> 00:09:09,520
And see out, you can put anything you don't have to say, I'm unstoppable, but is kind of a hint at

133
00:09:09,520 --> 00:09:15,490
what is going to happen, then go ahead and try running this program and it will be quite interesting

134
00:09:15,490 --> 00:09:16,300
what you see.

135
00:09:17,650 --> 00:09:19,750
So go ahead and pass the video and run this.

136
00:09:19,930 --> 00:09:26,680
And then don't worry, you can play the video again, and I won't tell you how to fix what is probably

137
00:09:26,680 --> 00:09:27,490
going to happen.

138
00:09:29,290 --> 00:09:37,330
So if your program went careening out of control without any sign of stopping, you can always do something

139
00:09:37,330 --> 00:09:42,980
like clicking in the console area and pressing control see, which means holding control and pressing

140
00:09:42,980 --> 00:09:43,360
and see.

141
00:09:43,990 --> 00:09:48,880
What this does is it kills the current process that is running and that process is your program.

142
00:09:49,840 --> 00:09:54,940
So most likely, it's this unstoppable loop program that you've just done right now.

143
00:09:56,050 --> 00:10:01,000
So as you continue through your program a journey, you will encounter this probably quite often.

144
00:10:01,000 --> 00:10:05,200
Actually, you'll have these infinite loops and it won't stop and you'll have to control C and then

145
00:10:05,200 --> 00:10:10,330
you'll have to go debug them and fix them so your program cannot get stuck there in that loop, just

146
00:10:10,330 --> 00:10:11,890
bouncing from bottom to top.

147
00:10:14,710 --> 00:10:18,280
So let's take a look at this.

148
00:10:19,150 --> 00:10:22,930
So I'm going to go ahead, actually, and just do this.

149
00:10:22,930 --> 00:10:32,680
In case you didn't run that, I'll show you, but you probably already ran it on this field, blah.

150
00:10:34,000 --> 00:10:35,320
And I'll save it.

151
00:10:35,320 --> 00:10:38,140
And then we will compile it, and then I will run it.

152
00:10:38,140 --> 00:10:43,060
And it's just going, look at it blinking and you'll notice as I let it keep running.

153
00:10:43,210 --> 00:10:48,100
Actually, I don't know if you'll be able to hear it, but my computer fan is starting to get faster.

154
00:10:48,760 --> 00:10:53,800
The computers just smashing through various lines, printing them out, going crazy.

155
00:10:54,520 --> 00:10:57,040
So yeah, if you get freaked out.

156
00:10:57,070 --> 00:11:00,880
Just control C and that will stop it.

157
00:11:02,740 --> 00:11:04,420
OK, so I'm going to get rid of that.

158
00:11:07,060 --> 00:11:15,070
And I'm going back here to the presentation, and so we should make a modification to our loop condition

159
00:11:15,070 --> 00:11:20,860
in our current program because you know what, it kind of almost is like an infinite loop.

160
00:11:21,100 --> 00:11:26,080
If you make the user, keep having to guess, what if the user can't guess your password?

161
00:11:26,080 --> 00:11:30,580
I mean, it's probably pretty hard, like they have no idea about any constraints about the password,

162
00:11:30,590 --> 00:11:33,280
so it can just take them forever and ever and ever and ever.

163
00:11:34,210 --> 00:11:39,940
And do we really want them to have to look up how to exit out of the program in the terminal?

164
00:11:39,940 --> 00:11:41,710
They have to like control C.

165
00:11:42,280 --> 00:11:43,300
That's not that great.

166
00:11:43,300 --> 00:11:46,420
We probably should have an easier option for our users, right?

167
00:11:48,130 --> 00:11:50,770
So let's make a modification to this program.

168
00:11:51,280 --> 00:11:52,970
So I want to go back in here.

169
00:11:53,710 --> 00:11:58,930
What we should probably do is say that they have an option to quit.

170
00:11:59,350 --> 00:12:08,560
So what we will say is enter the password or type Q to quit.

171
00:12:08,590 --> 00:12:09,160
How about that?

172
00:12:10,320 --> 00:12:12,020
And then what we can do here.

173
00:12:13,310 --> 00:12:18,710
Is I'll add this in here as well, try again or Typekit Q to quit.

174
00:12:20,630 --> 00:12:23,820
All we have to do is change our while condition here.

175
00:12:24,710 --> 00:12:30,260
So what happens is that the input is getting saved over and over again into this variable.

176
00:12:30,260 --> 00:12:33,980
So when I ran it and I typed pass, it got saved in input.

177
00:12:35,680 --> 00:12:38,560
You know, then it went up here and it said, is pass equal in the password?

178
00:12:38,590 --> 00:12:39,380
No, it's not.

179
00:12:39,400 --> 00:12:40,990
And then so it said, try again.

180
00:12:41,200 --> 00:12:48,310
And then I entered past W and so passed W over wrote Pass like pass was starting here and now it's passed

181
00:12:48,310 --> 00:12:48,820
w.

182
00:12:49,180 --> 00:12:51,850
Then I go back to the top, it says is pass w.

183
00:12:53,960 --> 00:12:55,340
Not equal to passwords.

184
00:12:56,860 --> 00:13:00,040
Sorry, last time I said equal, it's really checking if it's not equal to password.

185
00:13:00,820 --> 00:13:03,510
And it's like, yes, it's true, it's not equal, and then it goes again.

186
00:13:03,510 --> 00:13:04,660
And I said past W.

187
00:13:04,690 --> 00:13:08,110
Oh, and that got saved in here, so it keeps overwriting it right?

188
00:13:08,710 --> 00:13:12,280
If we type Q, Q will get saved into input.

189
00:13:13,580 --> 00:13:20,930
And then it will say, while Q not equal to password, is CUNA equal to password, well, yeah, IQ

190
00:13:20,930 --> 00:13:22,830
is not equal to password, so keep going.

191
00:13:22,850 --> 00:13:29,660
So what we can do is add something to this safe while the input is not equal to password, so it's not

192
00:13:29,660 --> 00:13:30,680
the right answer.

193
00:13:32,300 --> 00:13:33,920
And while

194
00:13:36,380 --> 00:13:46,220
the input is also not equal to the screen Q because we're reading into a string right or reading into

195
00:13:46,220 --> 00:13:46,790
a string.

196
00:13:48,650 --> 00:13:49,940
So we want to check.

197
00:13:50,150 --> 00:13:53,600
We want to loop as long as they haven't got the right answer, which is this.

198
00:13:54,680 --> 00:14:00,650
And as long as they have not entered a queue because once they've entered queue, we want to stop,

199
00:14:00,650 --> 00:14:03,150
right, then we're going to once we queue.

200
00:14:03,770 --> 00:14:05,020
It's going to go back up.

201
00:14:05,030 --> 00:14:13,310
It's going to say, Oh, input is equal to queue because both sides of the and this is the left side

202
00:14:13,310 --> 00:14:13,940
of the end.

203
00:14:14,780 --> 00:14:17,790
This is the right side of the and they both have to be true.

204
00:14:17,810 --> 00:14:20,510
Both have to be true for this stuff to run, right?

205
00:14:20,990 --> 00:14:25,360
So if this is false, true and false is false.

206
00:14:25,370 --> 00:14:30,380
So it will actually stop the well, it'll continue past the F and it will say, you guessed it.

207
00:14:31,040 --> 00:14:33,450
So let's go ahead and check this theory.

208
00:14:33,490 --> 00:14:33,830
All right.

209
00:14:37,010 --> 00:14:39,460
So I run this into the password Typekit account.

210
00:14:39,500 --> 00:14:46,940
I'm just going to say pass again and then I'm going to say past W. and keep saying, Oh, and then I'm

211
00:14:46,940 --> 00:14:47,990
going to say, You know what?

212
00:14:47,990 --> 00:14:48,970
I don't know this.

213
00:14:48,980 --> 00:14:49,940
I just want to quit.

214
00:14:49,940 --> 00:14:53,030
I type Q and it just says, you guessed it.

215
00:14:53,270 --> 00:14:55,070
But did we really guess it?

216
00:14:55,310 --> 00:14:56,840
No, we didn't use it, right?

217
00:14:57,140 --> 00:14:59,990
If we quit, we shouldn't be rewarded with guessing it.

218
00:14:59,990 --> 00:15:12,770
So we should basically say something here that says if input is equal to password, then you can say,

219
00:15:14,300 --> 00:15:15,140
you guessed it right.

220
00:15:17,350 --> 00:15:26,990
So and else you can just say whatever you can just say thank you.

221
00:15:27,330 --> 00:15:33,960
And again, not a semicolon, I cannot tell you right now.

222
00:15:33,980 --> 00:15:34,980
OK, cool.

223
00:15:35,000 --> 00:15:38,820
So let's save that unless we run this, OK?

224
00:15:38,840 --> 00:15:40,550
That is a huge problem.

225
00:15:43,450 --> 00:15:46,490
And the line could not use template parameter.

226
00:15:46,520 --> 00:15:48,310
Oh yeah, it's just the Allen in line.

227
00:15:48,340 --> 00:15:49,210
That's what I was forgetting.

228
00:15:51,760 --> 00:15:55,780
OK, so we save it and try it again.

229
00:15:59,830 --> 00:16:07,330
OK, so let's go ahead and run this, so yet this time, if I, you know, I do the same thing and I

230
00:16:07,330 --> 00:16:14,170
say Pass W. and pass, whoa, and you go, I say to to quit and it's just a thank you, come again.

231
00:16:14,170 --> 00:16:16,060
And it just might say, you guessed it.

232
00:16:19,280 --> 00:16:24,020
OK, so that's kind of all I have to say about the wild loops.

233
00:16:24,500 --> 00:16:30,320
This is going to get really interesting, though, because now we can do a lot more stuff in here and

234
00:16:30,320 --> 00:16:34,880
that will help us actually solve some pretty cool problems.

235
00:16:34,910 --> 00:16:39,050
So we will continue on with the wild loops and the next lecture.

236
00:16:39,320 --> 00:16:43,940
And after we are kind of comfortable with, then we will move on to other types of loops.

237
00:16:44,630 --> 00:16:46,580
So with that, I'll see you in the next lecture.
