Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Most programming languages let you chain math operations together, and C# is no exception. The order of operations you were taught in math class applies in C# too.
- Most programming languages let you chain math operations together, and C# is no exception:
Console.WriteLine(1 + 2 + 3);
- Order of operations you were taught in math class applies in C# too.
Console.WriteLine(1 + 2 * 3);
- If you didn't have order of operations, you'd assume that you add
1
to2
, getting 3, and then multiply3 * 3
to get9
. But that's not what we get. - Instead we get 7.
- This is because of order of operations. In chained math operations, multiplication and division operations always come first , and addition and subtraction second .
- C# respects this concept by following something called operator precedence. That is, the evaluation of some operators precedes the evaluation of some other operators. The multiplication and division operators have higher precedence that addition and subtraction operators.
- So that's why when we evaluate
1 + 2 * 3
, we get7
and not9
. The multiplication operator has higher precedence than the addition operator, so we do the multiplication first, giving us6
. We then add1
and6
, giving us 7. - But suppose we wanted to ensure that the addition operation occurs first. If we were working in a math textbook, we'd add parentheses around the operation to indicate it should go first, no matter what:
(1 + 2) * 3
. - And that same notation works in C#. C# will always evaluate math operations within parentheses first, before it goes on to evaluate the rest of the expression. So with the parentheses,
1 + 2
is evaluated first, giving3
, and then that's multiplied by3
to give9
.
If you're not comfortable with operator precedence, or you want to learn more, visit these links.
Most programming languages like you
change math operations together and
0:00
C# is no exception.
0:05
The order of operations that you were
taught in math class supplies in C# too.
0:07
If you didn't have order
of operations you'd
0:12
assume that you add 1 to 2 getting 3.
0:15
And then multiply 3 by 3 to get 9,
but that's not what we get.
0:17
Instead, the result is 7.
0:24
This is because of order of operations.
0:27
In chain math operations,
multiplication and
0:31
division operations always come first and
addition and subtraction second.
0:34
C# respects this concept by following
something called operator precedents.
0:40
That is,
0:45
the evaluation of some operators precedes
the evaluation of some other operators.
0:45
The multiplication and division operators
have higher precedents than addition and
0:51
subtraction operators.
0:56
So that's why when we evaluate 1 + 2 x 3,
we get 7 and not 9.
0:57
The multiplication operator has higher
precedence than the addition operator.
1:02
So we do the multiplication first giving
us 6, we then add 1 in 6 giving us 7.
1:08
But suppose we wanted to ensure that
the addition operation occurs first.
1:15
If we were working in a math textbook we'd
add parentheses around the operation to
1:20
indicate it should go first, and
the multiplication should happen second.
1:24
And that same notation works in C#,
C# will always evaluate math operations
1:28
within parentheses first before it goes on
to evaluate the rest of the expression.
1:33
So with the parentheses 1 + 2
is evaluated first giving 3.
1:39
And then that's multiplied by 3 to give 9.
1:45
If you're not comfortable with operator
presidence or you want to learn more,
1:48
check the teachers notes for more info.
1:52
You need to sign up for Treehouse in order to download course files.
Sign up