Start learning free

LESSON 17 OF 409 · CALCULATIONS WITH NUMBERS

Mathematical Expressions in C++: Operators, Precedence, Integer Division

Lesson Read the full explanation

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

    cout << 3 - 4 + 6; // Prints 5
    cout << 5 + 2 - 3; // Prints 4
    cout << 2 * 5 + 1; // Prints 11
    cout << 4 / 2 * 5; // Prints 10
    

  • We can also use variables:

    int num1 = 5;
    int num2 = -1;
    
    cout << 2 * num1 * num2; // Prints -10
    cout << num1 + num2 - 3; // Prints 1
    cout << 20 / num1 + num2; // Prints 3
    cout << 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

    Combining operators in one expression

    An expression in C++ can use several operators at once, and the program works out a single value from it. The arithmetic operators are +, -, *, / and % (the remainder). You can print the result of an expression directly with cout, and mix numbers and variables freely:

    cout << 3 - 4 + 6 << endl;   // 5
    cout << 5 + 2 - 3 << endl;   // 4
    cout << 2 * 5 + 1 << endl;   // 11
    cout << 4 / 2 * 5 << endl;   // 10
    
    int num1 = 5;
    int num2 = -1;
    cout << 2 * num1 * num2 << endl;   // -10
    cout << num1 + num2 - 3 << endl;   // 1
    cout << 20 / num1 + num2 << endl;  // 3
    cout << num1 / 5 * num2 << endl;   // -1
    

    The arithmetic operators bind tighter than <<, so cout << 2 * 5 + 1 prints the value of 2 * 5 + 1 without parentheses.

    Which part is calculated first

    Two rules decide the order:

    • Precedence. *, / and % come before + and -. So 2 + 3 * 4 is 14, not 20.
    • Associativity. Operators of the same level are applied from left to right. So 10 - 2 - 3 is (10 - 2) - 3 = 5, and 4 / 2 * 5 is (4 / 2) * 5 = 10.

    Parentheses override both rules: (2 + 3) * 4 is 20, and 10 - (2 - 3) is 11. Add them whenever they make the intent clearer, even if the default order would already be right.

    Integer division and the remainder

    When both operands of / are integers, the result is an integer too: the fractional part is discarded, and the result is cut toward zero.

    cout << 5 / 2 << endl;     // 2
    cout << -7 / 2 << endl;    // -3
    cout << 5 / 2.0 << endl;   // 2.5
    cout << 7 % 3 << endl;     // 1
    cout << -7 % 2 << endl;    // -1
    

    If either operand is a double, the division is done in floating point. The remainder % works on integers only, and its sign follows the left operand.

    The order matters when integer division is involved, because it throws information away at the moment it runs. 1 / 2 * 4 is 0, since 1 / 2 is already 0, while 4 * 1 / 2 is 2. Storing the result in a double does not help if the division has already happened: double avg = (3 + 4) / 2; stores 3. Make one operand a double, as in (3 + 4) / 2.0, to get 3.5.

    Common mistakes

    • Expecting 5 / 2 to be 2.5. Two integers give an integer.
    • Forgetting parentheses. To take the average of two values, write (a + b) / 2. The expression a + b / 2 divides only b.
    • Overflow. An int holds values up to 2,147,483,647. Going past that is undefined behaviour for signed integers. Use long long when a product might be large, and convert before multiplying: (long long)big * 2 is exact, where big * 2 is already too late.
    • Mixing text and arithmetic in one cout. cout << "Total: " << 4 * price + bag works, because the arithmetic is one expression. Each << separates the pieces being printed, so cout << 1 + 2 << 3 prints 33: the 3 from 1 + 2, then a separate 3.

    The exercise, step by step

    The lesson starts with price = 93 and bag = 7.

    1. Andy buys four products and one bag. Four products cost 4 * price and the bag costs bag. Multiplication goes first, so 4 * price + bag needs no parentheses: 372 + 7 = 379. Print it with cout and end the line with endl.
    2. Mary has 150 and wants two products and one bag. Her total is 2 * price + bag, which is 193. She needs to pay the difference, so subtract what she has: 2 * price + bag - 150. The multiplication happens first, then the addition and subtraction from left to right. The output is 43.
    int price = 93;
    int bag = 7;
    
    cout << 4 * price + bag << endl;
    cout << 2 * price + bag - 150 << endl;
    
    379
    43
    

    The lesson writes cout and endl without std::. In a file of your own, add #include <iostream> and either using namespace std; or the std:: prefix.

    Key takeaways

    • Multiplication, division and remainder come before addition and subtraction. Equal levels go left to right.
    • Use parentheses to change the order or to make it obvious.
    • Dividing two integers gives an integer, cut toward zero. Make one side a double to keep the fraction.
    • Check the size of your numbers: int overflow is undefined behaviour.
    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