I was surfing the internet and I found this code:
while (n > 0)
{
sum += n % 10;
n /= 10;
}
The code itself is somewhat irrelevant, except for the operator /=
. What is that operator?
I was surfing the internet and I found this code:
while (n > 0)
{
sum += n % 10;
n /= 10;
}
The code itself is somewhat irrelevant, except for the operator /=
. What is that operator?
In a few words, the line of code
n/=10
would be equivalent ton= n/10
. These operators are like "short" versions of the times when you must use the value of the same variable that you are saving, the common case is with addition and subtraction (+=
-=
). But, that doesn't mean that those are the only short, less common, but existing versions, there are the equivalents for multiplication and division, so:a += b
It is equivalent toa = a + b
a -= b
It is equivalent toa = a - b
a *= b
It is equivalent toa = a * b
a /= b
It is equivalent toa = a / b