From -1933514192001264603
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,de99e5c46dfe5a28
X-Google-Attributes: gidf78e5,public
From: Glenn Schrader <gschrad@ll.mit.edu>
Subject: Re: Multiple declarations in for loop?
Date: 1998/05/29
Message-ID: <356B0149.83BE3151@ll.mit.edu>#1/1
X-Deja-AN: 357755251
Approved: Fergus Henderson <fjh@cs.mu.oz.au>
Content-Transfer-Encoding: 7bit
References: <356196A0.4214@lehman.com> <6jsdc4$fvr@engnews1.Eng.Sun.COM> <6jutcg$96n@marianna.psu.edu> <356A4155.2781@wizard.net>
X-Original-Date: Tue, 26 May 1998 13:52:09 -0400
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Organization: MIT Lincoln Laboratory
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUANW8bb+EDnX0m9pzZAQF4ggGAksh9UGvokGG+vGxUPVF7uuI+n8+/Z+2X RZ4DBgtWwX9Q0YBgqkELtM5WD9sX+XGt =nMc7
Newsgroups: comp.std.c++


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              ]



