jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Weird output 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 "Weird output" Watch "Weird output" New topic

Weird output

James Zhang
Ranch Hand

Joined: Sep 06, 2002
Posts: 30
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;
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

Joined: Aug 05, 2002
Posts: 787
change --i to i-- and see the result...
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
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:
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

Joined: May 05, 2000
Posts: 13974
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 ]

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
James Zhang
Ranch Hand

Joined: Sep 06, 2002
Posts: 30
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

Joined: Aug 26, 2001
Posts: 7610
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 ]

[Blog] [Blogroll] [My Reviews] My Linked In
I agree. Here's the link: http://aspose.com/file-tools
subject: Weird output
It's not a secret anymore!