Start learning free

LESSON 19 OF 347 · CALCULATIONS WITH NUMBERS

Operator Precedence in C#: Which Operator Runs First

Lesson Read the full explanation

TL ; DR:

  • When we string operations together, C# must know which one to do first. This is called operator precedence.


  • * and / are performed before + and -:

  • Console.WriteLine(1 + 2 * 3); // Prints 7
    Console.WriteLine(6 - 4 / 2); // Prints 4
    Console.WriteLine(5 * 4 - 2 / 2); // Prints 19
    

  • We can add () to force an operation to be performed first:

    Console.WriteLine((1 + 2) * 3); // Prints 9
    Console.WriteLine((6 - 4) / 2); // Prints 1
    Console.WriteLine(5 * (4 - 2) / 2); // Prints 5
    


Full lesson:

When we string operations together, C# must know which one to do first. This is called operator precedence.

This is the hierarchy from highest precedence to lowest precedence:

  • Parentheses are always respected
  • Exponentiation (raise to a power)
  • Multiplication, Division and Remainder
  • Addition and Subtraction
  • Left to right


First example:

Console.WriteLine(1 + 2 * 3); // Output: 7

Multiplication is executed before addition, so:

1 + 2 * 3 = 1 + 6 = 7

Second example:

Console.WriteLine((1 + 2) * 3); // Output: 9

What's inside parentheses is executed before multiplication, so:

(1 + 2) * 3 = 3 * 3 = 9

Third example:

Console.WriteLine(6 / 4 * 3 + 5); // Output: 9.5

Multiplication and division come before addition. Also, multiplication and division have the same priorty so they are executed from left to right:

6 / 4 * 3 + 5 = 1.5 * 3 + 5 = 4.5 + 5 = 9.5

Forth example:

Console.WriteLine(6 / ((4 + 2) / 3)); // Output: 3

Computer sees parentheses so it looks at it like this:

6 / ((4 + 2) / 3) = 6 / x, where x = (4 + 2) / 3

I have to compute x first. Parantheses are executed before division:
(4 + 2) / 3 = 6 / 3 = 2

I can replace x with 2:
6 / ((4 + 2) / 3) = 6 / 2 = 3


Assignment
Follow the Coding Tutorial and let's practice with operator precedence!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    C# · CALCULATIONS WITH NUMBERS

    Operator Precedence in C#, explained

    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

    1. Parentheses are always evaluated first.
    2. Unary operators such as the minus in -x.
    3. Multiplication, division and remainder: *, /, %.
    4. 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 * 3 does 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.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free