• 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

Infinite loop..WHY?

 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class test {
public static void main(String[] s) {
System.out.println("Start");
for(int i=0; i < 10; i = i++ ) {
System.out.println("Processing...");
}
System.out.println("End");
}
}
Can anyone explain what happens here?
i = i++?
Thanks,
Cathy.
 
mister krabs
Posts: 13974
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We have talked about the "i=i++" issue many times. Here is how it works.
1) Store the original value of i.
2) increment i.
3) assign i with the stored value (not the incremented value).
The order of operation is that the post-fix occurs before assignment but it is the original value that is assigned.
Try using "i++" instead of "i=i++".
 
Cathy Song
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Thomas.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry, sir, but I am a neophyte to Java. I still couldn't understand despite your explanation. Why is it that iteration expression 'i = i++' makes it an infinite loop?
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem in that iteration expression lies in the postfix incrementation. As Thomas tried to explain, in that particular iteration expression the value of i actually never changes.
In that assignment the post-fix incrementation increments only the value of i as it was before the assignment. I'll try explain a little better with some pseudo code:

can be read also like this:

I hope that latter example helps you
 
K Ville
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, sir. You explained it well. Now, I understand
 
Cathy Song
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maha Anna has given an amazing explanation/method here:
https://coderanch.com/t/190825/java-programmer-SCJP/certification/Array
Thanks,
Cathy.
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Cathy.. it really helped..
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow ... it was a really interesting & informative thread !
Thanks Cathy & Durga.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic