Comment #0 by qs.il.paperinik — 2024-02-14T16:19:51Z
For loops allow a braced sequence of statements in the first section:
```d
// Valid D code:
for ({int i = 1; int n = 10;} i < n; ++i) { }
```
Variables declared there are visible in the following two sections and the loop body.
I propose to allow this for the condition as well:
Like the initial section, the condition could be a braced sequence of statements, where the last statement would be special-cased in the sense that it must evaluate to something that can be interpreted as a Boolean condition.
E.g.:
```d
// Proposed D code:
for (int i = 0; {int n = 10 - i; i < n;} ++i) { }
```
The main difference between this and an immediately invoked lambda is probably scope: The braces as part of the `for` loop construct do not create a scope; the variable `n` in the example would be visible in the condition, the increment (what `++i` is) and the body of the for loop.
It would make sense to allow the same for the condition of the `while` loop.
That allows e.g. to rewrite the standard C `fgetc` pattern in a cleaner fashion:
```d
while ({int c = std::fgetc(fp); c != EOF;}) { ... }
```
To be consistent, it should also work on a `do…while` loop, albeit not being that useful as the scope of variables declared in its condition should not be visible in the loop body.
Comment #1 by qs.il.paperinik — 2024-07-02T15:11:24Z
Can also be extended to `if` statements:
```d
if ({ auto x = f(); !x.empty; }) { }
```
Comment #2 by robert.schadek — 2024-12-13T19:33:20Z