1
00:00:00,090 --> 00:00:03,030
We just saw the use of arithmetic
in code for the first time.
2
00:00:03,030 --> 00:00:06,060
Of course,
C sharp can do more than just addition.
3
00:00:06,060 --> 00:00:07,974
You can do anything
a basic calculator can do.
4
00:00:07,974 --> 00:00:14,770
There's subtraction,
there's multiplication, there's division.
5
00:00:14,770 --> 00:00:17,180
There's also an operator called modulus.
6
00:00:17,180 --> 00:00:21,910
The modulus operator returns
the remainder of a division operation.
7
00:00:21,910 --> 00:00:24,776
You can also combine these operations
together in a single line.
8
00:00:26,926 --> 00:00:29,750
As you can see,
the result of this expression is 6.
9
00:00:29,750 --> 00:00:32,710
When you combine a bunch of
operations together like this,
10
00:00:32,710 --> 00:00:36,730
you need to be very careful about what
order the operations are performed in.
11
00:00:36,730 --> 00:00:39,610
Some operators have higher
precedence than others, and
12
00:00:39,610 --> 00:00:43,070
they are performed first even if
they aren't first on the line.
13
00:00:43,070 --> 00:00:47,190
In C sharp this works exactly like
the arithmetic you learned in school.
14
00:00:47,190 --> 00:00:51,400
Multiplication, division, and
modulus are performed first.
15
00:00:51,400 --> 00:00:56,160
If you have multiple multiplication,
division, or modulus operators in a row
16
00:00:56,160 --> 00:00:59,560
like we do here,
then they're performed left to right.
17
00:00:59,560 --> 00:01:03,760
Then addition and subtraction
are performed, again from left to right.
18
00:01:03,760 --> 00:01:07,680
So in our expression here,
we first do 3 times 7.
19
00:01:07,680 --> 00:01:10,960
Then we divide the result of that by 3.
20
00:01:10,960 --> 00:01:14,880
Then we take the modulus of that,
that gives us 1.
21
00:01:14,880 --> 00:01:20,240
Then we add 5 and 2 together and
subtract 1 and we get 6.
22
00:01:20,240 --> 00:01:23,680
Now what if we wanted
the 2- 3 to happen first?
23
00:01:23,680 --> 00:01:27,075
Just like in arithmetic,
we can use parentheses for that.
24
00:01:27,075 --> 00:01:29,470
Parentheses have the highest precedence,
so
25
00:01:29,470 --> 00:01:32,370
whatever is inside them
will get evaluated first.
26
00:01:33,450 --> 00:01:37,859
If we wanted the addition to happen next,
we could add more parentheses like this.
27
00:01:38,870 --> 00:01:41,280
The innermost parentheses
have the highest precedence,
28
00:01:41,280 --> 00:01:45,460
so what's in them will
always get evaluated first.
29
00:01:45,460 --> 00:01:48,500
We don't all encounter long
arithmetic like this every day.
30
00:01:48,500 --> 00:01:51,010
Sometimes, I have to stop and
think about it.
31
00:01:51,010 --> 00:01:54,350
I'll include the order of operation rules
in the teacher's notes for your reference.