• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

loop iterator

 
Ranch Hand
Posts: 176
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

What is the difference, if any, between:

Prints "123"

and

also prints "123"
 
Bartender
Posts: 15741
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nothing. I prefer the first because I like the way it looks better.
 
Ranch Hand
Posts: 144
MySQL Database Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 3
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 176
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 :-)
 
reply
    Bookmark Topic Watch Topic
  • New Topic