FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


evaluating from right to left?





badai
we are being told that if the operators has the same precedent, it will be evaluated from left to right.

let say:

a = 1;
b = 2;
c = 3;

now do this:
a = b = c

you will get a = 3 and b = 3.

by right it should be evaluated like this:
(a = b) = c
which a =b then b = c

why it is like this?
Peterssidan
You haven't told us what language but in C++ it can be Left-to-right or Right-to-left depending on what operator. If you look at the associativity to the right of this page you see that the assignment operator is Right-to-left.

That means a = b = c will be treated as a = (b = c) and give you a=3, b=3 and c=3 as you said. This on the other hand: (a = b) = c would give you a=3, b=2 and c=3 because (a = b) returns a reference to a so it is a that is assigned the value of c.
badai
i tried it in PHP, javacript and C++

btw, can't really understand your explanation.
amagard
In Python something like ( a = b ) = c doesn't work at all:

Code:
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 1
>>> b = 2
>>> c = 3
>>> a = b = c
>>> a
3
>>> b
3
>>> c
3
>>> a = 1
>>> b = 2
>>> (a = b) = c
  File "<stdin>", line 1
    (a = b) = c
       ^
SyntaxError: invalid syntax
>>>
Peterssidan
Yes, assignment seems to be right-to-left in PHP and JavaScript too so it is never treated as (a = b) = c.

Actually (a = b) = c is not even allowed in most languages (C, PHP, Java, JavaScript, ...).
manfer
The = assigment operator has right to left associativity. When you write a = b = c is just an abreviation of a=c and b=c.

You must differentiate:

1.- Precedence (the order in which operations are made)

5 + 6 * 2 where the * operation is done first because it has higher precedence

2.- Associativity (left to right or right to left)

Direction in which the expression is evaluated. Which is not the same for all operators.

5+4+3 evaluates to 9+3 that evaluates to 12 (+ - * / has all of them left to right associativity)

a = b = 3 (a=3 and b=3, = has right to left associativity, and it is not the only one, all unary operators and all assigment operators has right to left associativity if I'm not wrong)

(a=b) = c is not valid code. In javascript you will get the "invalid assignment left-hand side" ReferenceError that just indicates that the left side is not valid because the left side of an assigment operation must be always a variable which is not true in that code where the left side is a expression (a=b) and not a variable.

Just as another example a = 3 = 5 will produce the same error because we again have an assigment operation where the left side is not a variable 3 = 5

A good example of the right to left associativity could be:
Code:

var a = 5;
var b = 10;
a -= b += 13;
console.log("New value of b is " + b);
console.log("New value of a is " + a);
venkat_60
while compiling and giving out the result the system reads the code from left to right , so first it reads a=b and then b=c .
So, all a,b&c become equal to c's value .... !!!! Wink
badai
manfer wrote:
a = b = 3 (a=3 and b=3, = has right to left associativity, and it is not the only one, all unary operators and all assigment operators has right to left associativity if I'm not wrong


see... someone managed to explain it in plain words. no example, no cryptic description. just plain "= has right to left associativity". this is what i'm looking for.

just hope he doesn't made it up.
manfer
Peterssidan wrote:
Yes, assignment seems to be right-to-left in PHP and JavaScript too so it is never treated as (a = b) = c.

Actually (a = b) = c is not even allowed in most languages (C, PHP, Java, JavaScript, ...).


In this post by @Peterssidan you have good links to the actual official documentation of PHP and Javascript about precedence and associativity.
Arrogant
Well there are operators with right-to-left or left-to-right associativity.
Some are interpreted from the left and some from the right
the associativity operators are right-to-left operators and so the results are a= 3, b= 3
Related topics
Marquee ~ it's really fun!
Enemy Press
DO YOU CHEAT?
My short (uncompleted) story; The Heros Sword: A New Legacy
The Middle East Conflict
right or left?
[man] Creacion de una pagina simple en html
describe your dream home
Ragnarok Games Guide: Wizards/Mages
qscomputing.net - my new site
HTML Tutorial.
Need some help with IE fix
Are you left-handed?
3D Worm in JavaScript!!!
Accurate Left/Right Brain Test?
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.