2

I can initialize two int variables in the for initialization like this:

#include <iostream>
using namespace std;

int main() {
    for(int i = 0, j=i+1; i<4; i++, j++){
        cout << "i: " << i << ", j: " << j << endl;
    }
    return 0;
}

and it prints:

i: 0, j: 1
i: 1, j: 2
i: 2, j: 3
i: 3, j: 4

How I could initialize two variable of different types, for example int and float, like this?

#include <iostream>
using namespace std;

int main() {
    for(int i = 0, float j=i+1; i<4; i++, j++){
        cout << "i: " << i << ", j: " << j << endl;
    }
    return 0;
}

This last code returns me a syntax error, is there a way to accomplish that?

madx
  • 6,723
  • 4
  • 55
  • 59
  • which compiler gives you *sintax error*? seems compiler developers should be better at spelling. – thang Feb 27 '15 at 10:54

6 Answers6

3

It is not possible to declare and initialize variables of multiple types in a for loop.

But you can assign and use multiple type variables like in the example below:

#include <iostream>
using namespace std;

int main() {
    int i;
    float j;
    for (i=0, j=i+1 ; i < 4 ; i++, j++) {
        cout << "i: " << i << ", j: " << j << endl;
    }
    return 0;
}

}

So this would act as you want your loop to behave.

Codebrewer
  • 130
  • 9
1

You can't define two different types in same for loop. Define one of them outside the loop like:

float j = 1;
for(int i = 0; i<4; i++, j++){

OR define type outside the loop and initialize values within the loop like:

float j;
int i;
for(i = 0, j=1; i<4; i++, j++){
Abhineet
  • 5,320
  • 1
  • 25
  • 43
SMA
  • 36,381
  • 8
  • 49
  • 73
1

Why not just try

#include <iostream>
using namespace std;

int main() {
    float j = 1;
    for (int i = 0 ; i < 4 ; i++, j++){
        cout << "i: " << i << ", j: " << j << endl;
    }
    return 0;
}

Just define the float outside the for loop. Or, as suggested by others

#include <iostream>
using namespace std;

int main() {
    float j;
    int i
    for (i = 0, j = 1 ; i < 4 ; i++, j++){
        cout << "i: " << i << ", j: " << j << endl;
    }
    return 0;
}

Hope this is what you wanted :)

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
Arun A S
  • 6,421
  • 4
  • 29
  • 43
1

What you are trying to do is same like this,

int main()
{
 int i, float j ; // which is obviously Syntax error
}

Now, coming to your question,

Q::Why I couldln't initialize two variable of different types ?

A::Because, its' a Syntax error.

Q::is there a way to accomplish that?

A::Absolutely not.

Community
  • 1
  • 1
Abhineet
  • 5,320
  • 1
  • 25
  • 43
0

You can't define two different types in same for loop, you may move the declaration of the type outside of the loop, or use std::pair (or std::tuple) to aggregate your type into one:

//#include <utility>

for (std::pair<int, float> p = {0, 1.f}; p.first < 4; p.first++, p.second++){
    std::cout << "i: " << p.first << ", j: " << p.second << std::endl;
}

Live demo

Jarod42
  • 203,559
  • 14
  • 181
  • 302
0

is there a way to accomplish that?

Using a struct:

for (struct {int i; float j;} x = {0, 1}; x.i < 4; x.i++, x.j++) {
    cout << "x.i: " << x.i << ", x.j: " << x.j << endl;
}
David Ranieri
  • 39,972
  • 7
  • 52
  • 94