I want to write a for loop as shown below; in the initialization section, I want to declare variables of different types:
for (int loop=0, long result = 1; loop <= 10 ; loop++, result *= 2 )
{
cout << "2^"<<loop<<"=" << result<<endl;
}
But it is giving error, means it’s not allowed. Any solution for that?
Don’t write code like this. It is speed-bump code, somebody will read this some day and go Whoa! and lose 5 minutes of his life trying to figure out why you did this. That’s 5 minutes he’ll never get back, you’ll owe him for no good reason.
If constricting the scope of result is really that important then use an extra set of braces:
Now put this on top and you’ll have written not just readable code but re-usable code: