• 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

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"
 
Saloon Keeper
Posts: 15705
367
  • 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 :-)
 
Dinner will be steamed monkey heads with a side of tiny ads.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic