Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

Think about this code

    if(true)
        if(false)
            System.out.println("x");
    else
        System.out.println("y");

it prints "y". Sometimes, it's too confusing to humans without braces.

Similar to what irreputable said, I've also seen this example:

if(false)
    //System.out.println("x");

System.out.println("Done");

Can you see why the second print doesn't get run?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top