This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ++ Sign - Order of Operation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "++ Sign - Order of Operation" Watch "++ Sign - Order of Operation" New topic
Author

++ Sign - Order of Operation

Ken Pullin
Ranch Hand

Joined: Jan 29, 2001
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".
venkat_alladi
Greenhorn

Joined: Jan 31, 2001
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

Joined: Jan 25, 2001
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

Hungson Le<BR>
Ishaan Mohan
Ranch Hand

Joined: Jan 20, 2001
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

Joined: Jan 16, 2001
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.
venkat_alladi
Greenhorn

Joined: Jan 31, 2001
Posts: 12
Thanks for the correction Hungsen. I got the mistake i did with
the operators
Peter Tran
Bartender

Joined: Jan 02, 2001
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ++ Sign - Order of Operation
 
Similar Threads
increment
increment operators
Java direct Recursion
multiplication
hi i need help..please help me...