There are three things that might be suspicious for you if you are a C++ beginner:
First thing might be operator /=
, which combines an integral division (i.e. without remainder), with an assignment. So x /= 10
actually is the same as x = x / 10
.
Second, each expression in C++ has – after having been evaluated – a value. For an assignment like (x = 0)
, the result is the value of x
after the assignment, i.e. 0
in this case.
Third, a condition like if (x) ...
with x
being of integral type has in C++ the same meaning as if(x != 0)
, i.e. it is false
if x
equals 0
, and it is true
if x
is anything else but 0
.
All together: while ( x /= 10 )
means assign x
the value of an integral division by 10
and then compare the value to 0
. If 0
is reached, the loop ends.
BTW: length
starts with 1
, because any number, even 0
, comprises at least one digit.