Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

++ Sign - Order of Operation

Ken Pullin
Ranch Hand
Posts: 43
Hi - could someone please explain to me how this gets evaluated. Thanks for your help.
public class TestClass
{
public static void main (String args[ ] )
{
int k = 1;
int i = ++k + k++ + + k ;
System.out.print(i+ " "+ k) ;
}
}
The answer is "7 3".

Greenhorn
Posts: 12
The precedence is
Postfix ++, Prefix ++, Unary +
Hence the expression will be evaluated as
i = ++k + k++ + +k; k = 1
i = ++k + 2 + +k; k = 2
i = 3 + 2 + +k; k = 3
i = 3 + 2 + 3; k = 3
i = 7; k = 3
Hope this helps

Hungson Le
Ranch Hand
Posts: 32
venkat,
3+2+3 = 8 (not 7) =)
Please correct me if I'm wrong: postfix & prefix ++ has the same precedence which is higher than +, and expressions are evaluated left to right.
int i = ++k + k++ + +k ;
i = 2 + k++ + +k; k=2 postfix
i = 2 + 2 + +k; k=3 prefix
i = 2 + 2 + 3; k=3
Hungson Le

Ishaan Mohan
Ranch Hand
Posts: 115
Hi,
this is a explanation given by Maha Anna I hope this will help you .
1. wherever you find the var just substitute the value and when you find ++/-- operator irrespective of post/pre increment/decrement (++/--) blindly increment/decrement the latest value of that var.. You have to start from left to right. So let's start
int i=5; array[i++]=++i+i++;array[5(6)] = (7)7 + 7(8);[/b](i) for i substitute the latest value of i(ii) for ++ symbol, increment the latest value(iii) for -- symbol, decrement the latest value(iv) Finally do the calculation for all non-bracketed values(v) Be careful when the assigned var is the same as the var which undergone all these ++/-- operations in the statement. In this case the assigned var takes its latest value int i = 10; ex. i = i++ + --i; i = 10(11) + (10)10; So i = 10 after exe of above stmt (vi) In order to do the arithmetic, justcut-down all the bracket values array[5] = 7 + 7; //Note here i's latest value is 8 array[5] = 14;(vi) Since all the elements of an int[] has default value 0,array[6] is 0. System.out.println(array[5]+" "+array[6]+" "+i); So it prints [b] 14 0 8 Another example int i=10; int j = i++ + i + --i + ++i + i; j = 10(11) + 11 + (10)10 + (11)11 + 11; j = 10 + 11 + 10 + 11 + 11; // i's latest value = 11 here j = 53;
Final answer i=11 and j=53

natchit
Greenhorn
Posts: 29
int i = ++k + k++ + +k ;
Ok. lets expand this expression to its literal meaning. Operands in an expression are evaluated first irrespective of precedence.
int i = (++(k=k)) + (k=k)++ + +(k=k);
Now the value of k is inserted from left to right.
So

(++(k=k)) is evaluated first which returns 2 and then that value is used in the expression.
Next term, (k=k)++, first k=2 is used in the expression and then k incremented. So k=3 and the current total value of the expression is 4.
Last, +k is evaluated which is obivously just +3.
So, the result is 7.
venkat, post/pre fix precedence rules cannot be applied here because, there is only + operation here through out the expression.

Greenhorn
Posts: 12
Thanks for the correction Hungsen. I got the mistake i did with
the operators

Peter Tran
Bartender
Posts: 783
You all need to check out Maha Anna's excellent discussion regarding this issue. Read it...Try it...Memorize it...
http://www.javaranch.com/ubb/Forum24/HTML/000775.html
-Peter