In this question,I’m asking how the following snippets work, as it involves weird use of variable:
while (+(+i--)!=0)
{
i-=i++;
}
console.log(i);
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Interesting problem… you’ve tagged it Java, JavaScript and C — note that while these languages have the same syntax, this question involves very subtle semantics that may (I’m not sure) differ across languages.
Let’s break it down:
The
--postfix decrement operator is most tightly bound. So this is equivalent to+(+(i--)). That is therefore equivalent to the value of+(+i)(that is,i), but it also decrementsiafter taking the value. It compares the value with 0 to see if the loop should continue. Thus,while (+(+i--)!=0)is equivalent to the following:Note that it also performs
i--at the end of the loop.Inside the loop, I believe you have some undefined behaviour, at least in C, because you are referencing
ion the right, and also updatingion the left — I believe that C doesn’t define which order to do that in. So your results will probably vary from compiler to compiler. Java, at least, is consistent, so I’ll give the Java answer.i-=i++is equivalenti = i - i++, which is equivalent to to reading all the values out of the expression, computing the result of the expression, applying the post-increment, and then assigning the result back. That is:Clearly, this is the same as writing
i = 0. So the body of the loop sets i to 0.Thus, the loop executes just one time, setting i to 0. Then, it decrements
ione more time on the next while loop, but fails the check becausei(before decrementing) == 0.Hence, the final answer is
-1, no matter what the initial value foriis.To put this all together and write an equivalent program:
When I tried it in Java and JavaScript, that’s what I got. For GCC (C compiler), it gives -1 only when
istarts out as 0. Ifistarts out as anything else, it goes into an infinite loop.That’s because in GCC (not necessarily all C compilers),
i-=i++has a different meaning: it does the store back toifirst, then does the post-increment. Therefore, it is equivalent to this:That’s equivalent to writing
i = 1. Therefore, on the first iteration, it setsito 1, and then on the loop, it checks whetheri == 0, and it isn’t, so it goes around again, always settingito 1. This will never terminate, but for the special case whereistarts out as 0; then it will always terminate the loop and decrementi(so you get-1).Another C compiler may choose to act like Java instead. This shows that you should never write code which both assigns and postincrements the same variable: you never know what will happen!
Edit: I tried to be too clever using that
forloop; that wasn’t equivalent. Turned back into a while loop.