Allow variables declared in do-while loop body to be in scope in the condition?

David Alayachew davidalayachew at gmail.com
Mon Jan 15 07:48:57 UTC 2024


Hello Amber Dev Team,

Would it make sense and would there be value in allowing variables declared
in the body of a do-while loop to still be considered in scope for the
condition of the same do-while loop?

Basically, the following example is currently not valid Java, but should it
be?

```java

do
{

   final var someVar = someMethod();

   doSomethingWith(someVar);

   //etc.

}

while (someVar.someBooleanReturningMethod());

```

I think this is clear, simple, and is better than the alternative.

```java

/* Must be mutable! :( */
/* And you probably need a default value too! */
var someVar = someYuckyDefaultOrWorseYetNull;

do
{

   //code

}

while (someVar != 42);

//And annoyingly enough, the variable is still in scope outside of the
loop! Blegh.

```

Since do-while loops are very old, I wouldn't be surprised if someone else
has asked this before. But regardless, I couldn't find anything, so I ask
now -- is there any value in doing this?

Thank you for your time and help!
David Alayachew
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20240115/ee7397c7/attachment-0001.htm>


More information about the amber-dev mailing list