• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

About Do While

 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic