permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes  when we use pre increment and when we use post increment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark " when we use pre increment and when we use post increment" Watch " when we use pre increment and when we use post increment" New topic
Author

when we use pre increment and when we use post increment

aditya chaudhari
Greenhorn

Joined: Jul 05, 2011
Posts: 10

Exactly when we use pre and post increment..??
I know the working of pre and post increment perfectly
But i want to know what exactly is the difference between them in terms of using them.
i mean when we use pre increment and when we use post increment
i googled it out but didnt got satisfactory answer( may be it was not of my understanding level answers).
This is right place where i can get my answer surly .
Please tell me .
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

aditya chaudhari wrote:Exactly when we use pre and post increment..??

Let say, you declare variable to int pos = 0; then you have to do

you declare variable to int pos = 1; then

aditya chaudhari
Greenhorn

Joined: Jul 05, 2011
Posts: 10

Thanks Seetharaman I got my answer now i will look for similar example like you
You clear my doubt
Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Here's my strategy for when to use those two similar-looking features:

I don't ever use them in a place where I do anything with the result of the incrementation (is that the word?). And that means that they are interchangeable. Both of them increment the variable they are applied to, but I don't have to know which of them does what after that.

I would recommend you do that too. Sure, for the certification exam you have to know which is which, but in the real world of programming it's much easier to do it my way.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: when we use pre increment and when we use post increment
 
Similar Threads
Regarding the previous message
Why do i get this run time eroor... plz help
For Loop Use
Doubt in char array and byte array
pre ++ or pre --