jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes About Do While 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 "About Do While" Watch "About Do While" New topic
Author

About Do While

Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
Can anyone explain these,
why its giving compilation error:
1) do
// System.out.println("hii");
while(true);

But when i have something like this, there are no compilation errors:
2) do
System.out.println("hii");
while(true);

Or even when i give something like this i dont get any compilation errors
3) do
//System.out.println("hii");
while(true);
while(false);
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
The key to this problem is that there must be one statement between do and while(...);

In the first, there is no statement.

in the second case, there is one print statement.

In the third case, "while(true);" is treated as a while loop with an empty statement as the body, so "while(true);" is the one statement and "while(false);" is the rest of the do statement.


Mike Gershman
SCJP 1.4, SCWCD in process
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
If you for some reason want to do nothing (at least once) while some condition is true you can write


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About Do While