roboticonx8 roboticonx8 - 2 months ago 12
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
x/=10
but is there a possibility to do that inside the condition?

Ben Ben
Answer

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.

Comments