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

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.

Leave a Comment