[C++ Question] For loop Multiple Initialization

  • Thread starter charlie.xia.fdu
  • Start date
P

Pawel Dziepak

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

Hi C++ users,

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

example from: http://www.tech-faq.com/iterations.shtml
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.mozdev.org

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

charlie.xia.fdu

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





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
 
J

James Kanze

for(int i=0,int j=10 ; i<5&&j<10 ; i++ , j--) {}
example from:http://www.tech-faq.com/iterations.shtml
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.
 
C

charlie.xia.fdu

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?
 
C

charlie

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/learn2quote.html>).

Good to know. Thanks all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top