roboticonx8 roboticonx8 - 1 year ago 88
C++ Question

math operation in while loop condition

i tried to do

while(n++ && x/=10);

but it returns error: lvalue required as left operand of assignment

I know it's from
but is there a possibility to do that inside the condition?

Ben Ben
Answer Source

You have a problem with operator precedence. If you look at the precedence rules, you will see that your while condition looks like this:

((n++ && x) /= 10)

and (n++ && x) is an rvalue, therefore cannot be assigned. Use brackets to remedy this:

(n++ && (x/=10))

Live example.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download