What precedence is
When an expression has several operators, C# needs a rule for which one to apply first. That rule is operator precedence. You already know it from school arithmetic: in 1 + 2 * 3 the multiplication comes first, so the result is 7, not 9.
Console.WriteLine(1 + 2 * 3); // 7
Console.WriteLine(6 - 4 / 2); // 4
Console.WriteLine(5 * 4 - 2 / 2); // 19
In the last line, 5 * 4 is 20 and 2 / 2 is 1, so the result is 20 - 1, which is 19.
The rules, from highest to lowest
- Parentheses are always evaluated first.
- Unary operators such as the minus in
-x. - Multiplication, division and remainder:
*,/,%. - Addition and subtraction:
+,-.
Operators at the same level are applied from left to right. So 10 - 4 - 3 is (10 - 4) - 3, which is 3, and 12 / 3 * 2 is (12 / 3) * 2, which is 8, not 2.
Assignment (=, += and the like) sits below all of these, which is why the right side is fully computed before it is stored. Comparison and logical operators, which you meet later, are lower than arithmetic too, so a + 1 < b * 2 does the arithmetic first.
There is no power operator
Some languages have ** or ^ for exponents. In C# the ^ symbol is a bitwise operator that does something entirely different, and there is no **. To raise a number to a power, call Math.Pow(2, 3). It returns a double, here 8. Because it is a method call, its arguments are evaluated before it runs, and the call acts like a single value in the surrounding expression.
Parentheses override the order
Put part of an expression in parentheses and it runs first:
Console.WriteLine((1 + 2) * 3); // 9
Console.WriteLine((6 - 4) / 2); // 1
Console.WriteLine(5 * (4 - 2) / 2); // 5
The last line is worked out as 5 * 2 / 2, then 10 / 2, which is 5. Nested parentheses are evaluated from the innermost outward: 6 / ((4 + 2) / 3) becomes 6 / (6 / 3), then 6 / 2, which is 3.
Integer division changes the result
Precedence tells you the order, but the types decide what each step produces. With int operands, / drops the fractional part. Take 6 / 4 * 3 + 5. Division and multiplication share a level, so they run left to right: 6 / 4 is 1 (not 1.5), then 1 * 3 is 3, then 3 + 5 is 8. With a double, 6.0 / 4 * 3 + 5 follows the same order but gives 9.5. Whenever a result looks wrong, check both the order and the types.
Working through a longer expression
Take 20 - 3 * (2 + 4) / 9 % 4. Slow down and do one operator at a time, always picking the highest-precedence one that is ready, and the leftmost if there is a tie:
20 - 3 * (2 + 4) / 9 % 4
20 - 3 * 6 / 9 % 4 parentheses first
20 - 18 / 9 % 4 3 * 6, leftmost of * / %
20 - 2 % 4 18 / 9
20 - 2 2 % 4
18
Writing out each step like this is the most reliable way to predict what an expression prints. Do it on paper whenever the answer surprises you.
Adding parentheses for readers
Extra parentheses never hurt. If you are unsure of the rules, or the reader might be, (a * b) + c says plainly what a * b + c already means. Most programmers add them in long expressions that mix several operators.
The exercise
The starting code is Console.WriteLine(2 + 3 * 3);, which prints 11, because 3 * 3 is computed first. The goal is to print 15. The target 15 equals 5 * 3, and 5 is 2 + 3. So the addition has to run first, and parentheses around it do exactly that.
The solution in C#
Console.WriteLine((2 + 3) * 3);
Now (2 + 3) is evaluated first and gives 5, and then 5 * 3 gives 15.
Common mistakes
- Reading left to right. The expression
2 + 3 * 3does not mean "add, then multiply". Multiplication wins even though it appears later. - Misplaced parentheses.
2 + (3 * 3)changes nothing, since multiplication already runs first. The parentheses must wrap the part that should run early. - Unbalanced parentheses. Every
(needs a matching), or the code will not compile. - Using
^for powers. It compiles for integers, but it is not exponentiation.
Key takeaways
*,/and%bind tighter than+and-.- Equal levels go left to right.
- Parentheses always win, and they make intent clear even when they are not needed.
- C# has no power operator; use
Math.Pow.