how I can get the variable from a for loop for each iteration

Hello ,
I am really new in C++ development.

I need to get the variable of each iteration in a for loop .
How can i do this
thank you
Hello dzeko1236,

Your question is a bit vague. What variable are you meaning? The for loop iterator or a variable outside of the for loop?

What have you tried?

Do you have any code that you can post?

Please post enough code that can be compiled and tested. It is easier than having to guess at what you left out.

Andy
It's usually done like this:
1
2
3
for (i=start; i< end; ++i) {
    // i is the iteration variable
}


Note that for is actually much more general and can express many other types of loops, such as walking through a list:
1
2
3
for (node *p = head; p; p = n->next) {
   // p points to a node
}


Always consider using a range-based for loop if possible.
Last edited on
thanks for your reply.

for example outside the for loop i have 3 int variable var1, var2, var3.

the for loop has as output for the first iteration the variable is 10 for the 2nd iteration 20 and for the 3rd iteration is 40.

so the operation I want is:

iteration1: assign 10 to var1 and assign nothing to the other.

iteration 2: assign 20 to var2 and assign nothing to the other.

iteration 3: assign 40 to var3 and assign nothing to the other.
for ( int i=0;i<10;i++){

// here I manipulate a Can bus.
So I would like to recuperate 9 variable , but for each iteration i get only one variable .
I want to assign this variable to another out of the for loop .
}
Is my question clear for you ?
so the operation I want is:

iteration1: assign 10 to var1 and assign nothing to the other.

iteration 2: assign 20 to var2 and assign nothing to the other.

iteration 3: assign 40 to var3 and assign nothing to the other.


The short answer is that you can't directly. The slightly longer answer is you can but not as you describe it. Possibly something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main() {

	int data[3] {};

	int& var1 {data[0]};
	int& var2 {data[1]};
	int& var3 {data[2]};

	for (int i = 0; i < 3; ++i)
		data[i] = i;

	std::cout << var1 << ' ' << var2 << ' ' << var3 << '\n';
}



0 1 2

Last edited on
Here we have three integer variables:
1
2
3
int var1;
int var2;
int var3;


Here we have three integer variables too:
int data[3];
They have been aggregated as array.
You can still address the individual integers:
1
2
3
data[0]
data[1]
data[2]


Now the loop:
1
2
3
for ( int i = 0; i < 3; ++i ) {
  data[i] = (i+1) * 10;
}

* On first iteration the i==0, and therefore a value is written only to data[0]. The value is (i+1)*10, i.e. (0+1)*10. 10
* On second iteration the i==1, and therefore a value is written only to data[1]. The value is (i+1)*10, i.e. (1+1)*10. 20


The int& var1 {data[0]}; creates a reference. Reference is an alias, an another name for existing variable. Here the variable is data[0] and the name of reference is var1.
The short answer is that you can't.
Actually, it's very easy:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using std::cout;

int main()
{
    int var1, var2, var3;
    int *ptrs[3] {&var1, &var2, &var3};

    for (int i=0; i<3; ++i) {
	*ptrs[i] = 10*(i+1);
    }
    cout << var1 << ' ' << var2 << ' ' << var3 << '\n';
}


You're probably better off using an array though. Anytime you find yourself naming variables with suffixes 1,2,3, etc. it's almost certain.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

int main()
{

    int var1{0};
    int var2{0};
    int var3{0};
    
    
    for( int i = 0; i < 30; i++)
    {
        // 1st 10 iterations
        if(i < 10)
            var1++;
        
        // 2nd 10 iterations
        if( (i < 20) and (i >= 10) )
            var2++;
        
        // 3rd 10 iterations
        if( i >= 20)
            var3++;
    }
    std::cout << var1 << ' ' << var2 << ' ' << var3 << '\n';
    
    return 0;
}


10 10 10
Program ended with exit code: 0
Topic archived. No new replies allowed.