• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About Do While

 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you for some reason want to do nothing (at least once) while some condition is true you can write
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic