Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C++ why can't I write a for() loop like this: for( int i = 1, double i2 = 0;

Tags:

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

(A)

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

(B)

int i = 0, j = 0; 

(C)

 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?

like image 272
Jonathan Avatar asked Jul 26 '10 17:07

Jonathan


2 Answers

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.

like image 191
Kirill V. Lyadvinsky Avatar answered Sep 22 '22 06:09

Kirill V. Lyadvinsky


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:

{     for-init-statement     while ( condition ) {         statement         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.

like image 21
bta Avatar answered Sep 18 '22 06:09

bta