5

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?

Nawaz
  • 327,095
  • 105
  • 629
  • 812
  • Possible duplicate of [Is it possible to declare two variables of different types in a for loop?](https://stackoverflow.com/questions/2687392/is-it-possible-to-declare-two-variables-of-different-types-in-a-for-loop) – ks1322 Sep 07 '17 at 16:12

7 Answers7

21

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:

{
    long result = 1;
    for (int loop = 0; loop <= 10; loop++)
    {
        cout << "2^" << loop << "=" << result << endl;
        result *= 2;
    }
}

Now put this on top and you'll have written not just readable code but re-usable code:

void printPowersOfTwo(int from, int to)
Hans Passant
  • 873,011
  • 131
  • 1,552
  • 2,371
6

it is as if you tried:

int loop=0, long result = 1;

anywhere else.

you can do this:

for (struct { int loop; long result; } vars = { 0, 1};

but dont

Anycorn
  • 46,748
  • 41
  • 153
  • 250
  • 1
    @dsd but please dont actually use it unless there is a really good reason. just cause you can doesnt mean you should. – Anycorn Dec 26 '10 at 06:46
3

You can declare multiple variables in a for loop declaration part as long as they are of a single base type (i.e. you can declare an int and an int* in a single declaration, but not an int and a long).

mmx
  • 390,062
  • 84
  • 829
  • 778
3

That is not allowed by C++. That is why it's giving error.

By the way, you can do this by defining a local struct inside the function itself,

struct local {int loop; long result; };

for (local power = {0,1}; power.loop <= 10 ; power.loop++, power.result *= 2 )
{
        cout << 2 <<"^" <<power.loop << "=" << power.result << endl;
}

Working code here at ideone : http://www.ideone.com/ELT4a


Or you can simply define the struct in the for loop itself, like this,

for ( struct {int loop; long result; } power = {0,1}; power.loop <= 10 ; power.loop++, power.result *= 2 )
{
        cout << 2 <<"^" <<power.loop << "=" << power.result << endl;
}

Sample : http://www.ideone.com/X1MdC

Nawaz
  • 327,095
  • 105
  • 629
  • 812
  • 3
    Complex is not always elegant. Harder to notice a bug in it. – Öö Tiib Dec 26 '10 at 06:38
  • 4
    @ Öö Tiib : I don't think that is complex. people don't use ( or even know) this. that is **why** it appears to be complex. complex is template metaprogramming, i think. – Nawaz Dec 26 '10 at 06:41
  • @Nawaz : Yes, depends on audience reading it. Several coding standards i have seen consider even declaring multiple variables on same line as too complex. – Öö Tiib Dec 26 '10 at 07:07
  • @ Öö Tiib : agree.. that depends on audience... but I think, C++ programmers are accustomed to little bit complex coding..if they don't write this, they write something else..especially when they use STL, such as header file.. – Nawaz Dec 26 '10 at 07:15
3

Something like this would be a heck of a lot more idiomatic than trying to stuff all of your loop body into the one line. Anybody who later has to sort out your code will appreciate it if you use a fairly normal style. Calling a loop index variable "loop" is really no more expressive than calling it "i", and using a <= in a for loop instead of a < is certainly uncommon in my experience.

long result=1;
for (int i=0; i<11; i++)
{
  std::cout << "2^" << i << " = " << result << std::endl;
  result *= 2;
}
wrosecrans
  • 998
  • 1
  • 7
  • 14
2

you have to write as -

    int loop;
    long result;
    for (loop = 0, result = 1; loop <= 10; loop++, result *= 2) {
        cout << 2 <<"^" <<power.loop << "=" << power.result << endl;
    }

You can't write declaration part within loop in c++;

Khoyendra Pande
  • 1,617
  • 3
  • 25
  • 41
0

If this is initialization section you can do this.

 for (int loop=0; loop <= 10 ; loop++, result *= 2 )
    {
        static long result = 1;
        cout << "2^"<<loop<<"=" << result<<endl;
    }

As a warning if you call this twice it could be a problem.

I recently pumped this out

for (double tempTemp = 0.0,i=0;tempTemp >=targetTemp;i++)
{
  Sleep(100);
  deviceSafeCall(IL_GetFloat(myHandle,L"SensorTemperature",&tempTemp));
  cout << ".";
  static int fancy = 0;
  if (++fancy%10==0)
  {
      cout <<tempTemp <<endl;
  }
}
Mikhail
  • 7,123
  • 9
  • 56
  • 121