Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8661167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:26:00+00:00 2026-06-12T16:26:00+00:00

While writing another do … while loop in Java, I started thinking about its

  • 0

While writing another do ... while loop in Java, I started thinking about its syntax. A typical programmer will write something like

do {
  somethng();
}while(booleanValue);

However, Java has a built-in way of compressing this by allowing the programmer to remove the braces when only one line is inside the loop (this is typical for Java expressions):

do
  something();
while(booleanValue);

This can be thought of and written as

do something();
while(booleanValue);

or even as

do;
while(booleanValue);

This is quite interesting. This brought my attention to the fact that this is a Java statement that must be read and run spanning two lines, meaning that after the do line is run, then the operation is not complete until the while line is run. Remember that other expressions are only one line:

if(booleanValue) something();
while(booleanValue) something();
for(Object declaration; booleanValue; operation()) something();
for(Object declaration : iterableObject) something();
throw throwableObject;
return anyObject;
switch(integer) {}
//case is excluded because it operates a lot more like a goto operation than a statement

So I started thinking, not about why this is, but about how to compress this statement a bit more.

Assuming that a “line” is anything that’s terminated in a semicolon (;) or contained within braces ({ and }), let’s go into this with this knowledge: a do statement necessarily requires two lines to be run and a do statement is must continue running at least until it reaches an empty while statement.

So why ever use braces?

Let’s look at some scenarios with this syntax. How about a do...while statement with one enclosed statement:

do something();
while(booleanValue();

Alright, nothing new, here. What about do...while statement with three enclosed statements:

do
  statement1();
  statement2();
  statement3();
while(booleanValue);

Here, Java will see that it is a do statement, and it will run lines 2, 3, and 4 before seeing the empty while statement. At this point, it knows we’re ready to end the loop, evaluate the expression, and possibly return to the do or exit the loop.

How about something that you might think breaks it; a nested while loop:

do
  statement1();
  while(booleanValue1)
    statement2();
while(booleanValue2);

Here, Java sees it’s a do statement and enters it, then runs line 2 normally. At line 3, it sees that it’s a while statement. At this point it must decide whether this is the end of the loop. It inspects the while statement and discovers that it is not empty, and therefore not the end of the do loop. It enters the while statement and loops lines 3 and 4 until it doesn’t anymore. It then sees another while statement and, upon inspection, sees that it is empty and, therefore, the end of the do statement. It evaluates it and may or may not return to line 2, but we don’t care right now.

But Supuhstar, that sure seems like a lot of calculation! Won’t that slow down the JVM?

Not necessarily! This can all be done at compile time, much like Java determines if any other statement listed above is empty (for instance, trying to compile while(true); will result in the next line compiling with the error “unreachable statement“).

So, much like

for(Object declaration : iterableObject)
  somethingInvolving(declaration);

compiles into

Iterator<Object> i = iterableObject.iterator();
while(i.hasNext())
{
  Object declaration = i.next();
  somethingInvolving(declaration);
}

then

do
  statement1();
  while(booleanValue1)
    statement2();
while(booleanValue2);

could compile into

do
{
  statement1();
  while(booleanValue1)
  {
    statement2();
  }
}
while(booleanValue2);

But Supuhstar, Java doesn’t care about indentation! Can yours be written with any indentation, or none at all?

Certainly! The compiler would just as easily compile

do
  statement1();
  while(booleanValue1)
    statement2();
while(booleanValue2);

as it would

do statement1(); while(booleanValue1) statement2(); while(booleanValue2);

and both of these would do exactly the same thing.

Does anyone else agree that this would be an okay thing to include as Java syntax, or is there some glaring reason I’m missing that this cannot be done?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:26:01+00:00Added an answer on June 12, 2026 at 4:26 pm

    This particular statement struck my as slightly confused:

    It inspects the while statement and discovers that it is not empty,
    and therefore not the end of the do loop.

    Because what do you mean by “not empty”? while-loops are allowed to be “empty”:

    do
      statement1();
    while(booleanValue1);
    while(booleanValue2);
    

    That would be valid under your plan even though the first while-loop would close the do-while instead of the second.

    That being said, the grammar you are suggesting would not be ambiguous I don’t think …. but doesn’t matter because it would never be introduced because the language designers are not (and never have been) interested in making Java a cleaner or more terse language.

    You could even say verbosity is fundamental part of the Java philosophy. There many things the compiler could infer or allow but doesn’t. The good news is there are many more modern languages like Scala that allow you to write more brief, expressive code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sometimes while writing Java in Eclipse, I write code that generates warnings. A common
While writing yet another class full of methods that access database tables to work,
I've managed to open a file and read while writing to another file with
I am writing two applications: one to write data to a file and another
As an educational project, I'm writing (yet another) editor-style live syntax highlighter in JavaScript.
I am writing a script that calls another java program, to perform some tasks.
I've run into another issue while writing this code (with MUCH help from programmers
While writing an encryption method in JavaScript, I came to wondering what character encoding
While writing some C code, I decided to compile it to assembly and read
While writing code in a file that would comprise of PHP, HTML, CSS &

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.