• 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

S.O.P Statement in do....while loop

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SOP COMMENTED
class Demo60
{
public static void main(String[] args)
{
int x= 10;
do {
x--;
//System.out.println("x: "+x);
} while(x< 10);
System.out.println("Hello World!");
}
}
UNCOMMENTED
class Demo60
{
public static void main(String[] args)
{
int x= 10;
do {
x--;
System.out.println("x: "+x);
} while(x< 10);
System.out.println("Hello World!");
}
}
Why is the difference in OUTPUT???
PLEASE HELP.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code is different, so output is different, commented out statements not used in compilation... Whad output do you expect from each code? Looks like you have infinite loop...
 
Sagarya Kulkarni
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In 1st Code,control comes out of the loop & prints "Hello World"
but in 2nd Code,control goes in infinite loop......Why is it so???
 
Alexander Danilou
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My guess is because first code actually doing somenting in infinite loop then some register is overflowing faster and JVM makes forced exit. Second code (commented s.o.p) uses less memory (I think).May be given some time - hours? it will still spit Hello...
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alexander Danilou wrote:My guess is because first code actually doing somenting in infinite loop then some register is overflowing faster and JVM makes forced exit. Second code (commented s.o.p) uses less memory (I think).May be given some time - hours? it will still spit Hello...



Agree. Try to change the code

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sagarya Kulkarni wrote:In 1st Code,control comes out of the loop & prints "Hello World"
but in 2nd Code,control goes in infinite loop......Why is it so???



It is actually not an infinite loop -- it is just a ridiculous large loop. The loop will keep decrementing the x value, making the x value more and more negative. This keeps going until the number underflows -- becomes positve again (and a very large number) -- and fails the condition of the do-while loop, hence, exits the loop.

The second case will also eventually print "Hello World". It just have to print all the negative numbers first.

Henry
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry is right. Try this example with a byte primitive:


cheers
Bob
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic