• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help: Oeperator Associativity 2?

 
Ruff Young
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question below is at operators in Dan's Topic exam. The output is 1. The question looks so tricky. How do I group it? And could you give me some explanation of it in detail?
Each time execute part caculation, the variable 'i' is being changed? I think so.
Some my approch,
a compound assignment expression of the form E1 op= E2 can be rewritten as E1=(T)((E1)op(E2)) where T is the type of E1.
so i = i + (~i - -i * ++i + i-- % ++i * i++)
then i = 1 + (~i - -i * ++i + i-- % ++i * i++)
...
class F {
public static void main (String []s) {
int i = 1;
i += ~i - -i * ++i + i-- % ++i * i++;
System.out.print(i);
}
}
Thank you for your help?
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With the following information you might be able to find out the solution:
Operator precedence
How does the ++ operator work?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may help:My analysis of some expressions.
Your one is there too.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following class provides a sequence of simplified versions of the expression. Each print statement prints the same result.


I hope the above helps.
 
Ruff Young
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really appreciate you explain kindly. I am planning to take the exam in next week.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic