1
00:00:00,090 --> 00:00:00,480
OK.

2
00:00:00,510 --> 00:00:02,340
Welcome to another lecture.

3
00:00:02,400 --> 00:00:11,040
We are now finishing up with our lectures on wildlife, and we are starting a lecture series on a new

4
00:00:11,040 --> 00:00:13,020
type of loop, which is called a for loop.

5
00:00:14,800 --> 00:00:21,640
So you might be thinking, well, why do we need another loop in a while, loop enough a loop as a loop,

6
00:00:21,640 --> 00:00:22,030
right?

7
00:00:22,720 --> 00:00:28,840
Well, technically, yes, you could use it while loops for everything, but there are certain situations

8
00:00:28,840 --> 00:00:33,700
which for Loop is actually easier to use and kind of more sensible.

9
00:00:34,480 --> 00:00:35,770
So take the below code simply.

10
00:00:35,770 --> 00:00:39,400
For example, this is from our last program that we had in the last lecture, actually.

11
00:00:40,720 --> 00:00:46,210
So you noticed that what we had to do was define a position variable set equal to zero an hour, while

12
00:00:46,210 --> 00:00:47,710
Loop was kind of based on that right?

13
00:00:48,520 --> 00:00:51,190
We said loop as long as position is less than five.

14
00:00:51,580 --> 00:00:56,680
But to make sure that everything worked out, we had to do a plus plus position.

15
00:00:56,860 --> 00:00:58,570
You can also do position plus plus.

16
00:00:59,620 --> 00:01:05,170
The problem is, if we forgot to do this, somehow we forgot to put this last line in here, which is

17
00:01:05,170 --> 00:01:05,800
easy, right?

18
00:01:05,800 --> 00:01:10,090
Because you define your condition, you're like, I want it to loop as long as position is less than

19
00:01:10,090 --> 00:01:10,450
five.

20
00:01:11,260 --> 00:01:13,090
Here I do the stuff I care about.

21
00:01:13,090 --> 00:01:15,280
And then you just put a close bracket and you forget this.

22
00:01:15,460 --> 00:01:17,530
It's really easy to forget this line right here.

23
00:01:18,400 --> 00:01:22,600
If we have to forget that the thing is that the loop would actually run infinitely.

24
00:01:23,230 --> 00:01:27,960
So I will show you that infinite loop today and the editor.

25
00:01:28,450 --> 00:01:30,190
You might have already seen that, though.

26
00:01:31,650 --> 00:01:34,080
So I could just be showing you again in that case.

27
00:01:35,330 --> 00:01:42,950
But the problem that that really arises is the reason why this is looking infinitely if we don't put

28
00:01:42,950 --> 00:01:46,100
this is because think about it, position is equal to zero, right?

29
00:01:47,670 --> 00:01:54,120
Well, if you look at this condition here, we're basically asking this year less than five, yeah,

30
00:01:54,120 --> 00:01:56,700
zero is less than five, so we run this stuff in here.

31
00:01:56,910 --> 00:01:58,260
Imagine this doesn't exist.

32
00:01:58,860 --> 00:02:00,030
Position is still zero.

33
00:02:00,030 --> 00:02:05,640
We go back to the top here and we ask is zero less than five we go down is zero less than five zero

34
00:02:05,640 --> 00:02:10,020
less than five zero less than five zero less, but it just keeps going over and over again.

35
00:02:10,080 --> 00:02:15,030
Zero is always less than five and position is always zero.

36
00:02:15,030 --> 00:02:18,650
So always going to be true, always going to run the loop.

37
00:02:18,660 --> 00:02:21,000
This condition is true and evaluates to true.

38
00:02:21,690 --> 00:02:23,480
It's going to keep going and going and going and going.

39
00:02:23,500 --> 00:02:24,450
Same thing for count.

40
00:02:24,450 --> 00:02:29,240
If we did not increment count, it would just be like count less than five.

41
00:02:29,250 --> 00:02:33,210
Well, yeah, count is zero zero less than five zero less than five zero less than five, and we just

42
00:02:33,210 --> 00:02:33,870
keep doing that.

43
00:02:35,970 --> 00:02:42,180
So if you had a lot of these loops in your program, it would be easy to make a mistake and get trapped

44
00:02:42,180 --> 00:02:43,050
in an infinite loop.

45
00:02:43,050 --> 00:02:44,570
And then you'd have to track that down.

46
00:02:44,580 --> 00:02:49,020
And if your code has like thousands of lines and it's happening in multiple spots like you fixed one

47
00:02:49,020 --> 00:02:50,850
infinite loop, then you had another one.

48
00:02:51,910 --> 00:02:53,260
It can get kind of annoying.

49
00:02:53,290 --> 00:02:58,300
It's just kind of a weird idea to have to put this in here if it was the only way.

50
00:02:58,540 --> 00:02:59,020
Sure.

51
00:02:59,360 --> 00:03:00,580
You know, you get used to it.

52
00:03:01,960 --> 00:03:07,570
It's fine, but the fact is, is that we know how many times we want to loop.

53
00:03:08,900 --> 00:03:15,470
The while it was kind of useful for when we want to just loop based on some condition, we're not really

54
00:03:15,470 --> 00:03:16,790
sure how many times.

55
00:03:17,960 --> 00:03:25,830
We need to loop, but we know for sure that we want to loop until something is like true or false.

56
00:03:25,850 --> 00:03:29,000
You know, and tell us some condition is met kind of would be intel.

57
00:03:29,010 --> 00:03:32,750
Whatever in here is evaluated to false because that's technically the syntax, right?

58
00:03:33,710 --> 00:03:37,880
But it's kind of more based on just a condition, right?

59
00:03:37,910 --> 00:03:43,490
Not not a certain amount of times, but this program we really cared about looping five times, right?

60
00:03:44,460 --> 00:03:50,100
Because we didn't we went win position was zero and one position was one in position with two in position

61
00:03:50,100 --> 00:03:51,490
with three in position was four.

62
00:03:51,510 --> 00:03:53,970
So that was five times and then one position was five.

63
00:03:53,970 --> 00:03:55,740
We did not run the code in here.

64
00:03:56,520 --> 00:04:03,660
So we knew from the get go that we hardcoded this array to five and that we were going to want to loop

65
00:04:03,660 --> 00:04:08,130
five times to get five numbers for each position of the array.

66
00:04:09,950 --> 00:04:15,440
This is where a fault gets becomes a lot better solution.

67
00:04:16,040 --> 00:04:17,750
So let's take a look at this.

68
00:04:18,740 --> 00:04:24,800
A for loop is useful for running for a specific number of iterations, which is what our program was

69
00:04:24,800 --> 00:04:25,610
doing right there.

70
00:04:25,730 --> 00:04:27,050
So for loop is better.

71
00:04:28,690 --> 00:04:36,400
So what we can do is that we can define the counter variable actually on the same line as the loop declaration,

72
00:04:36,400 --> 00:04:37,390
which is pretty cool.

73
00:04:38,080 --> 00:04:44,190
So you notice down in here, I have a for loop and right here rather than into position equals zero

74
00:04:44,200 --> 00:04:45,850
like we had above the well loop.

75
00:04:46,540 --> 00:04:52,310
We can put it in the same line actually inside the parentheses here.

76
00:04:53,140 --> 00:04:55,120
And this doesn't have to be I.

77
00:04:55,120 --> 00:05:00,280
It can also be position as we'll talk about here on the next slides.

78
00:05:00,880 --> 00:05:03,970
We're going to break this piece of code down with these three components.

79
00:05:04,120 --> 00:05:07,360
This one right here, this one right here and this one right here.

80
00:05:08,570 --> 00:05:15,230
So first off, you want to use the word four instead of while you have the same parentheses as the while

81
00:05:15,230 --> 00:05:19,310
loop, but it's going to have more stuff in it and then you'll also use the curly braces.

82
00:05:20,890 --> 00:05:25,510
So this part right here, this is what I was talking about with the interposition equals zero.

83
00:05:26,020 --> 00:05:28,210
So you don't have to use the character.

84
00:05:28,210 --> 00:05:34,510
I this is really just to define our counter variable, just like the interposition equals zero and you

85
00:05:34,510 --> 00:05:38,590
could have this equal and you can have this i b the word position instead.

86
00:05:38,800 --> 00:05:42,040
So position here, position here, position here.

87
00:05:43,630 --> 00:05:48,190
I sense for iteration, if you don't really know what to call the loop is kind of a de facto thing to

88
00:05:48,190 --> 00:05:48,790
call it.

89
00:05:48,830 --> 00:05:50,830
I that's just what a lot of programmers do.

90
00:05:51,700 --> 00:05:55,090
But you know, you should call it whatever makes sense for the program.

91
00:05:56,820 --> 00:06:03,360
So also note that you do not have to put this here, you can also put in A. equals zero above the for

92
00:06:03,360 --> 00:06:10,470
loop and then just leave this section blank this underline section, except for the semicolon, and

93
00:06:10,470 --> 00:06:12,300
then you would still have the rest of this in here.

94
00:06:12,540 --> 00:06:13,820
So let's look at the rest of this.

95
00:06:13,830 --> 00:06:15,090
What is the rest of this line?

96
00:06:15,480 --> 00:06:20,910
So after finishing this first part where we declare the counter, so we start off at zero, start our

97
00:06:20,910 --> 00:06:21,690
counter at zero.

98
00:06:22,530 --> 00:06:30,570
We now need to say how long we're going to have the Lupron, how many iterations, what is this condition

99
00:06:30,570 --> 00:06:32,260
based on the iterations?

100
00:06:32,280 --> 00:06:34,770
Well, we need to use a comparison operator for this.

101
00:06:35,580 --> 00:06:38,760
So what we're seeing right here is that I started zero.

102
00:06:39,300 --> 00:06:45,360
The loop is going to run while I is less than size, so that's how many times it will run.

103
00:06:45,360 --> 00:06:48,870
It will run from zero to size minus one, right?

104
00:06:49,500 --> 00:06:55,440
Once in his size, it'll actually break out of the loop and continue on with the program, the rest

105
00:06:55,440 --> 00:06:55,980
of the program.

106
00:06:57,020 --> 00:07:01,790
So starting at zero, going up to but not including size.

107
00:07:04,400 --> 00:07:08,120
So finally, we have to define what the increments will be.

108
00:07:08,150 --> 00:07:13,940
So here we're just doing a simple A-plus plus, so that means we're incrementing I buy one each time.

109
00:07:14,600 --> 00:07:19,250
You do not have to do A-plus, plus you do not have to just increment by one each time you can increment

110
00:07:19,250 --> 00:07:20,300
by whatever you like.

111
00:07:20,540 --> 00:07:23,030
You can use different operators in here and stuff.

112
00:07:25,010 --> 00:07:30,890
So the last few things, like I said, are the curly braces inside of the curly braces is where the

113
00:07:30,890 --> 00:07:31,820
magic happens.

114
00:07:32,210 --> 00:07:37,670
That is where all of the stuff that you want to run multiple iterations should go right.

115
00:07:37,700 --> 00:07:42,230
All the stuff that you want to be repeated multiple times for this amount of times.

116
00:07:43,220 --> 00:07:45,500
Is going to happen where it says do stuff here.

117
00:07:46,100 --> 00:07:48,170
And of course, that could be as many lines as you want.

118
00:07:48,200 --> 00:07:49,190
Similar to the wallet.

119
00:07:50,300 --> 00:07:55,280
So let's go ahead and head to the editor and modify that while the program to now instead use for loops.

120
00:07:59,460 --> 00:08:02,430
So here I just kind of already written it out.

121
00:08:03,450 --> 00:08:09,510
So what I'm going to do is kind of show you how this was.

122
00:08:09,960 --> 00:08:17,100
So this was just a while and then we had position less than five, right?

123
00:08:18,120 --> 00:08:21,900
That led us to have to have position equals zero here.

124
00:08:22,290 --> 00:08:25,980
We also had to have an eight count equals zero.

125
00:08:27,820 --> 00:08:36,310
And we have this + + position in the loop, but we don't need that anymore.

126
00:08:37,090 --> 00:08:49,400
We definitely don't need this + + position because what we can do in here is just put this part right.

127
00:08:49,420 --> 00:08:55,180
I copied pasted this here and then put how long the loop runs.

128
00:08:57,440 --> 00:09:02,930
And then I put my position +, +, +, +, + + position here.

129
00:09:03,650 --> 00:09:05,240
So here's the starting condition.

130
00:09:06,340 --> 00:09:06,640
Right.

131
00:09:06,670 --> 00:09:14,410
This is the start of this starting value for the iteration, for the variable cell position equals zero

132
00:09:15,520 --> 00:09:19,430
goes from zero up until five and it stops at five.

133
00:09:19,450 --> 00:09:25,510
So not including it's not going to actually run when position is five because it's positioned less than

134
00:09:25,510 --> 00:09:25,810
five.

135
00:09:25,810 --> 00:09:28,150
And then we have our increment right here on one line.

136
00:09:29,520 --> 00:09:34,080
For the count, we had the same thing, except what we did now.

137
00:09:34,260 --> 00:09:35,760
We don't need the count anymore.

138
00:09:36,150 --> 00:09:41,820
There was a count plus plus in here or actually it was plus plus count.

139
00:09:43,110 --> 00:09:45,150
We don't need that anymore because we have our.

140
00:09:46,310 --> 00:09:52,730
Incrementing happening right here before all that was in here, it was actually just this count less

141
00:09:52,730 --> 00:09:55,430
than five when it was a while loop instead of before.

142
00:09:56,000 --> 00:09:59,960
Now we have count less than five as this middle piece.

143
00:10:00,590 --> 00:10:01,880
We're starting at zero.

144
00:10:01,880 --> 00:10:04,100
Just like this in position equals zero.

145
00:10:04,580 --> 00:10:07,550
And that is our entire for loop.

146
00:10:07,580 --> 00:10:13,430
You notice that our program is a lot shorter now, a lot more concise.

147
00:10:14,150 --> 00:10:21,680
So this is something that you should be using when you know how many times you want to iterate over

148
00:10:21,680 --> 00:10:22,070
something.

149
00:10:22,070 --> 00:10:24,710
So specifically, we know the size of the array.

150
00:10:24,950 --> 00:10:28,580
That's why we should, instead of while loop use a for loop, right?

151
00:10:29,540 --> 00:10:34,740
So let's go ahead and just compile this so we can see what's going on.

152
00:10:34,760 --> 00:10:36,300
I'm just going to leave it as well.

153
00:10:36,300 --> 00:10:37,690
Loops to CBP.

154
00:10:38,030 --> 00:10:43,160
It probably should be changed to four loops if I was actually going to hold on to this, but I go ahead

155
00:10:43,160 --> 00:10:43,970
and compile it.

156
00:10:44,390 --> 00:10:47,930
So let's go ahead and run this wild to see.

157
00:10:49,340 --> 00:10:56,780
So you notice of yeah, I actually forgot to re say this then that a few times apologies for that.

158
00:10:59,450 --> 00:11:03,060
So recompile re run it services.

159
00:11:03,080 --> 00:11:11,690
Enter a number four position zero three three two two one five six.

160
00:11:13,130 --> 00:11:16,550
And then you notice that they all print out nicely in there.

161
00:11:16,760 --> 00:11:19,100
So, oops, I screwed up too much.

162
00:11:22,460 --> 00:11:27,710
So let's scroll up just a bit so I can get them on here.

163
00:11:29,640 --> 00:11:30,180
There you go.

164
00:11:32,680 --> 00:11:39,520
So here we have the same exact results that we had before for the while loop, except we did it in a

165
00:11:39,520 --> 00:11:42,310
much more clear and concise manner.

166
00:11:42,490 --> 00:11:46,840
So here I entered three two one five six and it printed out three two one five six.

167
00:11:47,530 --> 00:11:54,700
This part right here that I mistakenly had was from previous save right and we save, but that is something

168
00:11:54,700 --> 00:11:58,600
that I wanted to show you, which is what happens with the infinite loop.

169
00:11:59,140 --> 00:12:01,600
So let me go ahead and make another while loop here.

170
00:12:01,630 --> 00:12:04,660
Just in case you haven't seen the infinite loop, I want to show you this.

171
00:12:05,740 --> 00:12:09,490
So let's go back to this whole and position equals zero.

172
00:12:11,690 --> 00:12:17,060
So what I can do is, I say, well positioned less than five, because that's what we had, right?

173
00:12:18,950 --> 00:12:28,730
And then I'm going to go in here and I'm just going to say, see out, I'm in the side a loop and then

174
00:12:28,730 --> 00:12:33,350
put it in line, so we initialize it to zero.

175
00:12:33,380 --> 00:12:41,390
We say position less than five, but let's say we forget to do this classless position, you know,

176
00:12:41,540 --> 00:12:45,620
or position plus plus whatever thing we're going to do.

177
00:12:46,460 --> 00:12:48,380
And we just save it and run it.

178
00:12:49,370 --> 00:12:50,450
So I save this.

179
00:12:52,920 --> 00:12:57,030
Compile it, and I'm actually going to bring this up a little bit more.

180
00:12:58,370 --> 00:12:58,950
We run it.

181
00:12:59,130 --> 00:13:03,930
It just keeps saying over and over and over and over again, I'm inside a loop, I'm inside a loop,

182
00:13:03,930 --> 00:13:07,680
I'm inside a loop and it kind of just flashing the same thing.

183
00:13:07,690 --> 00:13:08,850
So you can't really see.

184
00:13:08,850 --> 00:13:17,460
But like if I was to put, let's just say I did something like this, like I said, position minus minus.

185
00:13:18,690 --> 00:13:21,510
And then I said, see out.

186
00:13:24,110 --> 00:13:30,740
Position, you know, here I'm changing the value of position, but it's changing in the wrong direction,

187
00:13:30,740 --> 00:13:32,180
we're still going to have an issue.

188
00:13:32,210 --> 00:13:35,270
So let's see what happens when I do this.

189
00:13:36,260 --> 00:13:37,400
It's a little funky there.

190
00:13:38,150 --> 00:13:38,870
Let me be clear.

191
00:13:40,850 --> 00:13:45,730
So looking at just counting down into negatives, just going and going and going and going and going,

192
00:13:45,740 --> 00:13:50,120
so this shows you a little bit different output that really gives you a better idea of this infinite

193
00:13:50,120 --> 00:13:53,840
loop that's just continuing to count down.

194
00:13:54,080 --> 00:13:54,430
All right.

195
00:13:56,780 --> 00:14:03,290
So that is why that is why sometimes the while loops can become sort of burdensome and it's better to

196
00:14:03,290 --> 00:14:06,500
use a for loop if the situation makes sense.

197
00:14:06,500 --> 00:14:13,040
If you know how many times you want to iterate, if you're iterating over some container that is specific,

198
00:14:13,040 --> 00:14:19,940
known size, maybe not directly known by you, but you're able to call some sort of function that gives

199
00:14:19,940 --> 00:14:24,590
you the size, anything like that, a good situation to use for that.

200
00:14:26,910 --> 00:14:31,080
OK, so this was just meant to be a basic introduction to four lips.

201
00:14:31,560 --> 00:14:38,400
The main things that I wanted you to take away from this are how to create a for loop what these things

202
00:14:38,400 --> 00:14:40,830
are inside of here, just as a quick recap.

203
00:14:41,040 --> 00:14:43,740
This is where the counter for the for loop starts.

204
00:14:44,750 --> 00:14:50,990
This is the condition in which how long before Liverpool run for and this is the incremental version

205
00:14:50,990 --> 00:14:52,880
of this counter for the for loop.

206
00:14:55,240 --> 00:15:02,260
So with that, I will see you in the next lectures where we will practice for loops just as we practiced

207
00:15:02,260 --> 00:15:02,920
while loops.
