Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

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

Edd Grant
Greenhorn
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
Posts: 423

(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
Posts: 73
Hi Edd,

"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!

sumit kothalikar
Ranch Hand
Posts: 91
Your logic is somewhat incorrect because here post or pre increment does no matter in your code.

Muneeswaran Balasubramanian
Ranch Hand
Posts: 138
Hi Edd,
I think the Kordal explanation makes you clear in your doubt.That's the great explanation.

Edd Grant
Greenhorn
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