File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes operator precedence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "operator precedence" Watch "operator precedence" New topic
Author

operator precedence

uzma Akbar
Ranch Hand

Joined: Sep 21, 2005
Posts: 40
for (int i = 0; i++ < 2
System.out.print(i);


Can anyone explain that why the output is 12. Why the 2 get printed as it should be rejected at the check and also ++ operator has higher precedence as compared to < so isn't that i is incremented first and then checked for the value.?

Please help as it sounds quite easy but sometimes easy ones are tricky
Thanks
Uzma
Philippe Saint-Just
Greenhorn

Joined: Dec 07, 2005
Posts: 15
i++ increments i after it has been evaluated.
++i increments it before it's evaluated.
Naresh Gunda
Ranch Hand

Joined: Oct 15, 2005
Posts: 163
for (int i = 0; i++ < 2
System.out.print(i);

i=0
i<2 true , postfix operator, i value becomes 1, s.o.p executed 1 is priented

i=1
i<2 true , postfix operator, i value becomes 2, s.o.p executed 2 is priented

i=2
i<2 false
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: operator precedence