auouo
auouo

Reputation: 29

Why does this compile? [C++]

I tried to look for an answer online but couldn't quite find it.

Today I saw these lines of code:

    int main(){
    int n = 7;
    while(n /= 10);
    }

It doesn't make much sense, but the question was only 'will it compile?'. To which I answered no, but I was wrong.

My question is, why? Why does

    n /= 10

behave like a bool (or an int) here?

Upvotes: 1

Views: 152

Answers (4)

Francis Cugler
Francis Cugler

Reputation: 7925

What you have here for your while loop is as follows:

while ( expression );

If the expression is true or non 0 the loop will continue; otherwise if it evaluates to false or 0 it will terminate. So looking back at your original:

int n = 7;
while ( n /= 10 );

This then becomes:

while ( n = 7 / 10 ); 

Here the full expression is n = 7 / 10; This should result in 0 due to truncation of integer arithmetic. The value by implicit conversion from int to bool becomes false. As the yielded result is 0.

There is nothing here preventing this from compiling. As this is no different than having:

while ( false );

However with assignment and arithmetic operations; this may not always be the case, but in your case it is. Consider this next example: This will still compile but the loop will not terminate:

int n = 5;
while( n + n );

This will then become:

while( 5 + 5 );
...
while( 10 );
...
while( true );

Which will still compile but the loop will continue infinitely.

Upvotes: 2

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385375

Just like += and -= work, *= and /= work.

In fact, there are also &= and |=.

These all evaluate to the new value that has been assigned.

And, as you know, you don't have to put a boolean in a while/for/if condition; you only need to put something there that can be converted to a boolean.

For example, if (42), or for (char* ptr = begin; ptr; ++ptr), or while (n /= 10).

Upvotes: 0

Jaco Alberts
Jaco Alberts

Reputation: 11

C++ will convert the n /= 10 to bool. Integers = 0 converted to bool evaluates to false. Integers != 0 converted to bool evaluates to true. That while will be evaluated as while(false).

Upvotes: -1

Jerry Coffin
Jerry Coffin

Reputation: 490623

An assignment (including a compound assignment like /=) is an expression, which yields the value that was assigned1.

So, you can do something like: x = y = z = 0;, which assigns 0 to z, takes the result of that (also 0) and assigns it to y, and takes the result of that (still 0) and assigns it to x.

From there, it's making use of the implicit conversion from int to bool, in which 0 converts to false, and any non-zero value converts to true.


1. Note: that's what happens for built-in types. By convention, when/if you overload operator= for a class, you have it return *this;, so it works the same way, as a user would/will expect--but that part's not mandatory--you can overload your operator= to return a different value or an entirely different type--but this is almost always a bad idea and should usually be avoided.

Upvotes: 9

Related Questions