[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

[C++ Question] For loop Multiple Initialization

charlie.xia.fdu@gmail.com

11/3/2008 9:30:00 PM

Hi C++ users,

for(int i=0,int j=10 ; i<5&&j<10 ; i++ , j--) {}

example from: http://www.tech-faq.com/iterat...
Is not valid in my eclipse cdt.
Is there multiple initialization in C++? How can we use that?

Thanks!

Charlie
10 Answers

Pawel Dziepak

11/3/2008 9:39:00 PM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

charlie.xia.fdu@gmail.com wrote:
> Hi C++ users,
>
> for(int i=0,int j=10 ; i<5&&j<10 ; i++ , j--) {}
>
> example from: http://www.tech-faq.com/iterat...
> Is not valid in my eclipse cdt.
> Is there multiple initialization in C++? How can we use that?

That way is correct:

for (int i=0, j=10; i<5 && j<10; i++, j--) {}

Unless I'm wrong, there's no way to initialize variables of different
types in that part of for loop.

Pawel Dziepak

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail....

iEYEARECAAYFAkkPb5AACgkQPFW+cUiIHNo4jQCff0Shc8Rmg8IbW7OYSDPqVu5g
7dsAoLirnTWj3mIPx2eN9mnZPUNBsYi1
=4L+S
-----END PGP SIGNATURE-----

charlie.xia.fdu@gmail.com

11/3/2008 10:19:00 PM

0

On Nov 3, 1:39 pm, Pawel Dziepak <pdzie...@quarnos.org> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> charlie.xia....@gmail.com wrote:
> > Hi C++ users,
>
> > for(int i=0,int j=10 ; i<5&&j<10 ; i++ , j--) {}
>
> > example from:http://www.tech-faq.com/iterat...
> > Is not valid in my eclipse cdt.
> > Is there multiple initialization in C++? How can we use that?
>
> That way is correct:
>
> for (int i=0, j=10; i<5 && j<10; i++, j--) {}
>
> Unless I'm wrong, there's no way to initialize variables of different
> types in that part of for loop.
>
> Pawel Dziepak
it works!
thanks

>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.9 (GNU/Linux)
> Comment: Using GnuPG with Fedora -http://enigmail....
>
> iEYEARECAAYFAkkPb5AACgkQPFW+cUiIHNo4jQCff0Shc8Rmg8IbW7OYSDPqVu5g
> 7dsAoLirnTWj3mIPx2eN9mnZPUNBsYi1
> =4L+S
> -----END PGP SIGNATURE-----

James Kanze

11/3/2008 10:56:00 PM

0

On Nov 3, 10:29 pm, "charlie.xia....@gmail.com" <lxia....@gmail.com>
wrote:

> for(int i=0,int j=10 ; i<5&&j<10 ; i++ , j--) {}

> example from:http://www.tech-faq.com/iterat...
> Is not valid in my eclipse cdt.

It's not valid in C++ either. Just another case of someone who
doesn't know the language trying to write about it.

> Is there multiple initialization in C++?

Sort of. You can only write one declaration statement, but it
can define multiple variables, e.g.:

for ( int i = 0, j = 10 ; ... )

Generally speaking, i'ld avoid it, because it is confusing to
have multiple variables defined in the same declaration. But
there are probably exceptions; I know that some people like:

for ( Container::const_iterator
current = c.begin(), end = c.end() ;
current != end ;
++ current )

I'm not that fond of it, but if you raise it to the level of a
"standard idiom" in your code, I don't think I'd have any real
objections.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

James Kanze

11/3/2008 10:58:00 PM

0

Pawel Dziepak wrote:
> charlie.xia.fdu@gmail.com wrote:
> > for(int i=0,int j=10 ; i<5&&j<10 ; i++ , j--) {}

> > example from: http://www.tech-faq.com/iterat...
> > Is not valid in my eclipse cdt.
> > Is there multiple initialization in C++? How can we use that?

> That way is correct:

> for (int i=0, j=10; i<5 && j<10; i++, j--) {}

> Unless I'm wrong, there's no way to initialize variables of
> different types in that part of for loop.

for ( int i = 0 , *p = &i ; ... )

:-).

Not that I'd like to maintain code which did such things.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

blargg.h4g

11/4/2008 4:03:00 AM

0

Pawel Dziepak wrote:
> charlie.xia.fdu@gmail.com wrote:
> > for(int i=0,int j=10 ; i<5&&j<10 ; i++ , j--) {}
> >
> > example from: http://www.tech-faq.com/iterat...
> > Is not valid in my eclipse cdt.
> > Is there multiple initialization in C++? How can we use that?
>
> That way is correct:
>
> for (int i=0, j=10; i<5 && j<10; i++, j--) {}
>
> Unless I'm wrong, there's no way to initialize variables of different
> types in that part of for loop.

I'll probably regret this, but I believe you can:

double d;
int* p;
for ( char c = (d = 1.234, p = new int, 'X'); ...

Pete Becker

11/4/2008 10:21:00 AM

0

On 2008-11-03 23:03:16 -0500, blargg.h4g@gishpuppy.com (blargg) said:

> Pawel Dziepak wrote:
>> charlie.xia.fdu@gmail.com wrote:
>>> for(int i=0,int j=10 ; i<5&&j<10 ; i++ , j--) {}
>>>
>>> example from: http://www.tech-faq.com/iterat...
>>> Is not valid in my eclipse cdt.
>>> Is there multiple initialization in C++? How can we use that?
>>
>> That way is correct:
>>
>> for (int i=0, j=10; i<5 && j<10; i++, j--) {}
>>
>> Unless I'm wrong, there's no way to initialize variables of different
>> types in that part of for loop.
>
> I'll probably regret this, but I believe you can:
>
> double d;
> int* p;
> for ( char c = (d = 1.234, p = new int, 'X'); ...

Or, less cryptically,

double d;
int *p;
char c;
for (c = 'X', d = 1.234, p = new int; ...

Now, technically, that's not initialization, but it may well be what
was actually meant.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

charlie.xia.fdu@gmail.com

11/6/2008 5:16:00 PM

0

But actually I want these variables only in the scope of for loop.
Keep the initialization and increment in the for statement is the only
neat way I can think of.
Isn't it?


On Nov 4, 2:20 am, Pete Becker <p...@versatilecoding.com> wrote:
> On 2008-11-03 23:03:16 -0500, blargg....@gishpuppy.com (blargg) said:
>
>
>
> > Pawel Dziepak wrote:
> >> charlie.xia....@gmail.com wrote:
> >>> for(int i=0,int j=10 ; i<5&&j<10 ; i++ , j--) {}
>
> >>> example from:http://www.tech-faq.com/iterat...
> >>> Is not valid in my eclipse cdt.
> >>> Is there multiple initialization in C++? How can we use that?
>
> >> That way is correct:
>
> >> for (int i=0, j=10; i<5 && j<10; i++, j--) {}
>
> >> Unless I'm wrong, there's no way to initialize variables of different
> >> types in that part of for loop.
>
> > I'll probably regret this, but I believe you can:
>
> >     double d;
> >     int* p;
> >     for ( char c = (d = 1.234, p = new int, 'X'); ...
>
> Or, less cryptically,
>
> double d;
> int *p;
> char c;
> for (c = 'X', d = 1.234, p = new int; ...
>
> Now, technically, that's not initialization, but it may well be what
> was actually meant.
>
> --
>   Pete
> Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
> Standard C++ Library Extensions: a Tutorial and Reference
> (www.petebecker.com/tr1book)

Pete Becker

11/6/2008 5:44:00 PM

0

On 2008-11-06 12:16:21 -0500, "charlie.xia.fdu@gmail.com"
<lxia.usc@gmail.com> said:

> On Nov 4, 2:20 am, Pete Becker <p...@versatilecoding.com> wrote:
>> On 2008-11-03 23:03:16 -0500, blargg....@gishpuppy.com (blargg) said:
>>
>>
>>
>>> Pawel Dziepak wrote:
>>>> charlie.xia....@gmail.com wrote:
>>>>> for(int i=0,int j=10 ; i<5&&j<10 ; i++ , j--) {}
>>
>>>>> example from:http://www.tech-faq.com/iterat...
>>>>> Is not valid in my eclipse cdt.
>>>>> Is there multiple initialization in C++? How can we use that?
>>
>>>> That way is correct:
>>
>>>> for (int i=0, j=10; i<5 && j<10; i++, j--) {}
>>
>>>> Unless I'm wrong, there's no way to initialize variables of different
>>>> types in that part of for loop.
>>
>>> I'll probably regret this, but I believe you can:
>>
>>>     double d;
>>>     int* p;
>>>     for ( char c = (d = 1.234, p = new int, 'X'); ...
>>
>> Or, less cryptically,
>>
>> double d;
>> int *p;
>> char c;
>> for (c = 'X', d = 1.234, p = new int; ...
>>
>> Now, technically, that's not initialization, but it may well be what
>> was actually meant.
>>
> But actually I want these variables only in the scope of for loop.
> Keep the initialization and increment in the for statement is the only
> neat way I can think of.
> Isn't it?
>

You can't have everything. In general, though, a for loop that requires
three loop control variables is almost certainly a mistake.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

blargg.h4g

11/7/2008 9:08:00 AM

0

charlie.xia.fdu@gmail.com wrote:
> On Nov 4, 2:20 am, Pete Becker <p...@versatilecoding.com> wrote:
> > On 2008-11-03 23:03:16 -0500, blargg....@gishpuppy.com (blargg) said:
> > > Pawel Dziepak wrote:
> > >> charlie.xia....@gmail.com wrote:
> > >>> for(int i=0,int j=10 ; i<5&&j<10 ; i++ , j--) {}
> >
> > >>> example from:http://www.tech-faq.com/iterat...
> > >>> Is not valid in my eclipse cdt.
> > >>> Is there multiple initialization in C++? How can we use that?
> >
> > >> That way is correct:
> >
> > >> for (int i=0, j=10; i<5 && j<10; i++, j--) {}
> >
> > >> Unless I'm wrong, there's no way to initialize variables of different
> > >> types in that part of for loop.
> >
> > > I'll probably regret this, but I believe you can:
> >
> > > double d;
> > > int* p;
> > > for ( char c = (d = 1.234, p = new int, 'X'); ...
> >
> > Or, less cryptically,
> >
> > double d;
> > int *p;
> > char c;
> > for (c = 'X', d = 1.234, p = new int; ...
> >
> > Now, technically, that's not initialization, but it may well be what
> > was actually meant.
>
> But actually I want these variables only in the scope of for loop.
> Keep the initialization and increment in the for statement is the only
> neat way I can think of.
> Isn't it?

The for loop is for common loops that have a single variable that is
initialized, tested, and advanced. If it were made to handle every
possible loop, it would lose its usefulness. In fact, such a more general
loop construct already exists as a combination of a compound statement
({}) with a while or do-while loop in it. What do you gain by trying to
jam complicated things into a for loop anyway?

Also, please don't top-post, and don't quote signatures (see
<http://www.netmeister.org/news/learn2quot...).

charlie

11/14/2008 7:24:00 PM

0

On Nov 7, 1:08 am, blargg....@gishpuppy.com (blargg) wrote:
> charlie.xia....@gmail.com wrote:
> > On Nov 4, 2:20 am, Pete Becker <p...@versatilecoding.com> wrote:
> > > On 2008-11-03 23:03:16 -0500, blargg....@gishpuppy.com (blargg) said:
> > > > Pawel Dziepak wrote:
> > > >> charlie.xia....@gmail.com wrote:
> > > >>> for(int i=0,int j=10 ; i<5&&j<10 ; i++ , j--) {}
>
> > > >>> example from:http://www.tech-faq.com/iterat...
> > > >>> Is not valid in my eclipse cdt.
> > > >>> Is there multiple initialization in C++? How can we use that?
>
> > > >> That way is correct:
>
> > > >> for (int i=0, j=10; i<5 && j<10; i++, j--) {}
>
> > > >> Unless I'm wrong, there's no way to initialize variables of different
> > > >> types in that part of for loop.
>
> > > > I'll probably regret this, but I believe you can:
>
> > > >     double d;
> > > >     int* p;
> > > >     for ( char c = (d = 1.234, p = new int, 'X'); ...
>
> > > Or, less cryptically,
>
> > > double d;
> > > int *p;
> > > char c;
> > > for (c = 'X', d = 1.234, p = new int; ...
>
> > > Now, technically, that's not initialization, but it may well be what
> > > was actually meant.
>
> > But actually I want these variables only in the scope of for loop.
> > Keep the initialization and increment in the for statement is the only
> > neat way I can think of.
> > Isn't it?
>
> The for loop is for common loops that have a single variable that is
> initialized, tested, and advanced. If it were made to handle every
> possible loop, it would lose its usefulness. In fact, such a more general
> loop construct already exists as a combination of a compound statement
> ({}) with a while or do-while loop in it. What do you gain by trying to
> jam complicated things into a for loop anyway?
>
> Also, please don't top-post, and don't quote signatures (see
> <http://www.netmeister.org/news/learn2quot...).

Good to know. Thanks all.