wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes prefix increment operator doesn't increment until after for loop has completed 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 "prefix increment operator doesn Watch "prefix increment operator doesn New topic
Author

prefix increment operator doesn't increment until after for loop has completed

Edd Grant
Greenhorn

Joined: Jul 08, 2009
Posts: 4
Hi All,

I came across this tonight, it seems that when using the prefix increment operator in a for loop, the variable incremented by the operator is not incremented until after the for loop iteration - this makes it function exactly like the postfix increment operator which has left me somewhat confused!

The code below demonstrates this (albeit in a slightly irrelevant Thread):



This code produces the following output:

Using prefix increment operator: 0
Using prefix increment operator: 1
Using prefix increment operator: 2
Using postfix increment operator: 0
Using postfix increment operator: 1
Using postfix increment operator: 2

I was expecting it to output the following:

and produces the following output:
Using prefix increment operator: 1
Using prefix increment operator: 2
Using postfix increment operator: 0
Using postfix increment operator: 1
Using postfix increment operator: 2

Can anyone tell me what's going on here? Is my understanding of the prefix operator just plain wrong? Or does the prefix operator really work in the same manner as the postfix in a for loop? If this is the case are there any other times when it works in this manner (especially looking for ways in which this will appear in the SCJP exam)?

Very grateful for some insight here.

Cheers,

Edd
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Your understanding is wrong.


(where x, y and z are expressions)
is equivalent to:


'z' is always executed at the end of each loop iteration, after 'block' is executed and before checking 'y' at the start of next iteration.
Regardless of z is z++ or ++z - both will give the same results.
Ulrich Vormbrock
Ranch Hand

Joined: Apr 15, 2010
Posts: 73
Hi Edd,

the answer is quite simple:
"After each execution of the body of the for loop, the iteration expression is executed"
(from K&B SCJP 6, page 347)

Your iteration expression is ++i or i++ - if post- or pre-increment, it doesn't matter, because the iteration expression is always evaluated after the loop body runs!

Cheeky as I am, I've written the following piece of code:


This produces the following output:

Using prefix increment operator: 0 - for-loop: 1
Using prefix increment operator: 1 - for-loop: 2
Using prefix increment operator: 2 - for-loop: 3

Using postfix increment operator: 0 - for-loop: 0
Using postfix increment operator: 1 - for-loop: 1
Using postfix increment operator: 2 - for-loop: 2


The output marked in red represents the result concerning PRE-increment - you see, that the iteration expression "System.out.print..." is always executed last!

Hope this helps!


SCJP 6 (88%), SCWCD (89%)
sumit kothalikar
Ranch Hand

Joined: Apr 15, 2010
Posts: 91

Your logic is somewhat incorrect because here post or pre increment does no matter in your code.
Why you are using Thread in your code it is redundant.


Thanks & Regards
Sumit Kothalikar
Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
Hi Edd,
I think the Kordal explanation makes you clear in your doubt.That's the great explanation.


Cheers Munees
My Blog
Edd Grant
Greenhorn

Joined: Jul 08, 2009
Posts: 4
Hi All,

Thanks for the great responses, they were very clearly and simply explained - can't believe I'd missed that.

Cheers,

Edd
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: prefix increment operator doesn't increment until after for loop has completed
 
Similar Threads
Loops
loop iterator
Threads: synchronizing a block of code (K&B 1.5)
Dissecting the post-fix operator
Post increment and the assignment operator