473,732 Members | 2,210 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
10 58203
On Nov 7, 1:08*am, blargg....@gish puppy.com (blargg) wrote:
charlie.xia.... @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>).
Good to know. Thanks all.
Nov 14 '08 #11

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

Similar topics

5
14591
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
4671
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
1695
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
5498
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
1393
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
2381
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
1745
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
7380
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
2979
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
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8774
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
9307
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...
0
9181
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
8186
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
6735
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
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.