File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "About Do While" Watch "About Do While" New topic

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");

But when i have something like this, there are no compilation errors:
2) do

Or even when i give something like this i dont get any compilation errors
3) do
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:
subject: About Do While
It's not a secret anymore!