1
00:00:00,450 --> 00:00:10,680
So today we are going to talk about arithmetic operators, so we've gone over the logical operators.

2
00:00:11,040 --> 00:00:18,810
Now it's time to talk about some of the operators that have a little bit more to do with standard math,

3
00:00:19,020 --> 00:00:24,450
things like addition multiplication division, stuff like that.

4
00:00:24,450 --> 00:00:30,060
And we'll notice there's actually some, some more operators besides just those standard ones that I've

5
00:00:30,060 --> 00:00:30,810
just mentioned.

6
00:00:32,220 --> 00:00:40,230
So I went ahead and took the liberty of making a new file here called arithmetic operators to keep you

7
00:00:40,230 --> 00:00:40,580
notice.

8
00:00:40,590 --> 00:00:44,070
I'm quite lazy and I'm still putting it in this first program directory.

9
00:00:44,370 --> 00:00:49,590
But of course, it's a good idea for you to organize your files in a way that is easy for you to go

10
00:00:49,590 --> 00:00:51,990
back and look up things for reference.

11
00:00:52,380 --> 00:00:55,770
So I just put it in here, but you can make it wherever you want.

12
00:00:56,250 --> 00:00:58,650
I also went ahead and pulled up another terminal.

13
00:00:58,680 --> 00:01:02,610
I'm doing it in Visual Studio, so you can see again today.

14
00:01:04,110 --> 00:01:08,100
So you can just pull the terminal up as well so we can go ahead and compile and run our stuff.

15
00:01:08,520 --> 00:01:14,310
I have already made a main function in the skeleton with some comments, and you might be wondering,

16
00:01:14,490 --> 00:01:17,400
Hmm, I haven't seen this before.

17
00:01:17,470 --> 00:01:18,530
Up here.

18
00:01:18,540 --> 00:01:19,710
What's with this?

19
00:01:20,460 --> 00:01:25,380
So I'm just going to kind of sprinkle in some random things to the lectures that aren't having their

20
00:01:25,380 --> 00:01:26,780
own dedicated topics.

21
00:01:26,790 --> 00:01:31,800
You noticed that I already mentioned something about comments you might have seen that mentioned in

22
00:01:31,800 --> 00:01:39,330
other videos as well, but we talked about a comment on a single line, which is a slash slash before

23
00:01:39,330 --> 00:01:39,510
it.

24
00:01:40,380 --> 00:01:43,470
This is the same thing, except it's a multiple line comment.

25
00:01:43,470 --> 00:01:49,950
So it's kind of nice sometimes to put a description for your entire program at the top.

26
00:01:50,520 --> 00:01:58,980
Or if you needed to describe another big chunk of code, you might want to put a block comment, which

27
00:01:58,980 --> 00:01:59,850
is something else.

28
00:01:59,850 --> 00:02:01,980
You can call this or a multi-line comment.

29
00:02:02,190 --> 00:02:06,120
The way to do that is to put a slash and then a star to open the comment.

30
00:02:06,690 --> 00:02:14,790
Then you can type as many lines as you'd like and you close it with a star and then a slash so you could

31
00:02:14,790 --> 00:02:18,810
add more lines if you hear if you wanted to another line.

32
00:02:19,440 --> 00:02:24,660
You know this if I get rid of this, you notice it just goes all the way down to here.

33
00:02:24,660 --> 00:02:30,120
It sees this star slash that they put right here, and it considers these comments just because they

34
00:02:30,120 --> 00:02:32,340
have slash slash is already in front of them.

35
00:02:33,510 --> 00:02:35,340
So you got to make sure to close it.

36
00:02:37,500 --> 00:02:40,260
But yeah, just something else I wanted to mention.

37
00:02:40,260 --> 00:02:44,160
So you don't have to have these on a new line, either.

38
00:02:44,160 --> 00:02:47,010
I can back this up right on the same line.

39
00:02:47,490 --> 00:02:49,800
I can also work this out right here.

40
00:02:50,070 --> 00:02:51,180
You can put it like that.

41
00:02:51,600 --> 00:02:57,750
However, you would like it to look as long as there is an open slash star and a closed star slash.

42
00:02:59,550 --> 00:03:01,050
OK, so pretty cool.

43
00:03:01,780 --> 00:03:05,700
You know, I'm also including, I assume, because of course, we're going to be using that as always

44
00:03:05,700 --> 00:03:07,500
to print out stuff today.

45
00:03:07,520 --> 00:03:13,310
I'm going to go ahead and not do the using namespace skinny thing.

46
00:03:13,320 --> 00:03:22,350
I'm actually going to use the study for the standard namespace in the colon colon scope resolution operator

47
00:03:22,800 --> 00:03:26,670
to put before see out and before in line.

48
00:03:27,390 --> 00:03:32,490
Just because I'm going to mix it up, we'll be using it sometimes or sometimes not be using it.

49
00:03:34,220 --> 00:03:40,880
Like I said, I don't think it's like a horrible thing to use it, like you should never use it ever.

50
00:03:42,200 --> 00:03:48,050
You know, it's put there for a reason, it's possible to do it for a reason, so, you know, maybe

51
00:03:48,050 --> 00:03:53,480
not a good reason, but you can use it if you don't have to worry about some near-space collisions,

52
00:03:53,480 --> 00:03:55,040
that's something we'll get into later.

53
00:03:55,370 --> 00:04:01,670
But it's good programming practice to not use it and instead do this, which you just have to put this

54
00:04:01,670 --> 00:04:05,180
before anything that you would use inside of extreme.

55
00:04:05,210 --> 00:04:12,120
Something extreme is coming from the standard template library and C++, so it gives us access to things

56
00:04:12,120 --> 00:04:13,850
like see out and in.

57
00:04:14,270 --> 00:04:16,910
And so the difference is is you're going to see this.

58
00:04:18,190 --> 00:04:24,730
Rather than just this, and you will see this rather than just this.

59
00:04:25,720 --> 00:04:27,820
That's the only difference today, at least.

60
00:04:28,990 --> 00:04:30,840
OK, so with that, let's go ahead and get started.

61
00:04:30,860 --> 00:04:34,870
Make sure you have a main function here with the brackets.

62
00:04:34,870 --> 00:04:39,280
And then I have a lot of comments in here that just talk about what we're going to get into and discuss.

63
00:04:40,390 --> 00:04:42,250
So first off list, declare some variables.

64
00:04:42,850 --> 00:04:43,930
I'm going to leave this up to you.

65
00:04:43,940 --> 00:04:48,910
So why don't you pause the video and come up with an integer of float and a double variable?

66
00:04:51,490 --> 00:04:53,290
So hopefully you went ahead did that.

67
00:04:54,010 --> 00:04:59,290
I want you to get some practice and remember how to do that because we have discussed that already.

68
00:04:59,920 --> 00:05:04,720
So if you're still a little wobbly, you can go ahead and just do something like this.

69
00:05:06,220 --> 00:05:11,500
But whatever value you want, I'm just going to say this is five an integer called i1 that has the value.

70
00:05:11,500 --> 00:05:11,890
Five.

71
00:05:12,520 --> 00:05:23,620
I'm going to say a float and I'll call this f one equals ten point five, and I'm going to say a double

72
00:05:23,620 --> 00:05:24,340
b one.

73
00:05:24,520 --> 00:05:29,680
And I want to say this is fifteen point twenty five.

74
00:05:31,930 --> 00:05:32,390
Cool.

75
00:05:32,410 --> 00:05:34,180
So now we have some variables.

76
00:05:34,390 --> 00:05:39,340
So the first thing that we're going to go over is the basic operators.

77
00:05:41,260 --> 00:05:49,420
So these are things that you've probably seen before in math, this asterisk here is for multiplication.

78
00:05:50,560 --> 00:05:56,830
This plus is for addition, the minus is for subtraction.

79
00:05:57,610 --> 00:05:59,740
The slash is for division.

80
00:06:00,340 --> 00:06:03,460
And this you may or may not have seen you.

81
00:06:03,670 --> 00:06:11,290
Some of you might know this as the percentage sign, but it also has in programming.

82
00:06:11,290 --> 00:06:15,490
We refer to it as the modulo or modulus.

83
00:06:16,120 --> 00:06:23,740
So this just means remainder, and you might have already used modulo or modulus in mathematics.

84
00:06:23,750 --> 00:06:31,120
If you've taken a math class that deals with that stuff, maybe like discrete math or a number theory

85
00:06:31,120 --> 00:06:32,200
or something like that.

86
00:06:32,860 --> 00:06:37,570
But if you haven't, don't worry, I will explain what its purpose is and how to use it.

87
00:06:39,370 --> 00:06:45,100
So with that, let's go ahead and print out some mathematical things, like some operations.

88
00:06:45,790 --> 00:06:52,270
So I'm going to do ask Colin and call Remember and then ask out and I'm going to do.

89
00:06:52,300 --> 00:06:54,940
Let's just do something simple, so I'm going to do I one.

90
00:06:55,450 --> 00:06:58,030
That's our variable that has the integer five.

91
00:06:58,600 --> 00:06:59,380
And I'm going to do.

92
00:06:59,470 --> 00:07:01,660
Plus I won.

93
00:07:03,000 --> 00:07:11,040
Actually, I do have Class One to call, and then here I'm going to do an SUV and in line.

94
00:07:11,850 --> 00:07:14,850
So let's go ahead and print this out before we do that.

95
00:07:14,880 --> 00:07:17,430
Tell me what you think this result is going to be.

96
00:07:17,610 --> 00:07:24,570
Of course, I can't hear you say that, but just think about what it would be on your own before it

97
00:07:24,570 --> 00:07:25,440
gets printed out.

98
00:07:27,060 --> 00:07:35,820
So I'm going to save this and I'm going to actually meet all the D Plus plus I, so I'm in the directory.

99
00:07:35,820 --> 00:07:42,090
If you need a change to your directory, go ahead and change to the directory that has your file in

100
00:07:42,090 --> 00:07:42,260
it.

101
00:07:42,270 --> 00:07:42,910
That would just be.

102
00:07:42,930 --> 00:07:44,880
Make sure to follow this path up here.

103
00:07:47,170 --> 00:07:54,790
So I'm going to do G-force plus arithmetic operations to repeat that, oh, to name this swimmer, to

104
00:07:54,790 --> 00:07:58,870
name my alpha file and I'm going to call this just areas that you see.

105
00:08:01,070 --> 00:08:08,020
OK, now I can run it, so I'm going to do that slash Anthony ACSI.

106
00:08:10,090 --> 00:08:20,210
And we see in 11 here, so what happened was we had a I one was five plus one is five, so that's 10.

107
00:08:21,010 --> 00:08:24,190
And then we added one to that, which gave us 11.

108
00:08:24,850 --> 00:08:27,040
So that's why 11 printed out.

109
00:08:27,040 --> 00:08:29,650
So it actually printed out the result of all this.

110
00:08:32,170 --> 00:08:32,450
Cool.

111
00:08:32,470 --> 00:08:33,910
So let's try something else.

112
00:08:34,930 --> 00:08:41,890
We'll do another line this time we'll do some multiplication, so why don't we do F1 times to

113
00:08:44,740 --> 00:08:46,390
put that SUV phone call in?

114
00:08:50,350 --> 00:08:54,190
And so, yeah, let's go ahead and save this and run it.

115
00:08:57,690 --> 00:09:00,480
So I'm going to compile this and run it.

116
00:09:01,290 --> 00:09:10,290
And we noticed that we get twenty one, so this is a ten point five times too.

117
00:09:10,860 --> 00:09:14,160
So, you know, ten times to twenty point five times two is one.

118
00:09:14,160 --> 00:09:15,900
We have twenty one here for our result.

119
00:09:17,940 --> 00:09:20,880
Let's go ahead and do some divisions.

120
00:09:20,880 --> 00:09:25,080
So this time I'm actually going to throw in some

121
00:09:27,390 --> 00:09:30,000
parentheses just for the heck of it.

122
00:09:30,010 --> 00:09:35,730
So remember, the parentheses are things that you can actually use to separate a lot of stuff.

123
00:09:36,360 --> 00:09:45,030
So in math, you might have used like parentheses when you talked about the order of operations, which

124
00:09:45,030 --> 00:09:46,530
is what we will do next.

125
00:09:46,530 --> 00:09:47,760
We'll talk about the.

126
00:09:48,690 --> 00:09:54,240
Operator precedence, which is order of operations, but you and Matthew can sometimes separate things

127
00:09:54,240 --> 00:09:55,280
with parentheses, right?

128
00:09:55,300 --> 00:09:58,830
If you want something to happen first before something else happens.

129
00:09:58,830 --> 00:10:01,320
So I'm going to do that just right now, kind of.

130
00:10:01,320 --> 00:10:05,010
It's a little like precursor to us going over the order of operations.

131
00:10:05,010 --> 00:10:07,590
So I'll do when I want times to.

132
00:10:08,310 --> 00:10:11,220
So that is going to be five times too, right?

133
00:10:11,220 --> 00:10:12,240
Because five store in here.

134
00:10:12,240 --> 00:10:13,260
So that'll give us 10.

135
00:10:15,090 --> 00:10:21,090
And then here I'm just going to say divided by five.

136
00:10:22,140 --> 00:10:23,810
So that'll happen afterwards.

137
00:10:27,460 --> 00:10:30,410
And you don't have to put spaces here if you want.

138
00:10:30,430 --> 00:10:35,500
You can just go like this and put them right up against each other, I like to put spaces just to make

139
00:10:35,500 --> 00:10:41,350
them look nice, but you know, I'll go ahead and leave that with no spaces just to show you.

140
00:10:42,250 --> 00:10:43,750
So I went ahead and saved it.

141
00:10:43,780 --> 00:10:46,420
I'm going to go ahead and compile it and run it.

142
00:10:47,230 --> 00:10:48,720
So we didn't see we get to here.

143
00:10:48,730 --> 00:10:56,320
So what happened was we did five times two, which was 10, and then we did 10, divided by five, which

144
00:10:56,320 --> 00:10:56,770
was two.

145
00:10:58,690 --> 00:11:00,370
Cool, so pretty interesting.

146
00:11:00,940 --> 00:11:10,450
You know, we can actually, like, add some stuff in here like I could do a minus one and compile this

147
00:11:10,450 --> 00:11:15,400
and run it, and then that becomes a one because it subtracts the ones that's using the subtraction

148
00:11:15,400 --> 00:11:15,940
operator.

149
00:11:16,960 --> 00:11:18,640
Let's look at this interesting one, though.

150
00:11:18,970 --> 00:11:19,900
The modulus.

151
00:11:19,900 --> 00:11:21,190
So I'm going to do

152
00:11:23,830 --> 00:11:24,730
another.

153
00:11:25,660 --> 00:11:26,380
Let's see.

154
00:11:26,440 --> 00:11:27,040
I just choose.

155
00:11:27,040 --> 00:11:27,520
I want.

156
00:11:27,520 --> 00:11:29,050
Actually, I want is five.

157
00:11:29,710 --> 00:11:36,580
So I will do I one one two.

158
00:11:39,670 --> 00:11:48,100
So what this is going to do is it's actually going to get the remainder of the division by two.

159
00:11:48,670 --> 00:11:53,800
So when I say division by two, I'm talking about five divided by two because I want to has five, right?

160
00:11:54,460 --> 00:11:57,400
So five divided by two.

161
00:11:57,610 --> 00:12:01,420
Two only goes into five two times.

162
00:12:02,380 --> 00:12:09,160
And there is a remainder of one left right, because two times two is four.

163
00:12:09,400 --> 00:12:12,280
That's that's one less than five, there's a remainder of one.

164
00:12:13,740 --> 00:12:18,870
So let's let me kind of be more concrete about this, too.

165
00:12:19,260 --> 00:12:27,660
This has there's a little bit of a nuance with the division, too, so I should point that out first.

166
00:12:27,660 --> 00:12:32,730
Actually, the division is depending on what you have over here.

167
00:12:32,730 --> 00:12:34,950
So if I do, I one divided by two.

168
00:12:35,340 --> 00:12:36,960
Let's see what prints out, actually.

169
00:12:36,960 --> 00:12:40,350
So I'm going to go ahead and say this and run this.

170
00:12:44,110 --> 00:12:44,580
OK.

171
00:12:44,770 --> 00:12:52,810
So you notice that only two prints out, but five divided by two is not to what it's doing is it's actually

172
00:12:52,810 --> 00:12:54,480
just rounding it down.

173
00:12:54,490 --> 00:13:04,430
So it's just saying OK to the integer two goes into five two times and it gives you an integer result.

174
00:13:05,440 --> 00:13:10,510
But what if I was to go like this and put it two point o instead?

175
00:13:10,570 --> 00:13:16,720
So now it's not an integer type that I'm dividing it by looks.

176
00:13:18,480 --> 00:13:23,340
It is a floating point, kind of, you know, like this float double.

177
00:13:24,580 --> 00:13:30,070
So let's go ahead and save this, and then I go down here, compile and run it.

178
00:13:30,760 --> 00:13:34,240
So you notice now it says two point five, which is actually accurate.

179
00:13:34,540 --> 00:13:39,560
The more accurate, you know, five divided by two is actually two point five.

180
00:13:39,610 --> 00:13:45,400
So if you add a floating point here on the right side of the operator, so you have a floating point

181
00:13:45,400 --> 00:13:49,740
upper end that will give you a floating point result here.

182
00:13:49,750 --> 00:13:56,440
But if I just have the two, then it just kind of rounds it to two, even though there's that one left

183
00:13:56,440 --> 00:13:57,310
over right.

184
00:13:57,850 --> 00:13:59,740
We talked about the remainder of one.

185
00:14:00,130 --> 00:14:03,160
So what happens if I put the modulus, then let's go ahead and run it.

186
00:14:07,570 --> 00:14:09,340
So it actually says one.

187
00:14:10,640 --> 00:14:11,060
OK.

188
00:14:11,090 --> 00:14:15,500
And it does that because there is one left over like we talked about two times.

189
00:14:15,540 --> 00:14:20,090
You know, it goes in two times and two times two is four and five minutes for one, there's one left

190
00:14:20,090 --> 00:14:22,500
over from that modulus.

191
00:14:22,500 --> 00:14:23,810
There's something really interesting.

192
00:14:23,810 --> 00:14:27,500
It's actually used a lot in computer science, as we'll see later in the course.

193
00:14:28,700 --> 00:14:33,950
It can have much broader applications, so really cool, operate a really cool concept.

194
00:14:36,150 --> 00:14:42,440
All right, so that's kind of all I have to say about these basic operators.

195
00:14:42,490 --> 00:14:48,380
Let's go ahead and move on to like using more parentheses and talking about the precedence between operators.

196
00:14:49,830 --> 00:14:53,820
So I'm actually going to comment this out for now.

197
00:14:53,830 --> 00:14:58,950
So what we can do when I say commenting out, we're just going to transform these lines into a comment

198
00:14:58,950 --> 00:15:00,780
by putting the flags in front of it.

199
00:15:01,260 --> 00:15:01,740
So.

200
00:15:03,780 --> 00:15:10,480
Like this list now, they're considered comments, so this code is not going to run anymore because

201
00:15:10,630 --> 00:15:15,310
it has to slash last before it's seen as just some comment, just like this one.

202
00:15:17,600 --> 00:15:24,680
So you can uncomment them if you want to run them, but I'm going to count them out just so we can have

203
00:15:24,680 --> 00:15:27,500
kind of a clear, concise output here.

204
00:15:28,490 --> 00:15:33,700
So let's go ahead and use our variables again.

205
00:15:33,710 --> 00:15:38,660
But what we're going to do now is discuss the order of operations or operator precedent.

206
00:15:38,680 --> 00:15:43,640
So we have this multiplication is the first.

207
00:15:44,180 --> 00:15:52,910
Then after that comes division and then we have the modulo and then we have plus and then we have minus

208
00:15:53,870 --> 00:15:54,260
here.

209
00:15:56,860 --> 00:16:04,090
So that's kind of the order of operations, if you have heard of that in math and some other math class,

210
00:16:04,660 --> 00:16:12,760
you know that there was an order to things like you maybe have heard of him dos, which was like starting

211
00:16:12,760 --> 00:16:20,400
with the parents parentheses, the parentheses, exponents, multiplication division, addition, subtraction.

212
00:16:20,410 --> 00:16:22,060
That's what that acronym stood for.

213
00:16:22,540 --> 00:16:27,280
So in code, we just call it operator precedence, and it's really the same thing.

214
00:16:27,910 --> 00:16:34,390
So if I knew another line here, I'm just going to copy this

215
00:16:37,540 --> 00:16:38,070
cool.

216
00:16:38,830 --> 00:16:41,470
So let me go ahead and.

217
00:16:44,170 --> 00:16:49,660
Modify this a little bit, so let's think about and think about this, so if I want to do something

218
00:16:49,660 --> 00:16:57,400
like I'm one plus two times three minus one.

219
00:16:58,090 --> 00:17:03,190
So let's go ahead and save this and compile it and print it out.

220
00:17:04,090 --> 00:17:06,700
So we have 10 here, so I want us five, right?

221
00:17:08,400 --> 00:17:11,940
So we have five plus two times, three minus one.

222
00:17:12,180 --> 00:17:16,470
Well, you know, what's actually happening is this first, so we are two times three, which is six.

223
00:17:17,190 --> 00:17:23,430
And then what it's doing is it's saying, Oh, OK, we do this first three times two six, six plus

224
00:17:23,430 --> 00:17:27,660
five is what it'll do because of the order, other operations, which is 11.

225
00:17:28,380 --> 00:17:29,790
And so this is 11.

226
00:17:29,790 --> 00:17:31,740
And then it does a minus one, which is 10.

227
00:17:33,700 --> 00:17:35,420
So what if we didn't want it?

228
00:17:35,440 --> 00:17:39,430
What if we wanted to just have it in left or right or, well, then we'd have to use parentheses, right?

229
00:17:39,450 --> 00:17:44,770
So sometimes it's good to use parentheses if you want to force some some sort of mathematical ordering.

230
00:17:45,850 --> 00:17:50,170
So right now, we expect it to do five plus two.

231
00:17:50,260 --> 00:17:55,840
So there'd be five, six seven, we'll have seven times three then, which will happen next because

232
00:17:55,840 --> 00:17:59,680
multiplication happens before subtraction, seven times three is twenty one.

233
00:17:59,950 --> 00:18:01,060
And then we'll have a minus one.

234
00:18:01,070 --> 00:18:03,160
So we should we should get the result of 20.

235
00:18:04,480 --> 00:18:10,180
So I say this, compile it, run it and we get a result of 20 like expected.

236
00:18:11,960 --> 00:18:15,690
So that's kind of just, you know, what's going on with precedence here.

237
00:18:15,980 --> 00:18:22,790
If you want to, you know, if you wanted to flip this around, let's say we did a minus three times,

238
00:18:22,790 --> 00:18:24,900
maybe like two or something like that.

239
00:18:25,880 --> 00:18:32,570
You know, what it's going to do is it actually is going to evaluate three times to first and then it's

240
00:18:32,570 --> 00:18:35,030
just going to subtract that from this.

241
00:18:36,200 --> 00:18:42,440
So if you want this to happen all in the same order, really what you're going to need to do like in

242
00:18:42,440 --> 00:18:45,500
left to right, what are you going to need this surround this in parentheses as well?

243
00:18:46,680 --> 00:18:54,030
So that is something that you can use as these parentheses to force the ordering, otherwise that happens

244
00:18:54,030 --> 00:18:55,050
in this order here.
