• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pre and post increment ops ?

 
mansi gupte
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator






please explain how the pre and post increment operators in these two seperate pieces of code function?
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that sort of code, they are very simple to understand. The expressions ++i and i++ add 1 to the value of i, and --i or i-- subtract 1 from the value of i. Very easy to understand, as long as you don't do it 2147483648 times and get overflow errors
When used inside longer statements, however, those operations especially i++/i-- cause a great deal of confusion to beginners. They increment/decrement i but the whole expression returns a value equal to the old value of i. Do a search on this forum; similar questions come up about once a week.
 
mansi gupte
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:In that sort of code, they are very simple to understand. The expressions ++i and i++ add 1 to the value of i, and --i or i-- subtract 1 from the value of i. Very easy to understand, as long as you don't do it 2147483648 times and get overflow errors
When used inside longer statements, however, those operations especially i++/i-- cause a great deal of confusion to beginners. They increment/decrement i but the whole expression returns a value equal to the old value of i. Do a search on this forum; similar questions come up about once a week.


hello,

i understand the functioning if the increment operator, what i don't understand in how will both of these kinds work in the for loop i mentioned.I need their way of operation in the for loop given in the code

 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
piya rai wrote:hello,

i understand the functioning if the increment operator, what i don't understand in how will both of these kinds work in the for loop i mentioned.I need their way of operation in the for loop given in the code

You need some older technology - pencil and paper. Write down all your values before you start, after the 1st loop, after the 2nd loop, etc.
 
mansi gupte
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell,

Doubt cleared.... i used the "older technology"(was using even before i posted) but just missed one switch case here and got lost!....thanks anyways


Piya
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did I miss a point somewhere? Both loops look exactly same to me! (Yes except for the fact that one has i++ and the other ++i). I still don't get the point, using Pre or Post increment in this manner should have no impact on the code at all?
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem was the original poster wasn't sure how the loop worked. It's all sorted out now
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic