• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sagarya Kulkarni
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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.
 
Alexander Danilou
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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...
 
Welly Tambunan
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • 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

 
Henry Wong
author
Marshal
Pie
Posts: 21497
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry is right. Try this example with a byte primitive:


cheers
Bob
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic