aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes S.O.P Statement in do....while loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "S.O.P Statement in do....while loop" Watch "S.O.P Statement in do....while loop" New topic
Author

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

Sagarya Kulkarni
Greenhorn

Joined: Jun 09, 2009
Posts: 4
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

Joined: May 08, 2009
Posts: 28
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

Joined: Jun 09, 2009
Posts: 4
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

Joined: May 08, 2009
Posts: 28
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

Joined: Aug 18, 2008
Posts: 40
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Bob Wheeler
Ranch Hand

Joined: Apr 24, 2009
Posts: 317
Henry is right. Try this example with a byte primitive:


cheers
Bob


SCJP 6 - SCJD - SCWCD 5 - SCBCD 5
JavaEnterpriseEditionFaq - TomcatFaq
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: S.O.P Statement in do....while loop