473,698 Members | 2,393 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[C++ Question] For loop Multiple Initialization

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?

Thanks!

Charlie
Nov 3 '08 #1
10 58195
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

ch************* @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/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

iEYEARECAAYFAkk Pb5AACgkQPFW+cU iIHNo4jQCff0Shc 8Rmg8IbW7OYSDPq Vu5g
7dsAoLirnTWj3mI Px2eN9mnZPUNBsY i1
=4L+S
-----END PGP SIGNATURE-----
Nov 3 '08 #2
On Nov 3, 1:39*pm, Pawel Dziepak <pdzie...@quarn os.orgwrote:
-----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/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
it works!
thanks
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Fedora -http://enigmail.mozdev .org

iEYEARECAAYFAkk Pb5AACgkQPFW+cU iIHNo4jQCff0Shc 8Rmg8IbW7OYSDPq Vu5g
7dsAoLirnTWj3mI Px2eN9mnZPUNBsY i1
=4L+S
-----END PGP SIGNATURE-----
Nov 3 '08 #3
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/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::cons t_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:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 3 '08 #4
Pawel Dziepak wrote:
ch************* @gmail.com wrote:
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.
for ( int i = 0 , *p = &i ; ... )

:-).

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

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 3 '08 #5
Pawel Dziepak wrote:
ch************* @gmail.com wrote:
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.
I'll probably regret this, but I believe you can:

double d;
int* p;
for ( char c = (d = 1.234, p = new int, 'X'); ...
Nov 4 '08 #6
On 2008-11-03 23:03:16 -0500, bl********@gish puppy.com (blargg) said:
Pawel Dziepak wrote:
>ch************* @gmail.com wrote:
>>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.

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)

Nov 4 '08 #7
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...@versatile coding.comwrote :
On 2008-11-03 23:03:16 -0500, blargg....@gish puppy.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/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.
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)
Nov 6 '08 #8
On 2008-11-06 12:16:21 -0500, "ch************ *@gmail.com"
<lx******@gmail .comsaid:
On Nov 4, 2:20Â*am, Pete Becker <p...@versatile coding.comwrote :
>On 2008-11-03 23:03:16 -0500, blargg....@gish puppy.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/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.
>>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)

Nov 6 '08 #9
ch************* @gmail.com wrote:
On Nov 4, 2:20 am, Pete Becker <p...@versatile coding.comwrote :
On 2008-11-03 23:03:16 -0500, blargg....@gish puppy.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/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.
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/learn2quote.htm l>).
Nov 7 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
14590
by: alex | last post by:
Hi, it is possible to define multiple initialization methods so that the method is used that fits? I am thinking of something like this: def __init__(self, par1, par2): self.init(par1, par2);
1
4670
by: Knocked Wood | last post by:
Hi, I looked around and can't find anything on this at all and can not get it to work for IE. I'm trying to loop multiple sounds on a game, with three unique variables, when a link is clicked. Something like... <Script Language="JavaScript"> <!-- function playSound(soundName, loops, timeLength){
8
1693
by: J. Black | last post by:
Hello everyone - I've been developing an incident tracking and reporting system for an emergency services outfit that I work with part time. It's going well, except for one fly in the ointment. The situation is this - I have a dispatch form that is used by dispatchers to track incidents. An incident number is automatically assigned with an AutoNumber field once a dispatcher initiates an incident by opening the dispatch form. I have...
10
5495
by: PCHOME | last post by:
Hi! Would someone please help me thess C error(in gcc on Linux)? The compiler continues to give me: readLP.o: In function `Input_Problem': readLP.o(.text+0x0): multiple definition of `Input_Problem' main.o(.text+0x2f2): first defined here /usr/bin/ld: Warning: size of symbol `Input_Problem' changed from 172 to 185 in
2
1389
by: mike | last post by:
Hello fellow C++ experts, is there any dramatic difference between multiple inheritance: struct MyType4 : MyType1, MyType2, MyType3 { int MyInt; }; and: struct MyType4 {
2
2377
by: timlyee | last post by:
int *p = new int; auto_ptr<intap1 = p; //will fail on 3 1 auto_ptr<intap1(p); //ok 2 *ap1 = 12; // 3 the first situation has called : explicit auto_ptr(_Ty *_Ptr = 0) _THROW0() the second : auto_ptr(auto_ptr_ref<_Ty_Right) _THROW0()
14
1729
by: Bill Reid | last post by:
OK, let's say that have a function menu structure declaration like this (since I basically do): typedef struct function_item { char *descrip; void(*function)(void); } t_function_item; typedef struct function_menu { unsigned short num_items;
6
7375
by: Mr. K.V.B.L. | last post by:
I want to start a map with keys but an empty vector<string>. Not sure what the syntax is here. Something like: map<string, vector<string MapVector; MapVector.insert(make_pair("string1", new vector<string>)); MapVector.insert(make_pair("string2", new vector<string>)); MapVector.insert(make_pair("string3", new vector<string>));
3
2974
by: =?Utf-8?B?Y2hyaXNiZW4=?= | last post by:
Here are the codes Queue<string> q = new Queue<string>; Console.WriteLine(q.Count + ""); The program will crash since q is null. So my question is that what is the best way for new to initialize the array with generic queue as the value?
0
8608
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9161
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9029
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8897
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8867
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7732
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6522
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.