File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes infinite for loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "infinite for loop" Watch "infinite for loop" New topic
Author

infinite for loop

preeti dengri
Ranch Hand

Joined: Nov 30, 2000
Posts: 111
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
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
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


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: infinite for loop