This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Pre and post increment ops ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Pre and post increment ops ?" Watch "Pre and post increment ops ?" New topic
Author

Pre and post increment ops ?

mansi gupte
Ranch Hand

Joined: Dec 30, 2008
Posts: 72






please explain how the pre and post increment operators in these two seperate pieces of code function?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
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

Joined: Dec 30, 2008
Posts: 72
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

Joined: Oct 13, 2005
Posts: 38519
    
  23
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

Joined: Dec 30, 2008
Posts: 72
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

Joined: Feb 08, 2009
Posts: 605

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?


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
The problem was the original poster wasn't sure how the loop worked. It's all sorted out now
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pre and post increment ops ?