WEBVTT
1
00:00:00.005 --> 00:00:01.008
- [Instructor] The important thing to remember
2
00:00:01.008 --> 00:00:04.002
when it comes to math and programming
3
00:00:04.002 --> 00:00:06.009
is that the computer does the work.
4
00:00:06.009 --> 00:00:08.004
Otherwise, you just need to know
5
00:00:08.004 --> 00:00:13.008
the C language's math operators and know how they're used.
6
00:00:13.008 --> 00:00:15.009
Like many other computer calculations,
7
00:00:15.009 --> 00:00:18.006
the C language uses four basic symbols
8
00:00:18.006 --> 00:00:21.007
for the four basic math operations:
9
00:00:21.007 --> 00:00:25.009
addition, subtraction, multiplication, and division.
10
00:00:25.009 --> 00:00:27.003
These should be familiar to you,
11
00:00:27.003 --> 00:00:30.007
especially if you've used a spreadsheet.
12
00:00:30.007 --> 00:00:34.008
Other less common operators include increment and decrement.
13
00:00:34.008 --> 00:00:39.000
These are single operators despite using two characters.
14
00:00:39.000 --> 00:00:42.007
The modulo operator calculates the remainder of two values
15
00:00:42.007 --> 00:00:44.004
called the modulus,
16
00:00:44.004 --> 00:00:47.009
so the modulus of 2 and 11 is 1
17
00:00:47.009 --> 00:00:53.003
which is what's left over when you cleanly divide 11 by 2.
18
00:00:53.003 --> 00:00:55.001
In C, the calculation
19
00:00:55.001 --> 00:00:58.001
goes on the right side of the equal sign.
20
00:00:58.001 --> 00:01:00.005
The result is then assigned to the variable
21
00:01:00.005 --> 00:01:02.009
on the left side of the equal sign.
22
00:01:02.009 --> 00:01:04.001
If you forget this order,
23
00:01:04.001 --> 00:01:06.007
you will see the infamous L-value warning
24
00:01:06.007 --> 00:01:09.000
or error when you compile.
25
00:01:09.000 --> 00:01:11.006
Mathematical results can be used immediately
26
00:01:11.006 --> 00:01:15.001
as in the printf function shown here.
27
00:01:15.001 --> 00:01:18.008
Finally, complex math equations are evaluated
28
00:01:18.008 --> 00:01:21.007
by using the order of precedence.
29
00:01:21.007 --> 00:01:26.001
In C, equations are evaluated left to right.
30
00:01:26.001 --> 00:01:28.008
Multiplication and division are done first,
31
00:01:28.008 --> 00:01:31.002
then come addition and subtraction.
32
00:01:31.002 --> 00:01:33.003
You can, however, override this order
33
00:01:33.003 --> 00:01:36.004
by using parentheses to set certain operations
34
00:01:36.004 --> 00:01:38.002
to be calculated first.
35
00:01:38.002 --> 00:01:41.002
It doesn't matter how long the equation gets,
36
00:01:41.002 --> 00:01:43.006
the computer calculates the result,
37
00:01:43.006 --> 00:01:45.009
but ensure that the values in the equation
38
00:01:45.009 --> 00:01:49.000
are stated exactly as you intended
39
00:01:49.000 --> 00:01:50.006
to get the result you need.