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 post/pre increment operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "post/pre increment operator" Watch "post/pre increment operator" New topic
Author

post/pre increment operator

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

How does the post and pre increment operator work when used on a for loop? The output of the following example appears to be the same



I was expecting the second loop to start from 1.

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18717
    
  40

O. Ziggy wrote:How does the post and pre increment operator work when used on a for loop? The output of the following example appears to be the same



I was expecting the second loop to start from 1.




Increment operators have a value and an effect. In both cases, the effect is the same -- the variable will be incremented. The value however, are different. With the post increment, the increment happens after, so the value is before the increment. With the pre increment, the value is after the increment.

In your example, you are not using the value -- if is simply thrown away. Hence, there is no difference in your output.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Suhas Mandrawadkar
Ranch Hand

Joined: Jul 21, 2007
Posts: 72

The iteration expression is executed after each execution of body


Regards, Suhas S. Mandrawadkar.
Certifications: SCJP 6, SCWCD 5, Oracle WebLogic Server Administrator, OCE Java EE 6 EJB Developer
Scotty Mitchell
Ranch Hand

Joined: Aug 09, 2011
Posts: 46
Suhas Mandrawadkar wrote:The iteration expression is executed after each execution of body


Look at this example and see the output difference now...


Output:
0123456789
12345678910
Suhas Mandrawadkar
Ranch Hand

Joined: Jul 21, 2007
Posts: 72

What you are printing is not iteration expression. You are printing y. Now ++y in a SOP statement will increment it before printing.
Your iteration expression is ++x, and it is still evaluating the body ten number of times

for(initialization and declaration of loop variable; condition expression; iteration expression)
{
//Printing variable x
}

Scotty Mitchell
Ranch Hand

Joined: Aug 09, 2011
Posts: 46
Suhas Mandrawadkar wrote:What you are printing is not iteration expression. You are printing y. Now ++y in a SOP statement will increment it before printing.
Your iteration expression is ++x, and it is still evaluating the body ten number of times

for(initialization and declaration of loop variable; condition expression; iteration expression)
{
//Printing variable x
}



I was agreeing with you lol..Was showing that in order to get what he wanted he'd need to have the pre increment within the body of the for loop since it executes first.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: post/pre increment operator