Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

working of increment operator  RSS feed

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I just saw 2 questions on the working of increment operator and, couldn't understand the output for them. Can someone please explain?

Ques 1.


output: 13:0

Ques 2:


output: 14:0

Thanks!
 
Marshal
Posts: 58345
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java arithmetic works from left to right. The [] has a high precedence, rather like (), so the i++ in the [] is evaluated first, then the i++ after the = then the 3ed i++.

Remember that i++ increments the value of i, but the whole expression returns the old value of i.

Now you should be able to work it out for yourself.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!