File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

loop iterator

 
Glen Iris
Ranch Hand
Posts: 168
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

What is the difference, if any, between:

Prints "123"

and

also prints "123"
 
Stephan van Hulst
Bartender
Pie
Posts: 4828
34
Chrome Netbeans IDE Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing. I prefer the first because I like the way it looks better.
 
Zeeshan Sheikh
Ranch Hand
Posts: 144
Java MySQL Database
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Fab Miredino
Greenhorn
Posts: 3
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 168
Chrome Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stephan - I thought as much.

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

Thank you Fab - thats a good idea :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic