28

Why is this not valid

for( int i = 0, int x = 0; some condition; ++i, ++x )

and this is

int i, x;
for( i = 0, x = 0; some condition; ++i, ++x )

Thanks

Thomas
  • 2,939
  • 6
  • 32
  • 29
  • possible duplicate of [Can I declare variables of different types in the initialization of a for loop?](http://stackoverflow.com/questions/8644707/can-i-declare-variables-of-different-types-in-the-initialization-of-a-for-loop) – Mechanical snail Aug 17 '12 at 05:24
  • @Mechanicalsnail: The alleged duplicate is newer than this question. Why would you close this? – bitmask Aug 18 '12 at 13:33
  • @bitmask: The other question has a bit more explanation of what we're trying to do, and has more upvoted answers. But which to close is a judgement call. – Mechanical snail Aug 18 '12 at 20:41

7 Answers7

43

when you need to declare two variables of different types, it can't be done by one declaration

Hackety hack hack:

for (struct {int i; char c;} loop = {0, 'a'}; loop.i < 26; ++loop.i, ++loop.c)
{
    std::cout << loop.c << '\n';
}

;-)

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
  • 5
    Wow! Never heard of this before. – sharptooth Feb 26 '10 at 12:26
  • That seems like a nice hack. Would you comment on some strange detail? If I replace type of i variable with something like std::vector::const_iterator, it won't compile... It seems like dirty code anyway to write long temporary struct with two iterators in the for loop, but I just thirst for understanding. – Dmitri K Aug 07 '14 at 12:01
41

this works:

for( int i = 0, x = 0; some condition; ++i, ++x )

it's a variable declaration:

int i, j; // correct
int i, int j; // wrong, must not repeat type
sergiom
  • 4,791
  • 3
  • 24
  • 32
11

Why should it be valid? It is a syntactically meaningless construst. What were you trying to say with it?

The first part of for header is a declaration. The

int i = 0, int x = 0

is not a valid declaration. It will not compile in for for the same reason why it won't compile anywhere else in the program

int i = 0, int x = 0; // Syntax error

When you need to declare two objects of type int in one declaration, you do it as follows

int i = 0, x = 0; // OK

The same thing can be used in for

for( int i = 0, x = 0; some condition; ++i, ++x )  

(But when you need to declare two variables of different types, it can't be done by one declaration and, therefore, both cannot be declared in for at the same time. At least one of them will have to be declared before for.)

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
8

Correct version is

for (int i = 0, x = 0; some condition; ++i, ++x)
stas
  • 631
  • 2
  • 7
  • 9
4

This is legal:

    for(int i = 0, x = 0; condition; ++i, ++x );

int x, int y is not a legal way of declaring variables;

Arve
  • 7,284
  • 5
  • 37
  • 41
3

Because a variable declaration (like int x) is not an expression and the comma operator (,) only combines expressions.

sth
  • 222,467
  • 53
  • 283
  • 367
0

I implemented this approach to calculate the diagonal difference. Here I am calculating the sum of Antidiagonal.

 for(int i=0,j=n-1; i<n,j>=0;i++,j--){
    sum_right += a[i][j];
  }
cammando
  • 596
  • 7
  • 20