Arithmetic Operators
|
Operator |
Meaning |
Example |
Result |
* |
Multiplication |
x * y |
The product of |
/ |
Division |
x / y |
The quotient of |
% |
Modulo division |
x % y |
The remainder of the division |
+ |
Addition |
x + y |
The sum of |
- |
Subtraction |
x - y |
The difference of |
|
|
Positive sign |
+x |
The value of |
|
|
Negative sign |
-x |
The arithmetic negation of |
++ |
Increment |
++x x++ |
|
-- |
Decrement |
--x x-- |
|
The operands of arithmetic operators may have any arithmetic type.
Only the % operator requires integer operands.
The usual arithmetic conversions may be
performed on the operands. For example, 3.0/2 is
equivalent to 3.0/2.0. The result has the type of
the operands after such conversion.
Note that the result of division with integer operands is also an integer! For example:
6 / 4 // Result: 1 6 % 4 // Result: 2 6.0 / 4.0 // Result: 1.5
The increment operator
++
(and analogously, the decrement operator
--) can be placed either
before or after its
operand. A variable x is incremented (i. e.,
increased by 1) both by ++x ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access