File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Weird output

 
James Zhang
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Everybody:
I just found a very strange thing in Java. Run the following code:
public class test {
public static void main(String args[]) {
int i=0;
do{
System.out.println("value of i is"+ i);
} while (--i<0);
System.out.println("the end");
}
}
Guess what is the output? Endless loop! Can some guru explain why?
Thanks a lot!
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
change --i to i-- and see the result...
 
Dave Vick
Ranch Hand
Posts: 3244
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lobo_zhang
Welcome to the Java Ranch, we hope you�ll enjoy visiting as a regular however,
your name is not in keeping with our naming policy here at the ranch. Please change your display name to an appropriate name as shown in the policy.
Thanks again and we hope to see you around the ranch!!

As for your question, there is nothing strange about the code you posted:
do{
System.out.println("value of i is"+ i);
} while (--i<0);
you assign i the value of 0 and the only thing you do to it is to keep reducing the value of it, sot he condition that --i<0 will always be true and the loop will run endlessly. Maybe you have this confused with a for loop where the value of the conditional variable is incremented on every iteration. From the
JLS section 14.12
The do statement executes a Statement and an Expression repeatedly until the value of the Expression is false.
In this case the expression is always true. Hope that helps.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's actually not an endless loop. At some point you'll overflow and kick into positive numbers.
2147483647 is the first positive number you will get.
Run this bit of code:

[ September 06, 2002: Message edited by: Thomas Paul ]
 
James Zhang
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for all of your replies. I don't know why I was thinking that do ... while (--i<0) means that it will go out of the loop when i is negative! Anyway, it is quite helpful to present your idea here and see other people's comments.
Thanks again!
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zhang,
Thank you for changing your displayed name but it still does not comply with our naming policy.
We'd like you to read the Javaranch Naming Policy again and change your publicly displayed name (change it here) to comply with our unique rule. Thank you.
PS: Quote from the naming policy:

For your publicly displayed name, use a first name, a space, and a last name. Obviously fictitious names or improperly formatted names may be locked out.

[ September 08, 2002: Message edited by: Valentin Crettaz ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic