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-. So2 + 3 * 4is14, not20. - Associativity. Operators of the same level are applied from left to right. So
10 - 2 - 3is(10 - 2) - 3 = 5, and4 / 2 * 5is(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 / 2to be2.5. Two integers give an integer. - Forgetting parentheses. To take the average of two values, write
(a + b) / 2. The expressiona + b / 2divides onlyb. - Overflow. An
intholds values up to 2,147,483,647. Going past that is undefined behaviour for signed integers. Uselong longwhen a product might be large, and convert before multiplying:(long long)big * 2is exact, wherebig * 2is already too late. - Mixing text and arithmetic in one
cout.cout << "Total: " << 4 * price + bagworks, because the arithmetic is one expression. Each<<separates the pieces being printed, socout << 1 + 2 << 3prints33: the 3 from1 + 2, then a separate 3.
The exercise, step by step
The lesson starts with price = 93 and bag = 7.
- Andy buys four products and one bag. Four products cost
4 * priceand the bag costsbag. Multiplication goes first, so4 * price + bagneeds no parentheses:372 + 7 = 379. Print it withcoutand end the line withendl. - Mary has 150 and wants two products and one bag. Her total is
2 * price + bag, which is193. 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 is43.
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
doubleto keep the fraction. - Check the size of your numbers:
intoverflow is undefined behaviour.
