Topic: do while loop - Scope of variables


Author: David R Tribble <david@tribble.com>
Date: 2000/04/21
Raw View
Paul Meyerholtz wrote:
>
> I keep doing the following and it always fails at compile time:
>
> do {
>         int x = something
>         ...
>         ... (changing x)
> } while (x <= something else);
>
> Of course the problem is easily fixed by taking the x declaration out
> of the loop, but then it has a larger scope and I only need it in the
> loop.  I know there is the disadvantage of declaring and disposing of
> x each time through the loop.
>
> But this seems similar to the for(int i = .. ; .. ; ..) loop where i
> has scope in the loop only.
>
> Should C++ be changed to allow the above do loop?

No, since you can always get the same result by enclosing the whole
do-while statement within another block:

    {
        int  x;

        do
        {
        ...change x...
        } while (x <= something_else);
    }

Or what about:

    for (int x = something;  ;  )
    {
        ...change x...

        if (x <= something_else)
            break;
    }

We shouldn't add a new syntactical construct to C++ (or any other
language) unless it really can't be done easily another way.

--
David R. Tribble, mailto:david@tribble.com, http://david.tribble.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Paul Meyerholtz <pholtz@ix.netcom.com>
Date: 2000/04/19
Raw View
I keep doing the following and it always fails at compile time:

do {
 int x = something
 ...
 ... (changing x)
} while (x <= something else);

Of course the problem is easily fixed by taking the x declaration out of
the loop, but then it has a larger scope and I only need it in the loop.
 I know there is the disadvantage of declaring and disposing of x each
time through the loop.

But this seems similar to the for(int i = .. ; .. ; ..) loop where i has
scope in the loop only.

Should C++ be changed to allow the above do loop?

Paul

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 2000/04/19
Raw View
Paul Meyerholtz wrote:
>
> I keep doing the following and it always fails at compile time:
>
> do {
>         int x = something
>         ...
>         ... (changing x)
> } while (x <= something else);
>
> Of course the problem is easily fixed by taking the x declaration out of
> the loop, but then it has a larger scope and I only need it in the loop.
>  I know there is the disadvantage of declaring and disposing of x each
> time through the loop.
>
> But this seems similar to the for(int i = .. ; .. ; ..) loop where i has
> scope in the loop only.
>
> Should C++ be changed to allow the above do loop?

I don't think it would be a good idea to let variables "escape"
braced blocks.
Note that there's a simple workaround:

{
  int x;
  do
  {
    x = something;
    ...
  } while (x <= something_else);
}

If there is really an extension done, it should be at the
correct scope, say

do (int x)
{
  x = something;
  ...
} while (x <= something_else);

But given the quite simple replacement, I'm not sure if this
is worth it (OTOH, the same trick works with for and while,
but nevertheless those got an extension for this).

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Paul Meyerholtz <pholtz@ix.netcom.com>
Date: 2000/04/19
Raw View
> do (int x)
> {
>   x = something;
>   ...
> } while (x <= something_else);
>
> But given the quite simple replacement, I'm not sure if this
> is worth it (OTOH, the same trick works with for and while,
> but nevertheless those got an extension for this).
>
There is something like this for a while loop?  Could you show an
example of it please?

Paul

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: James Kuyper <kuyper@wizard.net>
Date: 2000/04/20
Raw View
Paul Meyerholtz wrote:
>
> > do (int x)
> > {
> >   x = something;
> >   ...
> > } while (x <= something_else);
> >
> > But given the quite simple replacement, I'm not sure if this
> > is worth it (OTOH, the same trick works with for and while,
> > but nevertheless those got an extension for this).
> >
> There is something like this for a while loop?  Could you show an
> example of it please?

Examples from the C++ standard:

int i = 42;
int a[10];

for (int i = 0; i < 10; i++)
    a[i] = i;

int j = i; // j = 42

Note that this is also legal in C99. The follow examples
are legal only in C++:
====================================================
if ( int x = f()) {
 int x; // ill-formed, redeclaration of x
}
else {
 int x; // ill-formed, redeclaration of x
}

Note that the 'int x' in the if() is itself legal.
The comments indicate the scope of that declaration.
You can do the same thing in a switch() statement,
though the standard provides no examples of that
usage.
====================================================
struct Z {
    int val;
    A(int i) : val(i) { }
    ~A() { }
    operator bool() { return val != 0; }
};
int i = 1;
while (A a = i) {
    // ...
    i = 0;
}

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Greg Brewer" <nospam.greg@brewer.net>
Date: 2000/04/20
Raw View
I tend to agree with you on this.  I think it is possible to come up with a
general rule about the scope that would make the same rule apply to all
control loops.  Something along the lines that the control condition is
considered inside the scope of the loop statement.
I would propose something like
                v                Scope begins right after it is created so
that it isn't recreated
while (int i =f())
{
   int ii;
} // scope of ii ends at '}'

            v         Same as while
for (int i =f();i < n; i++)
{
   int ii;
}// scope of ii ends at '}'

switch (int i=f())
{
   case 0: i = 3;
   case 3:
}

and finally
do
{
   int ii;
} while ( );  // scope of ii ends a ')' instead of '}'

In each case the conditional clause is considered inside the scope.  In all
cases except do while(), the conditional clause exists in the same scope as
loop statement but before any variables local to that statement exist.  With
the do statement, the conditional clause exists as the last statement of the
loop scope and variables local to that scope will exists until after the
conditional clause.

Greg Brewer


"Paul Meyerholtz" <pholtz@ix.netcom.com> wrote in message
news:38FBA523.D3664A11@ix.netcom.com...
> I keep doing the following and it always fails at compile time:
>
> do {
> int x = something
> ...
> ... (changing x)
> } while (x <= something else);
>
> Of course the problem is easily fixed by taking the x declaration out of
> the loop, but then it has a larger scope and I only need it in the loop.
>  I know there is the disadvantage of declaring and disposing of x each
> time through the loop.
>
> But this seems similar to the for(int i = .. ; .. ; ..) loop where i has
> scope in the loop only.
>
> Should C++ be changed to allow the above do loop?
>
> Paul
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]
>


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]