views:

597

answers:

9
b=10;
while(a=b) {
  b--;
  if(b==-10)break;
}

B goes from 10 to -10. In my world, the while-statement, a=b, should always be true (since the assigment always "goes well"). That is not the case. When the loop stops, b will have a value of 0.

In my world, it should pass 0 and go all the way to -10, when the if-statement kicks in.

Have I misunderstood something major? (Code tested in IE8 and Adobe Acrobat)

+3  A: 

Obviously, if(0) is equivalent to if(false).

So, if(a=b) is true only if b equals to something that would become true if cast to boolean.

So, if say a and b are same type, it will be considered true if b is non-zero.

What I don't understand is why this code?

Pavel Radzivilovsky
+29  A: 
(0 = 0) == 0 == false
Jonathon
(0 = 0) == invalid assignment
dmb
+1  A: 

0 is falsy in javascript, a=b returns 0 when b reaches 0, thus javascript reads false.

Pim Jager
+9  A: 

0 is a falsey value, 0 == false is true, so as soon as b hits 0, then a = b evaluates to false and the loop breaks.

A simple fix would be

while ((a=b) > -10) {

or in this case, due to the break; statement you could just use

while (true) {
Andy E
Is falsey the official term for that? It seems like an odd word to use.
baultista
Falsy and truthy are well known descriptions of type coercion to a boolean value. I would hesitate to say "official".
Jonathon
@baultista: *"falsey"* is a made up term that is widely used around Stack Overflow, particularly in questions for dynamically typed languages like JS :-)
Andy E
+4  A: 

Yes, because the value of the assignment is that value that of the expression that is assigned. Which is 0, which is evaluated to false.

haffax
A: 

While loop expects a condition i.e. while(condition) and deals with true or false Therefore, your loop above translates to while(a) ergo the condition for the loop is the value of a.

When a reaches 0, your loop breaks.

Aditya Sehgal
+19  A: 

The value of an assignment is the value being assigned, so you can chain assignments a = b = 2 for example, so when b=0, the value of a=b is 0.

Brian Young
+1 This is the important thing that was not made clear in most answers. The "value" of an assignment operation is the value being assigned, NOT something that would tell if the operation "goes well".
PauliL
+1  A: 

the while-statement, a=b, should always be true (since the assignment always "goes well").

It's generally not a good idea to think of the return value as confirmation of the success of the operation. The continued execution of your code is the indication that the assignment went well.

Notably, your intuition is actually backwards from the standard in Unix shell scripting, where 0 means A-OK! and non-zero indicates some other result.

Now that things like Exceptions can be used to indicate a problem in execution, there are better mechanisms to use for this.

Alex Feinman
A: 

In fact 0 = 0 will give you a result of 0 which is interpreted as false. In other words while(0=0) is pretty much the same as while(0) which breaks the loop.

Alej