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?

1
  • which compiler gives you sintax error? seems compiler developers should be better at spelling. Commented Feb 27, 2015 at 10:54

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Your code has a syntax error, do you know where?
There was a ; at the end of the for loop
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++){

Comments

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 :)

Comments

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.

Comments

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

Comments

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;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.