1
00:00:00,790 --> 00:00:07,150
In this video we're going to be looking at the different operators that are available to us in C sharp.

2
00:00:07,150 --> 00:00:13,570
No I already went ahead and created the project and I call it C sharp upper it does once again try to

3
00:00:13,570 --> 00:00:17,800
avoid any special characters when naming your projects and solutions.

4
00:00:17,800 --> 00:00:24,660
And so I just use the word sharp instead of the the special character which looks like a no sign right.

5
00:00:24,670 --> 00:00:31,090
So C sharp operators and I already took the liberty of kind of writing in some things in the main function

6
00:00:31,090 --> 00:00:35,320
so you can pause the video try and create the project yourself and put you in these comments and then

7
00:00:35,320 --> 00:00:38,620
you can resume when you are done.

8
00:00:38,660 --> 00:00:44,360
No operators are symbols that are used to performed well literal operations art.

9
00:00:44,380 --> 00:00:50,260
So in any programming language you have access to these operators when you want to do things like math

10
00:00:50,620 --> 00:00:57,040
and all the other categories where they're loathe to do things that comparisons but for this introduction

11
00:00:57,040 --> 00:01:02,920
to operators are just going to be dealing with the ones that are probably more mathematical and we need

12
00:01:02,920 --> 00:01:05,170
to know if nothing else.

13
00:01:05,170 --> 00:01:08,810
So let's start off with the basic assignment operator.

14
00:01:08,830 --> 00:01:16,180
So we have used this already in previous examples and that is used when we want to give some variable

15
00:01:16,250 --> 00:01:16,750
of value.

16
00:01:16,780 --> 00:01:23,110
So if I have a variable it's into num then I want norm to have the value of five then I want to say

17
00:01:23,110 --> 00:01:26,550
num you are assigned the value five.

18
00:01:27,520 --> 00:01:30,400
So that's really what that is it's really an assignment.

19
00:01:30,430 --> 00:01:35,300
So 1 equals sign by itself is really seen as an assignment.

20
00:01:35,320 --> 00:01:35,610
All right.

21
00:01:35,620 --> 00:01:42,790
So we're putting the value of five inside of our variable whereas signing the value of five to our variable

22
00:01:42,970 --> 00:01:43,540
No.

23
00:01:44,440 --> 00:01:44,860
All right.

24
00:01:45,190 --> 00:01:50,230
So that's the basic and very easy one and that we can always just write a console.

25
00:01:50,230 --> 00:01:51,040
That's right.

26
00:01:51,040 --> 00:01:54,580
Line just to prove that it works.

27
00:01:54,580 --> 00:02:03,630
So I'm just going to say assign assigned value to variable.

28
00:02:03,670 --> 00:02:04,550
All right.

29
00:02:04,550 --> 00:02:08,470
And then I'm going to use my interpolation.

30
00:02:08,480 --> 00:02:11,850
So we should know how to concatenate string already.

31
00:02:11,900 --> 00:02:16,820
So since I have this one of villains I mean this is the one that you're going to use more frequently

32
00:02:16,820 --> 00:02:18,260
than not.

33
00:02:19,280 --> 00:02:24,560
Otherwise use the one that's your language or your Net framework supports.

34
00:02:24,560 --> 00:02:31,790
So once again we use this to assign a value to a variable it can be multiple variables with multiple

35
00:02:31,790 --> 00:02:32,930
assignments.

36
00:02:32,960 --> 00:02:38,860
It can be a static value so static means that I have called it in meaning I put five.

37
00:02:38,870 --> 00:02:44,110
So this will never change unless I come back to this screen and change the stakes from five to 10.

38
00:02:44,120 --> 00:02:45,570
That's the only way this will change.

39
00:02:45,590 --> 00:02:52,280
But then that's what makes it static whereas when we're assigning values from the user input it makes

40
00:02:52,280 --> 00:02:57,430
it more dynamic and the principle of assigning doesn't change.

41
00:02:57,430 --> 00:02:58,140
All right.

42
00:02:58,260 --> 00:03:01,710
Now let's move on to the arithmetic operator.

43
00:03:01,730 --> 00:03:10,070
So for arthritic operators they're usually used to perform mathematical operations.

44
00:03:10,130 --> 00:03:16,160
So I'm going to define two variables are I know int x and then I'm going to show you that you can actually

45
00:03:16,160 --> 00:03:24,410
do a bit curious on and assignment in one line so I can say X is equal to five and into Y is equal to

46
00:03:24,590 --> 00:03:25,450
10.

47
00:03:25,700 --> 00:03:26,240
Right there.

48
00:03:26,270 --> 00:03:33,260
So instead of splitting it into the lines I could just see int it whatever variable is equal to whatever

49
00:03:33,260 --> 00:03:35,720
value I want and do that in one line.

50
00:03:35,720 --> 00:03:37,900
So that's another week into your assignment.

51
00:03:37,900 --> 00:03:38,270
Right.

52
00:03:38,720 --> 00:03:41,380
But for this section I want us to do some math.

53
00:03:41,390 --> 00:03:46,060
So I'm going to go through different examples that we're going to print to screen the different outcomes

54
00:03:46,310 --> 00:03:48,620
of the math that we're doing with these.

55
00:03:48,650 --> 00:03:55,090
So I'm going to say console dot console dot right.

56
00:03:55,090 --> 00:03:58,350
The line is equal to.

57
00:03:58,900 --> 00:04:03,780
And then for each line I'm going to in to use my interpolation.

58
00:04:03,790 --> 00:04:10,540
So I went to see a dollar sign open and closed quotation marks and then I went to start off with additions

59
00:04:10,600 --> 00:04:18,190
but we're going to go through the different examples additions and then I am showing the result of an

60
00:04:18,250 --> 00:04:24,490
addition operation between x and y so to add X and Y we're literally going to use the plus side.

61
00:04:24,520 --> 00:04:31,540
So anyone with a plus saying can be used for concatenation reasons also but it's a form of what we call

62
00:04:31,900 --> 00:04:36,030
polymorphism where the plus sign has multiple purposes.

63
00:04:36,040 --> 00:04:39,310
The plus sign is seeing here that I'm adding two numbers.

64
00:04:39,310 --> 00:04:46,630
So do some math whereas in the context of a string when you see block of string plus string variable

65
00:04:46,630 --> 00:04:53,290
it seeing I'm adding two strings so technically it's the same addition but the outcome is going to be

66
00:04:53,290 --> 00:05:00,690
different because we don't expect to see 5 1 0 at the end of this operation we expect to see 15 where.

67
00:05:00,910 --> 00:05:04,170
Whereas if there were strings then you would see 5 1 0.

68
00:05:04,310 --> 00:05:10,800
So adorable to just know that this is arithmetic and this is how we're adding two numbers.

69
00:05:10,870 --> 00:05:15,250
So that's all we add and I'm just going to duplicate this line because I want to show a subtraction

70
00:05:15,250 --> 00:05:19,180
multiplication division and then this one called modules.

71
00:05:19,190 --> 00:05:19,450
Right.

72
00:05:19,450 --> 00:05:24,250
So I'm just going to duplicate this console of that right line by holding down control and pressing

73
00:05:24,250 --> 00:05:24,620
D.

74
00:05:24,790 --> 00:05:30,370
So that's a nice quick way to duplicate your lines in Visual Studio and then I'm just going to change

75
00:05:30,370 --> 00:05:30,810
this also.

76
00:05:30,850 --> 00:05:36,970
This is subtraction and then subtraction it's the minus sign.

77
00:05:37,000 --> 00:05:37,440
All right.

78
00:05:37,700 --> 00:05:45,140
So we're adding x plus y and they were subtracting X minus Y so anybody who understands the basic mathematics

79
00:05:45,470 --> 00:05:51,080
this should not be too all tough place for you to understand.

80
00:05:51,080 --> 00:05:54,140
So multiplication we use the asterisk.

81
00:05:54,140 --> 00:05:55,480
We don't use the X..

82
00:05:55,530 --> 00:06:01,130
You know when we are writing it on paper we use X but as you can see X is going to be seen as x.

83
00:06:01,220 --> 00:06:08,210
So if we want to operator that handles multiplication we need to use the asterisk or the star.

84
00:06:08,220 --> 00:06:08,680
All right.

85
00:06:09,020 --> 00:06:13,620
If we're doing division then what.

86
00:06:13,830 --> 00:06:19,950
Most if not all keyboards and I was just going to go out on a limb and say no keyboard has the division

87
00:06:19,950 --> 00:06:21,600
unsigned the way we write it on paper.

88
00:06:21,600 --> 00:06:28,190
So far division have to use your slash so the same slash you use for the comments is the same slash

89
00:06:28,200 --> 00:06:33,630
that you use for division but obviously you're using one of them because if I use two then that's going

90
00:06:33,630 --> 00:06:36,140
to start some commenting right.

91
00:06:36,150 --> 00:06:38,190
So one slash that is division.

92
00:06:38,190 --> 00:06:44,760
So this is saying that X R 5 is going to be divided by Y or 10.

93
00:06:44,760 --> 00:06:46,300
All right.

94
00:06:46,320 --> 00:06:53,950
We do have modulus and then modulus is the percentage side.

95
00:06:54,090 --> 00:07:00,620
So this is basically our division within this modulus is going to return the remainder.

96
00:07:00,670 --> 00:07:01,020
Right.

97
00:07:01,020 --> 00:07:07,260
So for instance if I said 2 divided by a 1.

98
00:07:07,350 --> 00:07:10,120
You know what that me actually change the told so I can.

99
00:07:10,170 --> 00:07:12,750
When we print details the screen you see exactly what I'm seeing.

100
00:07:12,750 --> 00:07:15,410
So let's say this is three and this is two.

101
00:07:15,510 --> 00:07:15,990
All right.

102
00:07:16,500 --> 00:07:23,340
So when we see three divided by two we're going to get back one point something right.

103
00:07:23,370 --> 00:07:26,240
That's the one point five.

104
00:07:26,240 --> 00:07:27,210
That would be the decimal.

105
00:07:27,240 --> 00:07:27,520
Right.

106
00:07:27,750 --> 00:07:34,440
But then if I said modulus it's going to return one because 2 goes into three one time with our remainder

107
00:07:34,440 --> 00:07:35,800
of one.

108
00:07:35,850 --> 00:07:36,570
All right.

109
00:07:36,570 --> 00:07:42,000
So that's the difference between the division and the modulus the modulus returns the remainder of the

110
00:07:42,010 --> 00:07:48,240
division operation and division well just tries to divide so I'm going to pause right here and then

111
00:07:48,240 --> 00:07:52,450
we're going to run this operation and see exactly what's happening.

112
00:07:52,560 --> 00:07:57,570
And so we we usually go over here in our solution explorer or actually our project and say it's at the

113
00:07:57,570 --> 00:08:01,770
start of project but you've probably figured out the shorter we had to do that by no.

114
00:08:01,800 --> 00:08:04,570
And that is by selecting from this dropped only.

115
00:08:04,580 --> 00:08:10,080
So right beside the start button there's a dropdown list with all the projects and you can just see

116
00:08:10,230 --> 00:08:11,300
which one you want.

117
00:08:11,340 --> 00:08:13,610
And then go ahead and start.

118
00:08:13,620 --> 00:08:18,360
All right so on screen we're seeing the results of our arithmetic operation.

119
00:08:18,360 --> 00:08:20,300
So we assigned the value.

120
00:08:20,340 --> 00:08:21,500
That's the first one.

121
00:08:21,750 --> 00:08:28,700
And then addition is five plus three plus two is five subtraction is one multiplication is six division

122
00:08:28,710 --> 00:08:29,210
is one.

123
00:08:29,220 --> 00:08:30,380
And modulus is one.

124
00:08:30,390 --> 00:08:34,930
So because they're both returning one you're not really seeing the pull off modulus.

125
00:08:35,280 --> 00:08:41,400
So I'm going to change this and I'm going to use and when to switch these numbers are on someone to

126
00:08:41,400 --> 00:08:46,610
use three and I'm going to use value like eight.

127
00:08:46,620 --> 00:08:51,180
So at least we know that when we hit the modulus we won't be seeing the same for the division and the

128
00:08:51,180 --> 00:08:56,740
seeing for the modulus but actually you will be sorry if my math is my my slip no.

129
00:08:56,820 --> 00:08:57,330
Let me try.

130
00:08:57,330 --> 00:09:06,000
Eleven so eleven divided by three is going to be three times but then the modulus is expected to return

131
00:09:06,120 --> 00:09:09,830
to because that's the remainder from this division operation.

132
00:09:09,840 --> 00:09:12,840
So let's try that again all right.

133
00:09:12,850 --> 00:09:13,540
So here we go.

134
00:09:13,570 --> 00:09:19,340
So we are getting different values because well once again it's X and Y and whatever we put in as of

135
00:09:19,350 --> 00:09:22,730
others of X and Y will influence the math being done.

136
00:09:22,750 --> 00:09:25,810
So three plus 11 is 14.

137
00:09:26,020 --> 00:09:27,870
Three from 11.

138
00:09:27,910 --> 00:09:32,660
That is eight or if I'm to read it exactly how we wrote it.

139
00:09:32,670 --> 00:09:42,370
That's eleven minus three leaves eight multiplication that is 11 times three division that is 11 divided

140
00:09:42,370 --> 00:09:44,740
by three is three.

141
00:09:44,740 --> 00:09:46,630
So it goes into it three times.

142
00:09:46,630 --> 00:09:48,950
I notice it's returning just an INT.

143
00:09:49,030 --> 00:09:49,720
All right.

144
00:09:49,720 --> 00:09:52,200
And then I say give me the modulus.

145
00:09:52,210 --> 00:09:54,370
So it's returning home many.

146
00:09:54,410 --> 00:10:03,610
You know the remainder of the division operation of eleven and three no right now we're adding two variables

147
00:10:03,620 --> 00:10:10,150
what if we wanted to add a variable to the value it had in it's of as well as another value.

148
00:10:10,180 --> 00:10:17,400
So for more context I wanted to add whatever X is and add four more to it.

149
00:10:17,420 --> 00:10:19,160
So whatever value X has.

150
00:10:19,190 --> 00:10:20,600
I want to add forward to it.

151
00:10:20,600 --> 00:10:22,760
And that should be the new value of x.

152
00:10:22,790 --> 00:10:30,280
So that means I would have to do something like X is no assign the value or X is equal to itself to

153
00:10:30,310 --> 00:10:32,670
loss for right.

154
00:10:32,720 --> 00:10:35,580
So I'm doing this after this operation so 11 3.

155
00:10:35,660 --> 00:10:37,040
Those are the values.

156
00:10:37,430 --> 00:10:40,890
And then the math will be done with the values of eleven point three.

157
00:10:40,910 --> 00:10:47,810
But then I want to change the value of x to be four more than it was when it was first initialized.

158
00:10:47,810 --> 00:10:48,470
Right.

159
00:10:48,470 --> 00:10:55,100
So now I'm adding four to whatever value X had because X is a variable I don't know what it has at any

160
00:10:55,100 --> 00:10:55,490
point.

161
00:10:55,490 --> 00:10:58,260
So I have to retain it in the math.

162
00:10:58,370 --> 00:11:06,050
And then after that math is done I assign it to itself so that with that done I'm just going to use

163
00:11:06,050 --> 00:11:16,640
this console right line and then I'm going to see a new value of x and then I'm just printing X to the

164
00:11:16,640 --> 00:11:23,720
screen and then just to prove that that gave it a new value I'm going to print reprint all of these

165
00:11:23,720 --> 00:11:31,310
additions operations are sorry all of these arthritic operations using X and then the expectation is

166
00:11:31,310 --> 00:11:37,400
that the values that the initial operations would have yielded should be different from these values

167
00:11:37,430 --> 00:11:41,630
after we've changed the value of x so let's try that.

168
00:11:41,660 --> 00:11:47,420
So now we can take a look and we see that okay all of these values were the same as we saw last time

169
00:11:47,660 --> 00:11:54,890
but then the new value of x is 15 because like I said we added four to whatever value X had and then

170
00:11:55,280 --> 00:12:00,200
it will work all different math because the value of x is different.

171
00:12:00,250 --> 00:12:06,080
Notice the division isn't all five because three goes perfectly into fifty and five times and these

172
00:12:06,080 --> 00:12:14,520
nor I mean those no operations like this are done all the time most you know you don't always know what

173
00:12:14,520 --> 00:12:20,700
value is there so you just retain the variable and do whatever addition or subtraction and multiplication

174
00:12:20,920 --> 00:12:23,380
it can be done with any other operators.

175
00:12:23,420 --> 00:12:31,530
If I wanted to multiply the value of X by four then I just say X is equal to its of times four and then

176
00:12:31,530 --> 00:12:33,100
that's its new value.

177
00:12:33,300 --> 00:12:42,400
No because of that function or the need to do that kind of function we have what we call compound assignment.

178
00:12:42,420 --> 00:12:52,880
So instead of having to type of x is equal to x plus four then we can actually do something like x plus

179
00:12:53,330 --> 00:12:55,810
equals five.

180
00:12:55,940 --> 00:12:58,250
So I'm just going to answer it for.

181
00:12:58,250 --> 00:13:01,210
Since we're using for it let me use for all the time.

182
00:13:01,220 --> 00:13:06,920
So I'm just going to copy this down here and show you that these two lines are seeing the same thing

183
00:13:07,310 --> 00:13:15,800
X plus equals four is the same thing as saying X is equal to x plus four and the same applies for any

184
00:13:15,800 --> 00:13:18,710
other operation that would be carrying out.

185
00:13:18,710 --> 00:13:28,520
So if I wanted to see X should be equal to x times five.

186
00:13:28,540 --> 00:13:33,800
All right so X is new value should be itself whatever value it has at the time.

187
00:13:33,800 --> 00:13:37,730
Times five whatever this use should be assigned to X.

188
00:13:37,820 --> 00:13:41,110
That's the same thing as seeing another right.

189
00:13:41,120 --> 00:13:50,110
The longer we in comments initially the shorter we x star equals five.

190
00:13:50,300 --> 00:13:50,880
All right.

191
00:13:50,920 --> 00:13:56,920
So any time we want to do that kind of operation where we're assigning the variable a new value based

192
00:13:56,920 --> 00:14:00,840
on what its value is already then we can rewrite it either way.

193
00:14:00,840 --> 00:14:05,440
We can write it as X is equal to x times five.

194
00:14:05,440 --> 00:14:12,280
Or we can see the variable which is x in this case that operates all equal.

195
00:14:12,310 --> 00:14:14,820
I notice there's no space there's no gap here.

196
00:14:14,820 --> 00:14:17,350
If you put that space there you might get an arrow.

197
00:14:17,350 --> 00:14:21,870
So it is operator assignment operator.

198
00:14:22,030 --> 00:14:28,180
And then the value and then that would imply to C sharp that you're doing this operation.

199
00:14:28,180 --> 00:14:28,390
All right.

200
00:14:28,390 --> 00:14:34,440
So I just wrote out some examples of assignment operators who can take some time and write these off.

201
00:14:34,510 --> 00:14:41,140
But what I'll be doing here is changing the value of X with each operation just showing you that whatever

202
00:14:41,140 --> 00:14:49,710
the operation is with the arithmetic symbols can be used for the compound assignment.

203
00:14:49,720 --> 00:14:53,520
And as long as you just have that operator and then the equals sign.

204
00:14:53,530 --> 00:14:55,790
So let's try it again.

205
00:14:55,800 --> 00:14:59,270
All right so we have our first block where we did our assignment.

206
00:14:59,280 --> 00:15:07,140
It is the value was 11 and then we did the operations eleven and the three I believe.

207
00:15:07,140 --> 00:15:07,760
Yeah.

208
00:15:07,800 --> 00:15:14,310
And then we did the operations with the three and eleven and then we changed the value of the x variable

209
00:15:14,310 --> 00:15:15,110
to 15.

210
00:15:15,330 --> 00:15:21,240
And then we did new operations and then by the time we got down there the value was still 15 because

211
00:15:21,240 --> 00:15:24,950
we didn't change a value again until I said X plus equals five.

212
00:15:24,960 --> 00:15:27,390
So 15 plus five is 20.

213
00:15:27,390 --> 00:15:34,050
Then I said X minus equals three which means whatever value I have no which is 20 I want it to be equal

214
00:15:34,050 --> 00:15:36,810
to that value minus three.

215
00:15:36,810 --> 00:15:42,760
So by variables value got set no to 17 because it was 20 minus three.

216
00:15:42,870 --> 00:15:48,240
And that's the new value of X then we go on to say whatever the value of x is I want to multiply it

217
00:15:48,240 --> 00:15:48,990
by two.

218
00:15:48,990 --> 00:15:52,940
So I am saying 17 times two is no thirty four.

219
00:15:53,250 --> 00:16:00,420
Then I say okay whatever value that has make it make x no equal to that divided by three and thirty

220
00:16:00,420 --> 00:16:04,260
four divided by three is pretty much eleven.

221
00:16:04,440 --> 00:16:12,510
And then we went on to say give me the modulus of three into eleven and then that gave us the two.

222
00:16:12,510 --> 00:16:18,220
So that's just how we handle compound assignments and arithmetic operations.

223
00:16:18,220 --> 00:16:23,740
So like I said these are not all of the operators that are available to us in C sharp.

224
00:16:23,850 --> 00:16:31,220
And by extension any other programming language but this is a good place to start.

225
00:16:31,220 --> 00:16:38,010
And as we go along look at if statements and for losers start seeing other operators use for other things.
