Start learning free

LESSON 17 OF 347 · CALCULATIONS WITH NUMBERS

Mathematical Expressions in C#: Combining Operators

Lesson Read the full explanation

  • We can perform multiple calculations using operators in the same line of code:

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

  • We can also use variables:

    int num1 = 5;
    int num2 = -1;
    
    Console.WriteLine(2 * num1 * num2); // Prints -10
    Console.WriteLine(num1 + num2 - 3); // Prints 1
    Console.WriteLine(20 / num1 + num2); // Prints 3
    Console.WriteLine(num1 / 5 * num2); // Prints -1
    



Assignment
Follow the Coding Tutorial and let's practice with mathematical expressions!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check

    C# · CALCULATIONS WITH NUMBERS

    Mathematical Expressions in C#, explained

    What an expression is

    An expression is any piece of code that produces a value. 3 - 4 + 6 is an expression, and so is 2 * num1 * num2. You can chain as many operators as you like in one expression, mixing numbers and variables, and C# works out a single result that you can print, store or use in a larger expression.

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

    The same works with variables:

    int num1 = 5;
    int num2 = -1;
    
    Console.WriteLine(2 * num1 * num2);  // -10
    Console.WriteLine(num1 + num2 - 3);  // 1
    Console.WriteLine(20 / num1 + num2); // 3
    

    In the last line, 20 / num1 is 4, and adding num2 (which is -1) gives 3.

    The order of evaluation

    Two rules decide how an expression is evaluated. Multiplication, division and remainder are done before addition and subtraction. Operators of the same level are done from left to right. So 2 * 5 + 1 multiplies first and gives 10 + 1, and 3 - 4 + 6 is (3 - 4) + 6, which is 5. The next lesson covers precedence in detail.

    Integer division inside expressions

    When both sides of / are int, the result is an int and the fractional part is dropped. This matters in longer expressions because the cut happens at that step:

    int num1 = 5;
    Console.WriteLine(num1 / 5 * -1);  // -1
    Console.WriteLine(7 / 2 * 2);      // 6, not 7
    Console.WriteLine(7 * 2 / 2);      // 7
    

    7 / 2 * 2 evaluates 7 / 2 first, which is 3, and then multiplies by 2. Reordering to 7 * 2 / 2 keeps the precision, because the multiplication happens before the division. If you need the decimal part, make one operand a double, as in 7.0 / 2.

    Parentheses when you want a different order

    If the default order is not what you mean, parentheses override it. Whatever is inside them is evaluated first:

    Console.WriteLine(2 + 3 * 4);    // 14
    Console.WriteLine((2 + 3) * 4);  // 20
    

    You can also add parentheses that don't change the result, just to make a long expression easier to read. Both 4 * price + bag and (4 * price) + bag are correct.

    Negative numbers and variables

    A minus sign in front of a value, as in int num2 = -1;, is the unary minus operator and it binds tighter than the arithmetic around it. In 2 * num1 * num2 the variable holds -1, so the result is negative. Be careful with num1 - -1, which means five minus negative one, or 6. Put a space between the two minus signs: written without it, -- is the decrement operator, which is a different thing and only works on a variable.

    The exercise

    The editor gives you two variables: price (the cost of one product, 93) and bag (the cost of one bag, 7). You print two totals.

    1. Andy buys four products and one bag. The total is four times the price plus the bag: 4 * price + bag. Multiplication runs first, so no parentheses are needed. The result is 372 + 7 = 379.
    2. Mary wants two products and one bag, and she has 150. Her total is 2 * price + bag, which is 193. Subtracting the money she already has gives how much more she needs: 2 * price + bag - 150. The multiplication happens first, then the addition and subtraction go left to right, so the result is 43.

    The solution in C#

    int price = 93;
    int bag = 7;
    
    Console.WriteLine(4 * price + bag);
    Console.WriteLine(2 * price + bag - 150);
    

    It prints 379 and then 43, one per line.

    Common mistakes

    • Missing the multiplication sign. Mathematical notation lets you write 4price, but C# requires 4 * price.
    • Adding before multiplying. 4 * price + bag is not 4 * (price + bag). The second one buys four bags too.
    • Hard-coding the numbers. Use the variables, not 93 and 7, so the code still works if the starting values change.
    • Forgetting the second print. Each of the two answers needs its own Console.WriteLine.

    Checking your work

    Before you run code, evaluate it by hand: replace the variables with their values and apply the order rules. For Mary, 2 * 93 + 7 - 150 becomes 186 + 7 - 150, then 193 - 150, then 43. If the output differs from your hand result, one of the two has a mistake, and finding which one is how you learn the rules.

    Key takeaways

    • An expression can combine numbers, variables and several operators.
    • *, / and % run before + and -; equal levels run left to right.
    • Integer division discards the fraction at the step where it happens.
    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