1
00:00:00,610 --> 00:00:08,890
OK, so in this lecture, we're going to go over some practice problems for the for loops.

2
00:00:09,160 --> 00:00:14,050
So we introduce that just recently, but just like the while loops.

3
00:00:14,650 --> 00:00:20,980
I think it's important that we kind of go over it a little bit more with some practice problems where

4
00:00:20,980 --> 00:00:24,760
you can kind of try and solve things for yourself.

5
00:00:24,760 --> 00:00:27,100
That way, it will really embed that knowledge.

6
00:00:28,240 --> 00:00:29,950
So we only have two problems.

7
00:00:30,790 --> 00:00:32,950
I think that you should be able to figure them out.

8
00:00:33,280 --> 00:00:41,080
I will give a hint on one of them kind of in between just to help you out a little bit if it seems a

9
00:00:41,080 --> 00:00:41,920
little confusing.

10
00:00:41,920 --> 00:00:48,220
And then of course, after each, I will be going over the solution or a solution to them.

11
00:00:48,220 --> 00:00:50,260
There's probably multiple solutions that you can do.

12
00:00:51,430 --> 00:00:51,820
All right.

13
00:00:51,820 --> 00:00:53,560
So let's go ahead and get started.

14
00:00:53,570 --> 00:01:01,870
So problem one description says to make a program that takes in two integers from the user and then

15
00:01:01,870 --> 00:01:07,260
prints out the result of the first number being raised to the power of the second number.

16
00:01:07,300 --> 00:01:09,910
So that is where the second number is the exponent.

17
00:01:10,860 --> 00:01:13,830
And you should do this by using a for loop.

18
00:01:14,610 --> 00:01:21,990
So you see an example here, so basically what it's doing is in this first example, you ask the user

19
00:01:21,990 --> 00:01:23,340
for a whole number.

20
00:01:23,460 --> 00:01:29,670
The user in this example puts two and then afterwards you ask them for another number.

21
00:01:29,850 --> 00:01:31,140
And the user puts five.

22
00:01:31,140 --> 00:01:35,280
And what it does is it calculates two to the fifth power.

23
00:01:35,580 --> 00:01:36,120
So.

24
00:01:37,060 --> 00:01:40,140
You know, two to the five, and it says that equals three to.

25
00:01:41,700 --> 00:01:43,740
It again, say intro hole, no.

26
00:01:43,770 --> 00:01:49,260
This is two and then seven is the second number, which is the exponent and says two to the seven two

27
00:01:49,260 --> 00:01:50,010
one two eight.

28
00:01:50,040 --> 00:01:53,820
We noticed the same thing down here to the 12 is 496.

29
00:01:54,930 --> 00:01:59,550
And these are significant numbers in computer science.

30
00:01:59,790 --> 00:02:04,290
Of course, your program should take in any numbers that are integers.

31
00:02:05,260 --> 00:02:12,970
And print out the integer result, but powers of two are significant in computer science because of

32
00:02:12,970 --> 00:02:21,150
binary, and when you're thinking about bits and bytes and stuff like that, you know, there there

33
00:02:21,250 --> 00:02:23,510
are definitely significant numbers.

34
00:02:23,530 --> 00:02:31,000
Quite often you will need to count and powers of two and know the results kind of at the top of your

35
00:02:31,000 --> 00:02:37,240
head, like knowing what to to the fifth is is pretty good when you're talking about 32 bit and 64 bit

36
00:02:37,720 --> 00:02:42,040
machines and address bases and architectures and things like that.

37
00:02:42,040 --> 00:02:44,200
So definitely useful.

38
00:02:44,200 --> 00:02:50,230
That's why I put two to this something as an example for these three examples.

39
00:02:52,150 --> 00:02:52,660
OK.

40
00:02:52,840 --> 00:03:01,720
So my challenge for you is to pause the video and try and solve this on your own.

41
00:03:02,590 --> 00:03:06,430
And I will go ahead and go over my solution.

42
00:03:06,430 --> 00:03:07,810
There's multiple ways to do it.

43
00:03:07,810 --> 00:03:12,220
And so if you didn't, don't necessarily do it the way that that I did, that's totally fine.

44
00:03:13,660 --> 00:03:16,990
But the important part is to use a for loop.

45
00:03:18,500 --> 00:03:20,840
So go ahead and pass the video and try and do it right now.

46
00:03:21,230 --> 00:03:27,950
I will give a another hint and let you kind of try it again and then I will show you the answer.

47
00:03:30,570 --> 00:03:40,920
OK, so a slight hint that might help you if you are struggling is the for loop can be used to basically.

48
00:03:42,410 --> 00:03:44,750
Calculate the result.

49
00:03:46,140 --> 00:03:56,910
By using the idea that something to a certain power is basically the first number, of course, multiplied

50
00:03:57,180 --> 00:03:58,530
this many times.

51
00:03:59,970 --> 00:04:08,730
As the second number, so it's basically, you know, two to the fifth is two times, two times, two

52
00:04:08,730 --> 00:04:10,110
times two times two.

53
00:04:10,950 --> 00:04:11,290
Right?

54
00:04:11,850 --> 00:04:18,690
Kind of like an easier example is like two to the third power is eight, which is two times two times

55
00:04:18,690 --> 00:04:19,020
two.

56
00:04:19,940 --> 00:04:25,220
Which, you know, that's like two times two is four, four times two is eight something like that.

57
00:04:25,700 --> 00:04:34,010
So you can use the concept of the loop to basically make this multiplication happen a specific number

58
00:04:34,010 --> 00:04:37,700
of times and then also know that you're going to have to.

59
00:04:38,820 --> 00:04:46,500
Kind of continuously multiply and keep track of a result, right, because you did two times, two is

60
00:04:46,500 --> 00:04:47,040
four.

61
00:04:47,070 --> 00:04:49,140
Now you have the number four and now you have to do.

62
00:04:49,410 --> 00:04:50,730
Times two again.

63
00:04:51,570 --> 00:04:54,210
So you got to take your results so far, which is four.

64
00:04:54,750 --> 00:04:57,450
And then you multiply that times two again, and that's how you get eight.

65
00:04:58,050 --> 00:05:02,940
And let's say you and I have two to the fourth power, you would now want to take that eight and multiply

66
00:05:02,940 --> 00:05:04,170
it times two again.

67
00:05:04,710 --> 00:05:07,770
The fourth loop is used to just do repetition, right?

68
00:05:09,030 --> 00:05:13,770
So you got to think of how to keep track of like a total right for your result.

69
00:05:14,100 --> 00:05:19,110
And then you need to multiply continuously by whatever the first number is.

70
00:05:20,630 --> 00:05:26,030
So that is like my big hint, so if you haven't been able to figure it out yet, go ahead and pass the

71
00:05:26,030 --> 00:05:27,530
video now and try again.

72
00:05:30,150 --> 00:05:38,910
OK, and so now I'm going to exit out of here and go to Visual Studio to go over a result or a solution

73
00:05:38,910 --> 00:05:39,420
to this.

74
00:05:41,680 --> 00:05:45,850
So let me see if I can increase the text size a little bit of this.

75
00:05:46,360 --> 00:05:47,500
This is big enough for you.

76
00:05:49,660 --> 00:05:50,140
Let's see.

77
00:05:51,010 --> 00:05:53,380
Yeah, so I think that's I think that's good enough.

78
00:05:54,850 --> 00:05:58,120
So I just had it already typed out.

79
00:05:58,830 --> 00:06:08,170
Unfortunately, I have a wrist injury right now, so I can't type very fast, and it would be kind of

80
00:06:08,170 --> 00:06:13,510
annoying to watch me chicken peck the keyboard and I really slow.

81
00:06:13,930 --> 00:06:19,690
So I just wanted to type it out previously so you can go over it so you don't have to watch my painfully

82
00:06:19,690 --> 00:06:21,910
slow typing process with one hand.

83
00:06:22,820 --> 00:06:23,210
All right.

84
00:06:23,450 --> 00:06:29,870
So what I did, of course, was start how we always do so far, which is a main function and of course,

85
00:06:29,870 --> 00:06:31,760
including extreme stream.

86
00:06:33,430 --> 00:06:34,210
At the top here.

87
00:06:35,450 --> 00:06:37,700
And I'm using namespace as CD.

88
00:06:38,120 --> 00:06:40,190
You don't have to use that, of course, you know how that goes.

89
00:06:41,370 --> 00:06:45,570
So we declare a first integer, right?

90
00:06:45,690 --> 00:06:48,000
We're going to need two integers, so we should declare those.

91
00:06:49,330 --> 00:06:50,860
So I declare these two integers.

92
00:06:51,100 --> 00:06:56,440
I also go ahead and declare a result that I'm going to keep track of.

93
00:06:56,680 --> 00:06:58,450
To print out right off the bat.

94
00:07:00,540 --> 00:07:03,330
And I don't initialize it yet, but this is totally up to you.

95
00:07:03,360 --> 00:07:07,620
You could do this different when it's you don't have to define it first or sorry, declare first and

96
00:07:07,620 --> 00:07:09,450
then initialize it later, but.

97
00:07:10,960 --> 00:07:11,740
That's how I did it.

98
00:07:11,890 --> 00:07:15,070
So first, I asked the user here to enter a whole number, right?

99
00:07:15,070 --> 00:07:18,610
And then what I do is I read that number into the first integer I want.

100
00:07:19,870 --> 00:07:21,700
Then we asked them to enter an exponent.

101
00:07:23,040 --> 00:07:29,070
And then we read that explanation, and then what I do is the way that I did it and there's multiple

102
00:07:29,070 --> 00:07:36,570
ways you can do this, but I said, OK, well, when you start out in the beginning, basically you

103
00:07:36,570 --> 00:07:42,330
have if you're thinking of the idea of let's let's take two to the three for an example, as a simple

104
00:07:42,330 --> 00:07:46,680
example to the third power you start out with two, right?

105
00:07:48,510 --> 00:07:55,170
And then what you do is you go, you know, two times, two times two.

106
00:07:56,690 --> 00:07:57,150
Right.

107
00:07:57,170 --> 00:07:59,090
So you start out with two.

108
00:08:00,300 --> 00:08:03,990
And then you do times to once in time times do twice.

109
00:08:05,260 --> 00:08:05,530
Right.

110
00:08:07,030 --> 00:08:13,270
So that's kind of what I thought about is that, OK, I'm going to start out with two, for example.

111
00:08:15,380 --> 00:08:16,380
Start out with two.

112
00:08:18,740 --> 00:08:28,340
And then what I was going to do is have a loop that doesn't start at zero, but it starts out at one

113
00:08:28,340 --> 00:08:33,070
because two to the first power is one.

114
00:08:33,080 --> 00:08:38,030
In fact, any number to the first power is that number itself.

115
00:08:38,300 --> 00:08:38,630
Right?

116
00:08:40,430 --> 00:08:46,010
So that's kind of what I started out with as I said, OK, I'm going to start my eye out at one.

117
00:08:47,380 --> 00:08:52,120
And then what I'm going to do is I'm going to go until it's less than.

118
00:08:53,520 --> 00:08:58,710
The second number that was entered, so let's take two to three, for example, so I'm going to start

119
00:08:58,710 --> 00:09:02,190
at one, so we have two to the one and then one I need to do.

120
00:09:03,220 --> 00:09:05,620
Is I basically need to do.

121
00:09:07,520 --> 00:09:11,930
A times two twice, right?

122
00:09:13,130 --> 00:09:17,450
I don't want to do it three times, so that's why I'm doing less than I to.

123
00:09:17,750 --> 00:09:19,880
So I two is three.

124
00:09:21,570 --> 00:09:24,920
And I want to do it twice when it enters the falou.

125
00:09:25,380 --> 00:09:26,790
It's going to multiply it.

126
00:09:28,210 --> 00:09:32,560
This for this first time when I equals one, right, and then it's going to go again and multiply it

127
00:09:32,560 --> 00:09:34,060
again when I equals two.

128
00:09:34,750 --> 00:09:37,420
I only want the multiplication to happen two times, right?

129
00:09:37,870 --> 00:09:40,870
Because I only it's already the result is already too.

130
00:09:41,350 --> 00:09:43,990
So you notice right here I have a times equal.

131
00:09:44,590 --> 00:09:49,540
So this would be the same thing as saying result equals resort times.

132
00:09:49,540 --> 00:09:50,320
I won.

133
00:09:51,570 --> 00:09:51,920
I.

134
00:09:53,390 --> 00:09:58,910
Result equals resort times I want that is basically the same as this, but this is a simplified version

135
00:09:58,910 --> 00:10:01,590
of it using the times equal.

136
00:10:01,610 --> 00:10:02,240
Operator.

137
00:10:04,890 --> 00:10:09,980
So what's going on is that let's take this two to three example here.

138
00:10:10,020 --> 00:10:15,300
So result equals two, let's just say the user entered to as the first number.

139
00:10:16,960 --> 00:10:21,970
And three, as the second number, so we start out with a result is to right.

140
00:10:23,460 --> 00:10:32,460
We start out with I equals one and we say go from eight is one until I is as long as I is less than

141
00:10:32,460 --> 00:10:32,820
three.

142
00:10:34,130 --> 00:10:36,900
Right, so we go in here and we say, OK, resort is too.

143
00:10:37,380 --> 00:10:44,010
Now we're going to say resort is now two times two.

144
00:10:44,790 --> 00:10:45,990
So that's four, right?

145
00:10:46,020 --> 00:10:50,340
So the first time we run this line, we're doing result equals resort times to.

146
00:10:52,620 --> 00:10:58,500
So the first number was two, and we're saying result is now two times two, so it's four.

147
00:10:59,580 --> 00:11:00,570
I was one.

148
00:11:00,570 --> 00:11:01,800
We go back up.

149
00:11:02,490 --> 00:11:04,110
Now I is two.

150
00:11:04,380 --> 00:11:05,130
It's not one.

151
00:11:05,130 --> 00:11:05,700
It's two.

152
00:11:06,750 --> 00:11:12,780
So he is I is too sorry to less than three.

153
00:11:13,290 --> 00:11:16,580
So I is to now and we say, is I less than three?

154
00:11:16,590 --> 00:11:18,720
It is right, so we still go again.

155
00:11:19,470 --> 00:11:22,290
So remember, our result is four, that's what we did last time.

156
00:11:22,290 --> 00:11:27,960
So now we're seeing a result actually equals four times to.

157
00:11:30,040 --> 00:11:31,870
So four times two is eight.

158
00:11:32,650 --> 00:11:33,010
Right?

159
00:11:34,360 --> 00:11:40,540
So now we store eight in resort and then we go back up and now is going to be three.

160
00:11:40,720 --> 00:11:42,850
And we say is three less than three.

161
00:11:42,970 --> 00:11:45,160
No three is not less than three, it's equal to three.

162
00:11:45,160 --> 00:11:45,940
So we stop.

163
00:11:46,770 --> 00:11:47,020
Right.

164
00:11:47,050 --> 00:11:53,380
And then we go down here, you print out what our first number was raised to the second no power.

165
00:11:54,630 --> 00:11:59,790
And then we say and this, you know, this is just printing that out is basically saying like, you

166
00:11:59,790 --> 00:12:04,410
know, first no, and then using the carrot symbol and then the second number that they entered to just

167
00:12:04,410 --> 00:12:08,250
show what the result is that we say equal result is eight.

168
00:12:08,250 --> 00:12:13,110
It has the correct result for a two to the third power and then we do it in line.

169
00:12:16,420 --> 00:12:16,810
So.

170
00:12:21,930 --> 00:12:23,850
I think that I am going to.

171
00:12:25,220 --> 00:12:31,700
Go ahead and try and tape this so I can run it again.

172
00:12:33,260 --> 00:12:38,850
OK, so two and then three, OK, we noticed that it's eight, right?

173
00:12:38,870 --> 00:12:42,020
Just to show you that I wasn't messing with you.

174
00:12:42,470 --> 00:12:46,700
That's really what they were sort of two of their powers now on to walk through how I did it.

175
00:12:46,700 --> 00:12:48,620
Another way that you could have done this.

176
00:12:49,220 --> 00:12:53,540
Actually, there's a there's probably multiple ways like a decent amount of ways, a handful of ways,

177
00:12:54,500 --> 00:12:58,190
but you can also think of it as starting at zero.

178
00:13:00,060 --> 00:13:08,070
If you wanted to, you could say start with I equals zero and I'm going to start at Russell equals one

179
00:13:09,090 --> 00:13:10,290
because two.

180
00:13:10,530 --> 00:13:15,960
Sorry, I can't just to any number to the zero power is going to be one, right?

181
00:13:17,830 --> 00:13:20,560
So that's just a mathematical concept.

182
00:13:20,830 --> 00:13:26,080
And if we wanted, we could actually start at zero if we want to with a loop.

183
00:13:26,860 --> 00:13:31,840
But then what we should do is we should say, OK, if we're talking about a zero exponent, we should

184
00:13:31,840 --> 00:13:38,380
be then considering to restart our result at one because let's take two, for example, again two to

185
00:13:38,380 --> 00:13:39,640
the three power again.

186
00:13:41,530 --> 00:13:44,440
Two to the zero is going to be one.

187
00:13:44,610 --> 00:13:48,430
Right, so if we're going to start that, we want to make sure that it's actually starting at one and

188
00:13:48,430 --> 00:13:55,840
then if we're going to go again, then we can basically do, you know, one times two, and that would

189
00:13:56,170 --> 00:13:57,610
bring us to two to the one.

190
00:13:58,330 --> 00:14:04,090
And then, you know, times two again would bring us two squared, which is four, right?

191
00:14:04,780 --> 00:14:08,200
So I can actually save this.

192
00:14:09,520 --> 00:14:11,890
I will try to compile this.

193
00:14:15,280 --> 00:14:15,730
All right.

194
00:14:15,730 --> 00:14:18,610
So let's try and do this again just to show that it works.

195
00:14:20,420 --> 00:14:21,500
So I do, too.

196
00:14:21,830 --> 00:14:25,780
I'm with you three and we get the same result here.

197
00:14:28,000 --> 00:14:28,330
Right.

198
00:14:31,500 --> 00:14:31,860
Cool.

199
00:14:32,370 --> 00:14:37,270
So that is the solution to the first problem you could do either of these two ways or you could do.

200
00:14:37,320 --> 00:14:39,600
Maybe you did differently and that's great.

201
00:14:39,870 --> 00:14:46,950
As long as it works in good job, I'm glad that you were able to figure it out if you couldn't figure

202
00:14:46,950 --> 00:14:47,270
it out.

203
00:14:47,370 --> 00:14:48,240
Don't worry.

204
00:14:48,450 --> 00:14:49,530
Keep practicing.

205
00:14:50,310 --> 00:14:56,940
It's just a matter of drilling this until you get it down and understand how the for loop works better.

206
00:14:58,540 --> 00:15:04,870
OK, so let's go ahead and head back to our presentation here.

207
00:15:06,490 --> 00:15:08,350
So now we're going to move on to the second problem.

208
00:15:10,150 --> 00:15:12,460
So this second problem?

209
00:15:13,060 --> 00:15:14,380
A pretty simple description.

210
00:15:16,320 --> 00:15:23,430
But it says to make a program that takes a word and this word is going to be of the data type string.

211
00:15:23,580 --> 00:15:25,230
So you're going to make a string.

212
00:15:25,590 --> 00:15:31,050
But it specifically says word because we're not talking about a sentence like we're not talking about

213
00:15:31,050 --> 00:15:33,390
multiple words separated by spaces.

214
00:15:34,020 --> 00:15:37,590
In the example, here you notice that it's just single words.

215
00:15:37,800 --> 00:15:43,140
And what you do is you take that word from the user, which is a string and print that word in reverse.

216
00:15:45,410 --> 00:15:48,620
So this one is kind of a much more simple idea.

217
00:15:49,750 --> 00:15:51,250
Not having to do as much work.

218
00:15:52,470 --> 00:16:00,390
But it may be a little confusing because we haven't really looked at an example that does this style

219
00:16:00,390 --> 00:16:02,640
of thing with a photo loop, I have mentioned it.

220
00:16:04,040 --> 00:16:07,310
But we haven't really done something like this yet.

221
00:16:08,480 --> 00:16:15,260
So just like the last one, I will encourage you to solve it on your own without me giving any hints.

222
00:16:15,770 --> 00:16:19,100
Then I will give a hint and you can try it again if you haven't solved it.

223
00:16:19,790 --> 00:16:23,660
And then if you haven't solved it by then, you can look at the solution that I will go over.

224
00:16:25,870 --> 00:16:26,320
OK.

225
00:16:26,470 --> 00:16:28,300
So, yeah, it's pretty simple this, isn't it.

226
00:16:28,330 --> 00:16:29,630
A couple of examples here.

227
00:16:29,650 --> 00:16:37,630
So in this example, the user entered A-word programming and then it prints out reversed word equals

228
00:16:37,630 --> 00:16:41,620
and then it shows the prints out the word in reverse here.

229
00:16:43,670 --> 00:16:43,980
Right.

230
00:16:45,300 --> 00:16:47,670
So note that I am not.

231
00:16:49,290 --> 00:17:00,210
Saying that you need to reverse the string so you don't necessarily have to reverse the string and then

232
00:17:00,210 --> 00:17:06,630
print the string out, although you can do that, if you would like to do that as a challenge, you

233
00:17:06,630 --> 00:17:07,890
can definitely do that.

234
00:17:08,640 --> 00:17:16,470
But all I'm really saying is that you need to print out the word in reverse, right?

235
00:17:18,270 --> 00:17:23,310
So just kind of remember that if you want to reverse the string, you can go ahead, but you don't have

236
00:17:23,310 --> 00:17:23,730
to do that.

237
00:17:23,730 --> 00:17:26,010
You can just print the word in reverse.

238
00:17:27,840 --> 00:17:28,530
So you notice.

239
00:17:28,530 --> 00:17:28,820
Yeah.

240
00:17:28,860 --> 00:17:29,930
Here's one example.

241
00:17:29,940 --> 00:17:33,300
Here's another example of software so pretty straightforward.

242
00:17:33,540 --> 00:17:35,790
All right, so go ahead and of video.

243
00:17:35,790 --> 00:17:40,230
I encourage you to try and solve it right now without any hints or help.

244
00:17:44,250 --> 00:17:50,190
OK, so if you weren't able to solve it, I'm going to give a hint now.

245
00:17:50,760 --> 00:17:58,710
So something that you can do for this to print it out in reverse is, of course you should be using

246
00:17:58,710 --> 00:17:59,520
a for loop.

247
00:18:00,620 --> 00:18:01,250
But.

248
00:18:02,290 --> 00:18:12,850
What you can do is instead of looping forward with incrementing with the A-plus plus, you can actually

249
00:18:12,850 --> 00:18:19,570
loop backwards with decremental with the decrement operator, which is I minus minus.

250
00:18:20,170 --> 00:18:32,860
So rather than starting at zero and going up to the bike length of the string minus one, you can actually

251
00:18:32,860 --> 00:18:33,670
reverse that.

252
00:18:33,670 --> 00:18:38,200
You can actually start at the end of the string, which is the length of the string minus one, and

253
00:18:38,200 --> 00:18:43,840
then you can go backwards until you reach the zero position.

254
00:18:45,700 --> 00:18:49,510
All right, so that's a hint, so if you have, you weren't able to solve it.

255
00:18:52,810 --> 00:18:57,340
In the beginning, go ahead and try that video and try to solve it again, and then I will go over the

256
00:18:57,340 --> 00:18:57,850
solution.

257
00:19:00,520 --> 00:19:05,410
OK, so now I'm going to go over the solution or at least one of the potential solutions.

258
00:19:07,100 --> 00:19:11,940
So I have these kind of flipped this says practice to but, you know, it should have.

259
00:19:12,260 --> 00:19:13,500
It should have said practice one.

260
00:19:13,820 --> 00:19:15,110
But this is what we're doing now.

261
00:19:17,570 --> 00:19:20,990
So once again, we, of course, start out with our basic stuff here.

262
00:19:21,080 --> 00:19:27,050
And you may or may not need to include a string.

263
00:19:28,660 --> 00:19:31,940
You might need to include string up here as well.

264
00:19:31,960 --> 00:19:36,010
In addition to that, maybe it's dependent on C++ version.

265
00:19:36,020 --> 00:19:37,030
I'm not a hundred percent sure.

266
00:19:37,030 --> 00:19:42,250
Actually, I didn't need to use it because of whatever version I'm using, but I know that string gives

267
00:19:42,250 --> 00:19:47,920
you access to some of these member functions that you can call on the string.

268
00:19:48,610 --> 00:19:54,790
So that's why you if it's not working, if it if you get an error where it doesn't understand a dot

269
00:19:54,820 --> 00:19:55,270
length.

270
00:19:56,450 --> 00:20:03,020
You're having a problem with that, then you might need to go up here and do a hashtag include string

271
00:20:03,320 --> 00:20:03,770
as well.

272
00:20:05,170 --> 00:20:09,520
Otherwise, don't worry about it, so we have our main function, right?

273
00:20:09,850 --> 00:20:14,560
And then I declare a variable to read into right.

274
00:20:14,590 --> 00:20:17,050
I'm going to make this variable here.

275
00:20:17,050 --> 00:20:22,690
I called it word and then we print out to the user that they should enter a word.

276
00:20:23,770 --> 00:20:26,260
Then we read into that word we're seeing.

277
00:20:28,530 --> 00:20:35,400
Then what we do is we put we print out reverse word equals because we're about to print out the result,

278
00:20:35,430 --> 00:20:35,760
right?

279
00:20:36,180 --> 00:20:41,580
So we're going to tell the user, OK, I'm about to print out the result because afterwards we're going

280
00:20:41,580 --> 00:20:42,750
to put the actual result.

281
00:20:44,010 --> 00:20:46,380
And this is how I went about it.

282
00:20:46,380 --> 00:20:55,620
So I did it for loop and I said, and I is equal to you, word length minus one.

283
00:20:56,040 --> 00:20:59,700
So I save where dot length minus one into I.

284
00:21:00,000 --> 00:21:03,900
This is the last position of the string, right?

285
00:21:04,320 --> 00:21:05,970
We take the size of the string.

286
00:21:05,970 --> 00:21:06,930
How long is it?

287
00:21:07,530 --> 00:21:12,430
And then since the indexing starts at zero, we need to do that minus one, right?

288
00:21:12,450 --> 00:21:15,150
That'll be the last position of the string.

289
00:21:15,160 --> 00:21:15,540
So.

290
00:21:16,720 --> 00:21:19,930
If I entered programming, it would be at the last G.

291
00:21:20,050 --> 00:21:20,440
Right?

292
00:21:21,900 --> 00:21:26,290
So then what we say is how long are we going to loop for, what is this condition based on?

293
00:21:26,310 --> 00:21:30,060
Well, we're going to loop as long as I is greater than.

294
00:21:31,740 --> 00:21:34,110
Or equal to zero.

295
00:21:34,650 --> 00:21:36,570
And we put in or equal to.

296
00:21:38,000 --> 00:21:46,460
Because we still want to print out the zeroth character, right, these zero position character, so

297
00:21:46,550 --> 00:21:52,010
programming as an example, again, the programming word P would be the first character.

298
00:21:52,010 --> 00:21:53,630
That's it positions zero.

299
00:21:53,780 --> 00:21:54,170
Right?

300
00:21:54,320 --> 00:21:59,480
So we started G, which is the last character of programming, and we're going to want to print that

301
00:21:59,720 --> 00:22:06,530
out and then we're going to want to keep going as long as it's greater than or equal to zero.

302
00:22:06,530 --> 00:22:14,030
So we will keep subtracting and we'll print out the word until we get to the P and we'll go ahead and

303
00:22:14,030 --> 00:22:17,570
print out the very first character, which is P, that's in the zero position.

304
00:22:17,570 --> 00:22:21,710
And then we're done right because it'll go into the negatives after that.

305
00:22:22,280 --> 00:22:25,610
So we use a decrement operator because we're going backwards.

306
00:22:25,610 --> 00:22:32,270
We start at the end of the word, the last character we go up and tell the first character.

307
00:22:33,140 --> 00:22:34,700
So it's kind of more like down.

308
00:22:34,700 --> 00:22:37,580
I guess we're going down until the first character.

309
00:22:38,570 --> 00:22:46,760
And we're doing that backwards subtraction, subtraction of the position, which is I by doing a minus

310
00:22:46,760 --> 00:22:49,130
minus, this is as decrement operator.

311
00:22:50,060 --> 00:22:54,440
And so each time we're printing out whatever position we're on, right?

312
00:22:56,080 --> 00:23:02,110
So let's take a very simple word for, except for an example, so how about the word car?

313
00:23:02,320 --> 00:23:03,550
So it's c.a.r..

314
00:23:04,540 --> 00:23:07,600
So the user enters the word car.

315
00:23:09,280 --> 00:23:12,220
Actually, what we do first is we say, hey, enter a word, please.

316
00:23:12,490 --> 00:23:17,020
They enter a car that gets red into this string variable word.

317
00:23:17,440 --> 00:23:21,160
So now the string variable where it holds the word car.

318
00:23:22,700 --> 00:23:26,630
Then we say, OK, reverse word equals we print that out.

319
00:23:27,770 --> 00:23:30,920
And now we're going to go and try and calculate the result and print it out.

320
00:23:32,360 --> 00:23:33,830
So we start this for loop.

321
00:23:34,550 --> 00:23:39,320
I is going to be equal to word length minus one.

322
00:23:39,590 --> 00:23:41,900
What is word length if it's car?

323
00:23:43,110 --> 00:23:47,880
Well, it's three characters, long, right, c.a.r. one to three.

324
00:23:48,280 --> 00:23:49,620
So three characters long.

325
00:23:50,250 --> 00:23:51,850
But then we say minus one.

326
00:23:51,870 --> 00:23:54,630
So instead of three, it's going to be two.

327
00:23:54,660 --> 00:23:55,460
And why is that?

328
00:23:55,470 --> 00:23:59,660
Because I'm going to try and type really badly here at risk.

329
00:23:59,760 --> 00:24:05,320
But see, is it position zero, right?

330
00:24:05,970 --> 00:24:08,490
A Is it position one, right?

331
00:24:08,820 --> 00:24:10,740
Ah, is it position two?

332
00:24:11,970 --> 00:24:17,280
So we did a three minus one is two.

333
00:24:18,030 --> 00:24:19,290
This is a position two.

334
00:24:19,290 --> 00:24:21,450
This is where we want to start is on the R.

335
00:24:21,690 --> 00:24:22,690
It's a position two.

336
00:24:22,920 --> 00:24:26,040
We want to go until position zero, which is the sea, right?

337
00:24:27,850 --> 00:24:33,070
So then we say, OK, is I greater than or equal to zero?

338
00:24:33,100 --> 00:24:35,590
Yes, two is greater than or equal to zero.

339
00:24:36,620 --> 00:24:39,710
So we're going to go down into here.

340
00:24:41,720 --> 00:24:43,130
And we're going to start out on.

341
00:24:43,850 --> 00:24:45,950
And so what we're going to do is we're going to print out.

342
00:24:47,050 --> 00:24:49,840
What is that word position to?

343
00:24:50,840 --> 00:24:57,470
Well, ah, is it we're in position to right, we have two zero one two position, two is ah.

344
00:24:57,620 --> 00:24:58,910
So we print out our.

345
00:25:00,430 --> 00:25:02,080
We go back to the top of the loop.

346
00:25:02,770 --> 00:25:11,260
We do a minus minus, right, so now we're at position one, which is this a we basically say is one

347
00:25:11,260 --> 00:25:12,460
greater than or equal to zero?

348
00:25:12,490 --> 00:25:13,060
Yes, it is.

349
00:25:13,070 --> 00:25:19,630
So we go in here and we say print out word position one.

350
00:25:20,760 --> 00:25:23,070
We're in position one is here, right?

351
00:25:23,130 --> 00:25:28,590
This is weird, position one is this middle one right here because this is two, this is one, this

352
00:25:28,590 --> 00:25:29,130
is zero.

353
00:25:29,580 --> 00:25:38,310
So we point out that a so now we've printed out in our first this is what's printing out are and now

354
00:25:38,310 --> 00:25:38,820
in a.

355
00:25:42,130 --> 00:25:49,240
And then what we do is we go back to the top and now we subtract one from one, and that gives us zero.

356
00:25:49,270 --> 00:25:56,950
And we say OK is zero greater than or equal to zero zero is not greater then, but it is equal to so

357
00:25:57,070 --> 00:26:01,720
greater or equal to or greater than or equal to operator.

358
00:26:02,590 --> 00:26:07,120
So it is equal to so this this condition is still satisfied.

359
00:26:07,120 --> 00:26:08,410
We still go down in here.

360
00:26:09,190 --> 00:26:12,670
We say, let's print out word at position zero.

361
00:26:13,620 --> 00:26:17,160
We're in position zero, is this character right here, see, right?

362
00:26:17,180 --> 00:26:21,180
We were here at one, we subtracted one and went left one.

363
00:26:21,960 --> 00:26:25,410
And now we're at position zero, which is, see, so we print out.

364
00:26:25,410 --> 00:26:26,010
See?

365
00:26:26,820 --> 00:26:27,170
All right.

366
00:26:27,180 --> 00:26:28,410
So we print out this.

367
00:26:29,690 --> 00:26:35,060
And in fact, there's no spaces, I just put the spaces here, so you could visibly see it better as

368
00:26:35,060 --> 00:26:36,830
I was showing you the positions.

369
00:26:37,790 --> 00:26:39,860
But the result is going to be like this, right?

370
00:26:42,960 --> 00:26:48,890
So let's go ahead and prove this, and then I print out a little in line just for accurate kind of.

371
00:26:50,580 --> 00:26:55,500
So I'm going to save, I'm actually just going to stay right here so you can see that I clicked and

372
00:26:55,500 --> 00:27:09,390
saved and I am going to do this is going to be hard to Typekit C++ f l crack test one

373
00:27:11,880 --> 00:27:20,670
o f l p one c so i compile that.

374
00:27:25,590 --> 00:27:34,170
OK, and I'm going to run this into a word, and I'm going to tape car and I press enter and you notice

375
00:27:34,170 --> 00:27:38,580
that it reverses it and does this are a see just like we expected.

376
00:27:41,000 --> 00:27:47,930
OK, so hopefully that wasn't too crazy, difficult it might have been because.

377
00:27:49,460 --> 00:27:55,820
You haven't haven't really showed you an example using the minus minus, but really important to know

378
00:27:55,820 --> 00:27:58,640
that you're not limited to just doing this.

379
00:27:59,180 --> 00:28:00,950
I and II equals zero.

380
00:28:01,160 --> 00:28:04,250
I less than something I.

381
00:28:04,280 --> 00:28:07,160
Plus plus you don't have to do that all the time.

382
00:28:07,190 --> 00:28:08,750
That's not what a for loop is.

383
00:28:08,750 --> 00:28:11,270
A for loop is really open to interpretation.

384
00:28:11,600 --> 00:28:17,900
You can put whatever for this value and you can put whatever condition and you can put like a bunch

385
00:28:17,900 --> 00:28:19,940
of different operators here.

386
00:28:21,460 --> 00:28:23,950
For the incrementing and decorating and stuff.

387
00:28:25,330 --> 00:28:30,460
So just know that you could put like, you know, word lengths like plus 10, you could just put like

388
00:28:30,460 --> 00:28:36,910
the constant one, you can put the number one thousand here, you can put some other variable here.

389
00:28:37,600 --> 00:28:37,960
Right.

390
00:28:38,560 --> 00:28:40,820
This could be like equals equals.

391
00:28:40,870 --> 00:28:42,550
This could be less than or equal to.

392
00:28:42,550 --> 00:28:43,980
This can be greater than an equal to.

393
00:28:43,990 --> 00:28:48,850
It could be less than it could be greater than it could be like not equal to.

394
00:28:49,540 --> 00:28:53,290
You can put all kinds of stuff here and then here, you know, this could be like.

395
00:28:54,390 --> 00:28:58,860
All kinds of different things as far as how much you're incriminating, I.

396
00:28:59,160 --> 00:29:03,650
Each time or not, you maybe you're decorating by one, maybe you're incriminating by 10, you know?

397
00:29:05,060 --> 00:29:08,180
Multiplying by two, you can do you do doing all kinds of stuff here.

398
00:29:09,610 --> 00:29:16,900
So it's important to understand that you can customize these four loops to move forward and backward

399
00:29:16,900 --> 00:29:23,970
through some sort of string or an array or a for loop doesn't have to be just going through an array.

400
00:29:23,980 --> 00:29:30,160
You notice that it can be used for counting and stuff like that, too, as long as you know a specific

401
00:29:30,160 --> 00:29:37,870
number or the user's going to enter something that can be translated to a number of times that a loop

402
00:29:37,870 --> 00:29:38,530
can run.

403
00:29:38,710 --> 00:29:39,970
That's when you should use a for.

404
00:29:41,140 --> 00:29:41,450
Right.

405
00:29:42,170 --> 00:29:47,600
In some point in your program, if you're going to be able to extract some value or know some value

406
00:29:47,600 --> 00:29:49,120
or use some value as a.

407
00:29:50,150 --> 00:29:53,630
Can be used as a number of times for a loop to iterate.

408
00:29:54,170 --> 00:29:57,920
Then you can build a for loop with that in mind here.

409
00:29:58,640 --> 00:30:00,900
And once again, you don't have to use AI.

410
00:30:00,920 --> 00:30:03,260
You can use whatever variable makes sense.

411
00:30:03,800 --> 00:30:07,310
It's commonplace to use AI a lot of times if you just have one Lou.

412
00:30:08,150 --> 00:30:14,420
It's just kind of like a programming thing that kind of caught on, you know, like an iteration or

413
00:30:14,420 --> 00:30:15,950
something like that, but you don't have to you.

414
00:30:17,090 --> 00:30:17,580
OK?

415
00:30:17,780 --> 00:30:21,560
So hopefully this helped you understand loops a little bit better.

416
00:30:22,700 --> 00:30:28,160
Of course, looking at more problems online and also coming up with your own practice problems is great

417
00:30:28,160 --> 00:30:28,690
as well.

418
00:30:29,690 --> 00:30:34,260
But you don't have to worry too much because we're going to be using for loops a lot.

419
00:30:34,280 --> 00:30:42,020
You're going to notice that a ton of problems that we solve are going to be using for loops and while

420
00:30:42,020 --> 00:30:42,740
loops as well.

421
00:30:42,740 --> 00:30:46,150
But a lot, a lot of loops, definitely a lot of for loops.

422
00:30:47,170 --> 00:30:52,090
They're huge for algorithms, and we're going to be doing a lot of algorithms in the second part of

423
00:30:52,090 --> 00:30:55,630
the course, even in the this first section, of course, as well.

424
00:30:57,650 --> 00:31:03,940
OK, so with that, I will see you in the next lecture where we will start getting into some new stuff,

425
00:31:03,950 --> 00:31:06,800
we're going to leave loops kind of behind.

426
00:31:06,810 --> 00:31:10,940
We'll still keep using them, but we're not going to explicitly be focusing on them.

427
00:31:11,360 --> 00:31:16,880
We're going to introduce some new concepts and try and use the loops with those new concepts as well.

428
00:31:18,230 --> 00:31:18,520
All right.

429
00:31:18,530 --> 00:31:20,030
So with that, I'll see you in the next lecture.
