It's not a secret anymore!
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "loop iterator" Watch "loop iterator" New topic
Author

loop iterator

Glen Iris
Ranch Hand

Joined: Jul 13, 2011
Posts: 164

Hi,

What is the difference, if any, between:

Prints "123"

and

also prints "123"


OCPJP 6, OCMJD
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3989
    
  18

Nothing. I prefer the first because I like the way it looks better.


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
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
http://mysqlearner.blogspot.com/
Fab Miredino
Greenhorn

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.


and
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: http://aspose.com/file-tools
 
subject: loop iterator
 
It's not a secret anymore!