Topic: Multiple declarations in for loop?


Author: Glenn Schrader <gschrad@ll.mit.edu>
Date: 1998/05/29
Raw View
James Kuyper wrote:
<snip>
> > It would be odd, but I think it would be worth it, because
> > it is frequently needed, and the alternatives are pretty
> > ugly. If the goal is to reduce oddity, maybe allowing
> > it in all other relevant contexts would be a solution :-) ?
> > Unless it would break something else, of course.
>
> I don't think that the following is very ugly, just a little clumsy:
>
> {
>         unsigned int j;
>         for(int i=0; i<10 && /* Note correction */ j<10; i++, j++)
>         {
>                 // Loop body; which presumably changes i or j, but not
>                 // always in the same way; otherwise j is redundant.
>         }
> }

How about the following? The basic idea is to use an object (in this
case defined as a struct) to aggregate the separate loop variables.


#include <iostream.h>

int
main(void)
{
  for(struct {int i; unsigned j;} loopvar = { -3, 0 };
      loopvar.j < 10;
      ++loopvar.i, ++loopvar.j)
    {
      cout << "i = " << loopvar.i
           << ", j = " << loopvar.j << endl;
    }
  return 0;
}


--
 ============================================================
 | Glenn Schrader         | Email gschrad@ll.mit.edu        |
 ============================================================
---
[ 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: Cristian Georgescu <cgeorges@lehman.COM>
Date: 1998/05/22
Raw View
> Steve Clamage <clamage@Eng.Sun.COM> wrote:
> : In article 4214@lehman.com, Cristian Georgescu <cgeorges@lehman.com> writes:
> : >> Only one simple-declaration is allowed. The following declares i and j as
> : >> you wish
> : >> for (int i=0, j=0; i<10,j<10; i++, j++) ...
> : >
> : >Is there a reason for this limitation (only one simple declaration);
> : >why not:
> : >
> : > for (int i=0, unsigned int j=0; i<10,j<10; i++, j++) ...
>
> : You can't write
> :       int i=0, unsigned int j=0;
> : in any context, so it would be odd to allow it in a for-loop header.
> : The comma in a declaration separates declarators, it doesn't end
> : a declaration.

The need to declare more than one local variable in a for loop arises
very ofter, and I was looking for a way to do it.
Of course, one can always surround the for loop in curly brackets.
--
                      Cristian Georgescu
_________________________________________________
                      email: CGeorges@lehman.com
                      tel:   (212) 526-3502

    _/      _/_/_/_/  Lehman Brothers
   _/      _/    _/   Equity Finance Systems
  _/      _/_/_/_/    World Financial Center
 _/      _/    _/     200 Vesey Street
_/_/_/  _/_/_/_/      New York, NY 10285.
_________________________________________________
---
[ 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: Cristian Georgescu <cgeorges@lehman.com>
Date: 1998/05/19
Raw View
> Only one simple-declaration is allowed. The following declares i and j as
> you wish
> for (int i=0, j=0; i<10,j<10; i++, j++)
> {
>  //...
> }

Is there a reason for this limitation (only one simple declaration);
why not:

 for (int i=0, unsigned int j=0; i<10,j<10; i++, j++)
 {
  //...
 }

--
                      Cristian Georgescu
_________________________________________________
                      email: CGeorges@lehman.com
                      tel:   (212) 526-3502

    _/      _/_/_/_/  Lehman Brothers
   _/      _/    _/   Equity Finance Systems
  _/      _/_/_/_/    World Financial Center
 _/      _/    _/     200 Vesey Street
_/_/_/  _/_/_/_/      New York, NY 10285.
_________________________________________________



[ 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: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1998/05/19
Raw View
In article 4214@lehman.com, Cristian Georgescu <cgeorges@lehman.com> writes:
>> Only one simple-declaration is allowed. The following declares i and j as
>> you wish
>> for (int i=0, j=0; i<10,j<10; i++, j++) ...
>
>Is there a reason for this limitation (only one simple declaration);
>why not:
>
> for (int i=0, unsigned int j=0; i<10,j<10; i++, j++) ...

You can't write
 int i=0, unsigned int j=0;
in any context, so it would be odd to allow it in a for-loop header.
The comma in a declaration separates declarators, it doesn't end
a declaration.

---
Steve Clamage, stephen.clamage@sun.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: "Ross Smith" <ross.s@ihug.co.nz>
Date: 1998/05/20
Raw View
Cristian Georgescu wrote in message <35608CBB.2571@lehman.com>...
>Does the language allow the definition of multiple variables in a for
>loop?
>
>F.I.: the compiler I am using (VC5.3) is accepting the following:
>
>int j=0;
>for (int i=0; i<10,j<10; i++, j++)
>{
> //...
>}

It accepts it, but I bet it isn't doing what you think it is. The comma
operator means "evaluate the LHS, discard the result, then evaluate the
RHS". Your termination clause, "i<10,j<10", is equivalent to plain
"j<10". I'm surprised the compiler didn't give a warning about an unsed
expression. (In the above loop the result is the same because both
variables are being incremented together, unless there's code inside the
loop that changes i or j.)

>but does not accept the following:
>
>for (int i=0, int j=0; i<10,j<10; i++, j++)
>{
> //...
>}
>
>Is this in the standard or it is just a compiler bug?

It's standard. You can't join declarations with the comma operator.

--
Ross Smith ................................... mailto:ross.s@ihug.co.nz
.............. The Internet Group, Auckland, New Zealand ..............
  "Remember when we told you there was no future? Well, this is it."
                                                        -- Blank Reg
---
[ 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: Oleg Zabluda <zabluda@math.psu.edu>
Date: 1998/05/21
Raw View
Steve Clamage <clamage@Eng.Sun.COM> wrote:
: In article 4214@lehman.com, Cristian Georgescu <cgeorges@lehman.com> writes:
: >> Only one simple-declaration is allowed. The following declares i and j as
: >> you wish
: >> for (int i=0, j=0; i<10,j<10; i++, j++) ...
: >
: >Is there a reason for this limitation (only one simple declaration);
: >why not:
: >
: > for (int i=0, unsigned int j=0; i<10,j<10; i++, j++) ...

: You can't write
:  int i=0, unsigned int j=0;
: in any context, so it would be odd to allow it in a for-loop header.
: The comma in a declaration separates declarators, it doesn't end
: a declaration.

It would be odd, but I think it would be worth it, because
it is frequently needed, and the alternatives are pretty
ugly. If the goal is to reduce oddity, maybe allowing
it in all other relevant contexts would be a solution :-) ?
Unless it would break something else, of course.

Oleg.
--
Life is a sexually transmitted, 100% lethal disease.
---
[ 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: 1998/05/26
Raw View
Oleg Zabluda wrote:
>
> Steve Clamage <clamage@Eng.Sun.COM> wrote:
> : In article 4214@lehman.com, Cristian Georgescu <cgeorges@lehman.com> writes:
> : >> Only one simple-declaration is allowed. The following declares i and j as
> : >> you wish
> : >> for (int i=0, j=0; i<10,j<10; i++, j++) ...
> : >
> : >Is there a reason for this limitation (only one simple declaration);
> : >why not:
> : >
> : > for (int i=0, unsigned int j=0; i<10,j<10; i++, j++) ...
>
> : You can't write
> :       int i=0, unsigned int j=0;
> : in any context, so it would be odd to allow it in a for-loop header.
> : The comma in a declaration separates declarators, it doesn't end
> : a declaration.
>
> It would be odd, but I think it would be worth it, because
> it is frequently needed, and the alternatives are pretty
> ugly. If the goal is to reduce oddity, maybe allowing
> it in all other relevant contexts would be a solution :-) ?
> Unless it would break something else, of course.

I don't think that the following is very ugly, just a little clumsy:

{
 unsigned int j;
 for(int i=0; i<10 && /* Note correction */ j<10; i++, j++)
 {
  // Loop body; which presumably changes i or j, but not
  // always in the same way; otherwise j is redundant.
 }
}


[ 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: martelli@cadlab.it (Alex Martelli)
Date: 1998/05/27
Raw View
James Kuyper <kuyper@wizard.net> writes:
 ...

>I don't think that the following is very ugly, just a little clumsy:
>{
> unsigned int j;
> for(int i=0; i<10 && /* Note correction */ j<10; i++, j++)

Also a little incorrect -- j is not initialized:-) (just init it
to zero in its definition, of course).


Alex
--
 ____    Alex Martelli, Bologna, Italia -- email: alex@magenta.com
 \SM/___                                 http://magenta.com/~alex
  \/\bi/     You never know what is enough unless you know what
     \/          is more than enough.
---
[ 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: "Liam Fitzpatrick" <liam@iname.com>
Date: 1998/05/19
Raw View
>F.I.: the compiler I am using (VC5.3) is accepting the following:
>
>int j=0;
>for (int i=0; i<10,j<10; i++, j++)
>{
> //...
>}
>
>but does not accept the following:
>
>for (int i=0, int j=0; i<10,j<10; i++, j++)
>{
> //...
>}
Only one simple-declaration is allowed. The following declares i and j as
you wish
for (int i=0, j=0; i<10,j<10; i++, j++)
{
 //...
}
---
[ 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              ]