• 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 for loop

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please look to the following code
public class Q46 {
public static void main(String[]args) {
int x;
int y;
for (x = 0, y = 5 ; x < y ; x=x++)
{
System.out.println( x + " " + y );
}
System.out.println( x + " " + y );
}
}
why this one turned to be an infinite one???producing 0 5 infinite times.
please explain
thank you
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi preeti,
The problem is <code>x=x++</code> ... x is always 0! The postfix operator <code>x++</code> is evaluated first, followed by the assignment.
This code is evaluated as:
<pre>
take the value of x(0)
increase it by 1 and store in memory
assign the original value of x(0) to x, overwriting the stored value of x(1)
</pre>
Use <code>x++</code> instead.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
reply
    Bookmark Topic Watch Topic
  • New Topic