do-while loops

Timon Gehr timon.gehr at gmx.ch
Wed Dec 28 13:55:50 PST 2011


On 12/28/2011 10:45 PM, Manfred Nowak wrote:
> bearophile wrote:
>
>> void main() {
>>      do {
>>          int x = 5;
>>      } while (x != 5); // Error: undefined identifier x
>> }
>>
>
> Do you mean, that the similar while-loop should also be okay?
>
> | void main() {
> |     while (x != 5) /* uses `x' out of _following_ scope */ {
> |         int x = 5;
> |     };
> | }
>
> -manfred

No, it is not any more valid than the following code, even if condition 
and body are scoped together:

void main() {
     bool _ = x != 5;
     int x = 5;
}

Related:

https://github.com/D-Programming-Language/dmd/pull/342#issuecomment-3232150


More information about the Digitalmars-d-learn mailing list