aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes loop iterator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Groovy Fundamentals video training course this week in the Groovy forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "loop iterator" Watch "loop iterator" New topic

loop iterator

Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 164


What is the difference, if any, between:

Prints "123"


also prints "123"

Stephan van Hulst

Joined: Sep 20, 2010
Posts: 3792

Nothing. I prefer the first because I like the way it looks better.
Zeeshan Sheikh
Ranch Hand

Joined: Nov 20, 2011
Posts: 144

Well in a loop you wouldn't see this difference. The value will always increment. To understand prefix and postfix increments consider following example

static int x = 0;

public static void main(String[] args) {

System.out.println ("The value of x: " + x++); //output 0
System.out.println ("The value of x: " + x); //output 1
System.out.println ("The value of x: " + ++x); //output 2

In first output current value of x will output and then it will be incremented
In last output statement value is incremented and and 2 will output on console.

As far as your example goes I don't think there will be any difference on the output.

I hope this helps.

MySQL Blog
Fab Miredino

Joined: Apr 08, 2011
Posts: 3

Hi Glen,

Maybe it would be more clear for you, if you see the loop for as a loop while. You can always change a loop into a while loop.

Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 164

Thanks Stephan - I thought as much.

Thank you Zeeshan. I should have stated that in the OP.

Thank you Fab - thats a good idea :-)
I agree. Here's the link:
subject: loop iterator