Possible Duplicate:
In C++ why can't I write a for() loop like this: for( int i = 1, double i2 = 0;
Why is it so 'hard' to write a for-loop in C++ with 2 loop variables?
#include <iostream>
using namespace std;
int main()
{
for (int i = 0, double j = 3.0; i < 10; i++, j+=0.1)
cout << i << j << endl;
return 0;
}
does not compile, becuase there are two declaration in the for-loop initializer block.
But Why?
In C++ grammar, different data types are separated with
;(if not function). Inforloop, once the;is found the meaning is changed. i.e.Other reason is possibly to avoid complexity in an already complex grammar, this feature is not allowed.
If you want to declare variables in
forloop scope, then you can always simulate that situation: