Why c++ does not support multiple initializers in for loop?
c++, for-loop, language-lawyer, programming-languages
Solution
In C++ grammar, different data types are separated with `;` (if not function). In `for` loop, once the `;` is found the meaning is changed. i.e.
for (<initializations>; <condition>; <next operation>)
Other reason is possibly to avoid complexity in an already complex grammar, this feature is not allowed.
If you want to declare variables in `for` loop scope, then you can always simulate that situation:
int main()
{
{
double j = 3.0;
for (int i = 0; i < 10; i++, j+=0.1)
cout << i << j << endl;
}
return 0;
}
Problem
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?