• 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

Weird output

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
change --i to i-- and see the result...
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic