2 ++ 2
is interpreted as:
2 ++ 2 == 2 + (+2)
So you perform an addition between 2
and +2
where the second +
is thus an unary plus. The same happens if you write 2 +++ 2
:
2 +++ 2 == 2 + (+(+2))
For the 4 -- 2
case something similar happens:
4 -- 2 == 4 - (-2)
So you subtract -2
from 4
resulting in 6
.
Using two, three (or even more) additions is not prohibited, but for integers/floats it only results in more confusion, so you better do not do this.
There are class
es that define their own unary plus and unary minus operator (like Counter
for instance). In that case ++
can have a different behaviour than +
. So you better do not use ++
(and if you do, put a space between the two +
ses to make it explicit that the second +
is a different operator).
Since there are unary plus and minus operators, anything after the first +
or -
is interpreted as unary. So 2 ++--++- 2
will result in 0
since:
2 ++--++- 2 == 2 + (+(-(-(+(+(-2))))))