do-while loops

Timon Gehr timon.gehr at gmx.ch
Wed Dec 28 06:37:11 PST 2011


On 12/28/2011 02:29 PM, bearophile wrote:
> One thing that I often find not handy in the design of do-while loops: the scope of their body ends before the "while":
>
>
> void main() {
>      do {
>          int x = 5;
>      } while (x != 5); // Error: undefined identifier x
> }
>
>
> So I can't define inside them variables that I test in the while().
>
> This keeps the scope clean, but it's not nice looking:
>
>
> void main() {
>      {
>          int x;
>          do {
>              x = 5;
>          } while (x != 5);
>      }
> }
>
> Bye,
> bearophile

I fully agree, but why does this go to D.learn?


More information about the Digitalmars-d-learn mailing list