Examples
x = y * (z - 1) / 2;
num = (num + 1) % maximum;
++counter;
total += 5;
Points to Note
- Arithmetic in C++ is similar to that in most other languages:
+
and -
do addition and subtraction, *
and /
do multiplication and division, %
does division yielding the remainder
*
and /
take precedence over +
and -
, as you would expect; brackets can be used to control the order of evaluation
- Division of two integers in C++ yields an integer result!
++
(two plus signs) can be used to increment a variable's value by 1; --
(two minus signs) can be used to decrement its value by 1
+=
combines addition with assignment, so that total += 5
is short for total = total + 5
-=
, *=
and /=
operators are also available