www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - do-while loops

reply bearophile <bearophileHUGS lycos.com> writes:
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
Dec 28 2011
next sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
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?
Dec 28 2011
next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
A very small cheat:
void main()
{
    if (int x = 0)
    do {
        x = 5;
    } while (x != 5);
}

Only works for this simple case though. Put your post in d.general, I
totally agree with it as well.
Dec 28 2011
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 12/28/2011 04:01 PM, Andrej Mitrovic wrote:
 A very small cheat:
 void main()
 {
      if (int x = 0)
      do {
          x = 5;
      } while (x != 5);
 }

 Only works for this simple case though. Put your post in d.general, I
 totally agree with it as well.
This won't work. The 'if' condition is always false.
Dec 28 2011
prev sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Timon Gehr:

 I fully agree, but why does this go to D.learn?
Because I think there's no hope to see this situation changed :-) Bye, bearophile
Dec 28 2011
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 12/28/2011 06:42 PM, bearophile wrote:
 Timon Gehr:

 I fully agree, but why does this go to D.learn?
Because I think there's no hope to see this situation changed :-) Bye, bearophile
Why? The only D code that would get broken would be code that uses a global variable in the loop condition of the same name as a do loop local variable.
Dec 28 2011
parent reply =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= <xtzgzorex gmail.com> writes:
On 28-12-2011 18:50, Timon Gehr wrote:
 On 12/28/2011 06:42 PM, bearophile wrote:
 Timon Gehr:

 I fully agree, but why does this go to D.learn?
Because I think there's no hope to see this situation changed :-) Bye, bearophile
Why? The only D code that would get broken would be code that uses a global variable in the loop condition of the same name as a do loop local variable.
That's still a bit of a risk to take for such a small change, IMHO. - Alex
Dec 28 2011
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 12/28/2011 09:32 PM, Alex Rønne Petersen wrote:
 On 28-12-2011 18:50, Timon Gehr wrote:
 On 12/28/2011 06:42 PM, bearophile wrote:
 Timon Gehr:

 I fully agree, but why does this go to D.learn?
Because I think there's no hope to see this situation changed :-) Bye, bearophile
Why? The only D code that would get broken would be code that uses a global variable in the loop condition of the same name as a do loop local variable.
That's still a bit of a risk to take for such a small change, IMHO. - Alex
Well, do loops are the least frequently used looping constructs. Also, if you actually have code like the following import foo; // defines global symbol 'x' void main(){ do { int x; // ... }while(x<2); } It is likely that it is actually buggy because the programmer assumed lookup would work differently.
Dec 28 2011
prev sibling next sibling parent Xinok <xinok live.com> writes:
On 12/28/2011 8:29 AM, 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
 }
I would just rewrite it like so: void main(){ while(true){ int x = 5; if(x != 5) continue; break; } }
Dec 28 2011
prev sibling next sibling parent Denis Shelomovskij <verylonglogin.reg gmail.com> writes:
28.12.2011 16:29, bearophile ïèøåò:
 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
+1 I faced it a few days ago too. An enhancement request should be filled. Even if it will be resolved as WONTFIX, at least we will know a reason.
Dec 28 2011
prev sibling parent reply Manfred Nowak <svv1999 hotmail.com> writes:
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
Dec 28 2011
parent Timon Gehr <timon.gehr gmx.ch> writes:
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
Dec 28 2011