• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

for loop(i++ / ++i)

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought that i know the concept of pre fix n post fix operators but I got confused once i saw ++i written in a for loop and working exactly the same way as i++.
Please clarify ,how it behaves in the for loop
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by itisha jain:
I thought that i know the concept of pre fix n post fix operators but I got confused once i saw ++i written in a for loop and working exactly the same way as i++.
Please clarify ,how it behaves in the for loop


It works the same as anywhere else. The value of the i++ expression is not used; only its effects.

Unrolling the logic of that loop, we get the following:

int i = 0;
if (i < 3) System.out.println(i); // 0
i++; // this expression evaluates to 0, but i is incremented to 1
if (i < 3) System.out.println(i); // 1
i++; // evaluates to 1; i is incremented to 2
if (i < 3) System.out.println(i); // 2
i++; // evaluates to 2; i is incremented to 3
if (i < 3) // it isn't, so the loop breaks

Hope this helps.
 
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Itisha,
As far as I Think there is no difference in post-increment and pre-increment or post-decrement and pre-decrement if this statement is all alone, ie. not combined with any kind of assignment.

In case there is assignment with these operators the result might be different


hope this will help you
 
itisha jain
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks steve,
 
Bartender
Posts: 1205
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The ++i idiom may be a holdover from early C programming days.

As we all know, i++ "returns" the value of i BEFORE the increment, while ++i has the NEW value of i. In pseudo-assembly, i++ might look like this:


Whereas ++i can remove the use of registerB to hold the non-incremented value of i:


In both cases, the expression's value is left in registerA, ready to be assigned to a new variable, passed to a function, etc.

Soon, however, compilers became smart enough to recognize situations where the value of the i++ expression was being ignored, so they didn't bother saving the original value, thus making i++ just as efficient as ++i.

However, some programmers got into the habit of always using ++i and found no reason to stop. You never know, after your current Java programming job, you might start working with a new language that doesn't have the optimization, and a habit of using "++i" will serve you well.

And lastly, if you're reading code out loud, it's easier to say, "increment i," when you see ++i than when you see i++.
[ October 04, 2005: Message edited by: Ryan McGuire ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic