Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

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

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 91
Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Edd,
I think the Kordal explanation makes you clear in your doubt.That's the great explanation.
 
Edd Grant
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

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

Cheers,

Edd
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!