programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# how assignement takes place

Ranch Hand
Posts: 257
• Number of slices to send:
Optional 'thank-you' note:

What is the order of assignment to 1?
Thanks.

Ranch Hand
Posts: 2545
• Number of slices to send:
Optional 'thank-you' note:
I think the result is:
a[0] = 2

Cowgirl and Author
Posts: 1589
5
• Number of slices to send:
Optional 'thank-you' note:
howdy all,
Yuck -- what awful code! But in the true spirit of the exam ; )
Anyway, the [ ] has the highest precedence, so on the left hand side of the left-most expression,
a[ a[b]] is really a[0] because that evaluation happens BEFORE the element at a[0] is changed to have a value of 2. In other words, the FIRST thing that happens is all the array index values (the things in the brackets) are evaluated, and THEN we move all the way to the right side to start working on the assignments.
So you can think of it like:
a[0] = a[0] = 2.
So here are the steps in order:
1) a[a[b]] evaluates to a[0]
// because [ ] has highest precedence (much higher than assignment, and b is indeed 0, so the thing at a[0] is 0 at this point, since it has not been initialized to anything other than the default value for an int.
2) a[b] is evaluated to a[0], because b has a value of 0.
3) 2 is assigned to the array element a[0] , this makes the result of that assignment a '2' (remember that the result of any assignment is the value of the thing being assigned, AFTER the assignment takes place.
4) Finally, the left-most a[0] is also given a value of 2, because that's the result of the previous assignment (the one to the right).
Yikes. I probably just made it more confusing.
Bottom line: In an expression with array indexes that are not literal values, FIRST go in and resolve all of them so that you know exactly WHICH array element/position/index you are going to work with. That way, even if other parts of the expression change the values used in those array element expressions, they will have no effect on which array element is being referenced.
Cheers,
Just let me know whenever I can make anything even more confusing than it already is.
I *can* tell you that while precedence is almost never an issue on the exam, because the exam philosophy is that you should just use parenthesis and not worry about it, THIS is a special case and you SHOULD understand that the array index is resolved first. In other words, any expression inside an array index [ something in here] will be fully evaluated so that there is an actual, locked down, never-to-change index and THEN we can start doing other things...
-Kathy

"Write your code as if the next guy to maintain it is a homicidal maniac who knows where you live."

John Lee
Ranch Hand
Posts: 2545
• Number of slices to send:
Optional 'thank-you' note:
I am with you, Kathy.
1) all the assignation start from right to left;
2) all the expression follow certain rules, such as: '*' and '/' before '-' and '+',...;
3) evaluate expression first, then start assignation;
I believe I will never make a mistake on this topic.

sun par
Ranch Hand
Posts: 257
• Number of slices to send:
Optional 'thank-you' note:
Thanks Kathy, Don

Ranch Hand
Posts: 115
• Number of slices to send:
Optional 'thank-you' note:
And for an extra credit, here's an even yuckier bit of code to evaluate

John Lee
Ranch Hand
Posts: 2545
• Number of slices to send:
Optional 'thank-you' note:
y[ 0 ] = 1 ;
y[ 1 ] = 1 ;
y[ 2 ] = 1 ;

John Paverd
Ranch Hand
Posts: 115
• Number of slices to send:
Optional 'thank-you' note:
The output is:
z=1
2 1 1

John Lee
Ranch Hand
Posts: 2545
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by John Paverd:
The output is:
z=1
2 1 1

y[2] = y[z--] = ++z; // y[0] = 2 ???
I thought the next step is to evaluate y[z--] instead to assign y[2] = ++z

John Paverd
Ranch Hand
Posts: 115
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by Don Liu:

y[2] = y[z--] = ++z; // y[0] = 2 ???
I thought the next step is to evaluate y[z--] instead to assign y[2] = ++z

Are you saying that the next step should be the assignment y[2] = ++z? If I have misunderstood your question, I apologize.
Expressions are generally evaluated left to right, but the assignment operators are evaluated right to left, and have the lowest precedence. Therefore the next step in the execution of the line you quoted is to evaluate z-- in y[z--] to determine which array element will be affected by the assignment.
[ December 27, 2002: Message edited by: John Paverd ]

John Lee
Ranch Hand
Posts: 2545
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by John Paverd:
Expressions are generally evaluated left to right, but the assignment operators are evaluated right to left

That is also what I understand.
Now let me follow through this procedure:

y[++y[z++]] = y[z--] = ++z ///// y[] ={1, 2, 3}, z=0;
y[++y[0]] = y[z--] = ++z ///// y[] ={1, 2, 3}, z=1;
y[2] = y[z--] = ++z ///// y[] ={1, 2, 3}, z=1;
y[2] = y[1] = ++z ///// y[] ={1, 2, 3}, z=0;
y[2] = y[1] = 1 ///// y[] ={1, 2, 3}, z=1;
y[] ={1, 1, 1}, z=1;
Which step is wrong?

John Paverd
Ranch Hand
Posts: 115
• Number of slices to send:
Optional 'thank-you' note:
OK Don, I understand your question now. Sorry for the confusion.
y[++y[0]] = y[z--] = ++z ///// y[] ={1, 2, 3}, z=1;
++y[0] increments the element at position 0 in the array y.
You correctly determined that the result of this expression was 2, but you did not update y[0] to its new value, 2.

John Lee
Ranch Hand
Posts: 2545
• Number of slices to send:
Optional 'thank-you' note:
Thanks John.
Just when I thought I will never make a mistake...

 Consider Paul's rocket mass heater.