or, "Declaring multiple variables in a for loop ist verboten" ?!

My original code was

 for( int i = 1, int i2 = 1; 
      i2 < mid;
      i++, i2 = i * i ) {

I wanted to loop through the first so-many squares, and wanted both the number and its square, and the stop condition depended on the square. This code seems to be the cleanest expression of intent, but it's invalid. I can think of a dozen ways to work around this, so I'm not looking for the best alternative, but for a deeper understanding of why this is invalid. A bit of language lawyering, if you will.

I'm old enough to remember when you had to declare all your variables at the start of the function, so I appreciate the

for( int i = 0; ....

syntax. Reading around it looks like you can only have one type declaration in the first section of a for() statement. So you can do

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

or even the slightly baroque

for( int i=0, *j=&i; ...

but not the to-me-sensible

for( int i=0, double x=0.0; ...

Does anyone know why? Is this a limitation of for()? Or a restriction on comma lists, like "the first element of a comma list may declare a type, but not the other? Are the following uses of commas distinct syntactical elements of C++?


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


int i = 0, j = 0;


 int z;
 z = 1, 3, 4;

Any gurus out there?


Based on the good responses I've gotten, I think I can sharpen the question:

In a for statement

for( X; Y; Z;) {..... }

what are X, Y and Z?

My question was about C++, but I don't have a great C++ refrence. In my C reference (Harbison and Steele 4th ed, 1995), they are all three expressions, and my gcc requires C99 mode to use for( int i = 0;

In Stroustrup, sec 6.3, the for statement syntax is given as

for( for-init-statement; condition; expression ) statements

So C++ has a special syntactic statement dedicated to the first clause in for(), and we can assume they have special rules beyond those for an expression. Does this sound valid?

+1  A: 

As long as you can write a valid statement with the comma , operator, it's acceptable.

That's not entirely accurate, given the context of the question.
Michael Foukarakis
+1  A: 

C++ (also C and Java) do not permit the declaration of more than one type of variables in the scope of a for loop. In the grammar, that is because the comma does not start a new statement in that context. Effectively, only one declaration is allowed inside the for(;;) statement. The rationale is because that requirement is fairly unusual, and you can get it only with a slightly more verbose construct.

Michael Foukarakis
+11  A: 

int i = 1, double i2 = 0; is not a valid declaration statement, so it cannot be used inside the for statement. If the statement can't stand alone outside the for, then it can't be used inside the for statement.

Edit: Regarding your questions about comma operators, options 'A' and 'B' are identical and are both valid. Option 'C' is also valid, but will probably not do what you would expect. z will be assigned 1, and the statements 3 and 4 don't actually do anything (your compiler will probably warn you about "statements with no effect" and optimize them away).

Update: To address the questions in your edit, here is how the C++ spec (Sec 6.5) defines for:

for ( for-init-statement; condition(opt) ; expression(opt) ) statement

It further defines for-init-statement as either expression-statement or simple-declaration. Both condition and expression are optional.

The for-init-statement can be anything that is a valid expression-statement (such as i = 0;) or simple-declaration (such as int i = 0;). The statement int i = 1, double i2 = 0; is not a valid simple-declaration according to the spec, so it is not valid to use with for. For reference, a simple-declaration is defined (in Section 7) as:

attribute-specifier(opt) decl-specifier-seq(opt) init-declarator-list(opt) ;

where decl-specifier-seq would be the data type plus keywords like static or extern and init-declarator-list would be a comma-separated list of declarators and their optional initializers. Attempting to put more than one data type in the same simple-declaration essentially places a decl-specifier-seq where the compiler expects a init-declarator-list. Seeing this element out of place causes the compiler to treat the line as ill-formed.

The spec also notes that the for loop is equivalent to:

    while ( condition ) {
        expression ;

where condition defaults to "true" if it is omitted. Thinking about this "expanded" form may be helpful in determining whether a given syntax may be used with a for loop.

Addendum: It's not a valid comma statement BECAUSE the types in a comma statement must (per C the language definition) be the same. Same reason why the comma statement `int a=0, int b=0;` is equivalent to `int a=0, b=5;` - the types of all variables declared after the left-most are inherited from the leftmost if absent (or must be the same if present).
There is no such thing as a "comma statement". The comma operator is used to make "comma expressions" and expressions (of all types) are parts of statements. But this is not an expression, and that comma is not a comma operator.
James Curran
Great answer, thanks. Two points:1) My compiler (g++/Cygwin) doesn't accept int a = 0, int b = 0;2) I was smugly thinking "oh yeah, I *do* know what (C) does", but I thought it would do the same thing as Perl. Perl would evaluate all the comma-separated elements and assign the last one to z. Not the same!
It's clear that it cannot be accepted in comma-expressions, but why are the syntax elements of `for` expressions in the first case? Why is `for` not special-cased like parameter lists?
James, are you saying that commas can be of two different kinds, either as a comma operator as part of an expression, or as part of a ?something else, what?
-1 its not a comma statement at all -- its a declaration statement. The difference between the multiple places commas can be used in C/C++ (expressions vs declarations vs arguments) is a key source of confusion for beginners, and this answer just increases the confusion.
Chris Dodd
I think bta nailed it in his/her update: The spec says that the first slot in a for statement can be **either** an expression (e.g. i=0, j=1) **or** a __simple__ declaration (e.g. int i=0, j=1, but only one 'type' allowed.)
@Jonathan: Yes, comma can appear either in a comma-expression, or eg. in an argument list (`f(1,2,3)`), initializer list (`C::C():f(1),g(2)`), declarators (`int x[]={1,2}`) etc.
+12  A: 

It's actually a limitation of declaration statements:

int i=0, j=0, *k=&i;     // legal
int i=0, double x=0.0;   // illegel

So, basically, the answer to your final question is: (A) & (B) are the same. (C) is different.

As bta points out:

 z = 1,3,4;

is the same as

 z = 1;

However, that is because = has a higher precedence than ,. If it were written as:

 z = (1,3,4);

then that would be the same as:

 z = 4;
James Curran
Precedence! I've been away from C for a while and have lost my habit of throwing in parentheses 'to be safe'.
It is not a limitation of "declaration statements", it is a limitation of declaration. The first part of `for` is not a statement by itself, so the matter has nothing to do with declaration *statement* specifically.
+11  A: 

If you need to use several variables of different type in for-loop then you could use structures as follows:

for( struct {int i; long i2;} x = {1, 1}; x.i2 < mid; x.i++, x.i2 = x.i * x.i )
  cout << x.i2 << endl;

so this is not a limitation, just use a little different syntax.

Kirill V. Lyadvinsky
+1: I was just about to post about this... Too bad that VC++ doesn't support it...
This should work. C++ Standard allows this.
Kirill V. Lyadvinsky
Yes, it *should* work, but then VC++ isn't C++ Standard compliant. Tested successfully with Comeau.
This is cool, and nice to have in my toolkit, but I think in this case it obscures more than is useful. I'd rather maintain code that declared i and i2 outside of the for() loop. Thanks.

Well, I did some more googling, and I think the answer for C++ is "for() statements are very special places" Ick.

Excerpting from an ISO spec:

for ( for-init-statement conditionopt ; expressionopt ) statement



and they have to specify that

[Note: a for-init-statement ends with a semicolon. ] 

So the C++ syntax spec. is specifically hacked so that only one decl-spec (i.e. type) is allowed in the first slot. Looks like our attempts to argue from basic principles were doomed. Thanks for all the responses.


I can see why you hope that would work, but---given that even using the rather simple minded teaching tool that

for (i=0; i<max; i++){

is equivalent to

while (i<max){

you syntax doesn't work---I can't see why you expected that it would. EAch of the bits need to be valid.