Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic 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.

0:00
Most programming languages like you change math operations together and

0:05
C# is no exception.

0:07
The order of operations that you were taught in math class supplies in C# too.

0:12
If you didn't have order of operations you'd

0:15
assume that you add 1 to 2 getting 3.

0:17
And then multiply 3 by 3 to get 9, but that's not what we get.

0:24
Instead, the result is 7.

0:27
This is because of order of operations.

0:31
In chain math operations, multiplication and

0:34
division operations always come first and addition and subtraction second.

0:40
C# respects this concept by following something called operator precedents.

0:45
That is,

0:45
the evaluation of some operators precedes the evaluation of some other operators.

0:51
The multiplication and division operators have higher precedents than addition and

0:56
subtraction operators.

0:57
So that's why when we evaluate 1 + 2 x 3, we get 7 and not 9.

1:02
The multiplication operator has higher precedence than the addition operator.

1:08
So we do the multiplication first giving us 6, we then add 1 in 6 giving us 7.

1:15
But suppose we wanted to ensure that the addition operation occurs first.

1:20
If we were working in a math textbook we'd add parentheses around the operation to

1:24
indicate it should go first, and the multiplication should happen second.

1:28
And that same notation works in C#, C# will always evaluate math operations

1:33
within parentheses first before it goes on to evaluate the rest of the expression.

1:39
So with the parentheses 1 + 2 is evaluated first giving 3.

1:45
And then that's multiplied by 3 to give 9.

1:48
If you're not comfortable with operator presidence or you want to learn more,

1:52
check the teachers notes for more info.
You need to sign up for Treehouse in order to download course files.
Sign up