How to condense multiple for loops with different starting values

In the function, I have several consecutive loops with the same code, but different initial values ​​for the control variable. The initial values ​​are obtained from the input to the function. Those.,

void thisFunction( class A a){ //some other code for (int i = ax; i != 0; --i){ code } for (int i = ay; i != 0; --i){ code } for (int i = az; i != 0; --i){ code } //some other code } 

Is there a way to condense all for loops into one loop, so when I make changes to the code inside the loop, I don’t have to change it for all three loops? An alternative is to write anotherFunction () with initial values ​​as input, but I need access to local variables in thisFunction ().

  void anotherFunction(int in){ for (int i = in; i != 0; --i){ code } } 

Is there any other way to condense loops?

Thanks.

+4
source share
5 answers

EDIT: In most cases, you should avoid this, and rather follow the MSalter answer . Just because you can, it does not mean that you should.

I'm not sure how good the idea is, but without context, a simple solution might be:

 int starts[3] = { ax, ay, az }; for ( int var = 0; var < 3; ++var ) { for ( int i = starts[var]; i != 0; --i ) { // code } } 

Note that the condition values ​​are obtained once at the beginning of the function, which means that if object a changes during the first cycle, this change will not be displayed in subsequent control loops. If you need it, the solution can be changed to store pointers.

EDIT : There is a comment suggesting the use of array size. I did not add that there is no change here, but in any case, the best way to get the size of the array:

 template<typename T, unsigned int N> inline unsigned int array_size( T (&)[N] ) { return N; } //... for ( int var = 0; var < array_size(starts); ++i ) { 
+7
source

Your guess is correct - you will have to reorganize your code into a separate function. Local variables thisFunction will become arguments for anotherFunction ; Often they will be transmitted by reference.

+9
source

Using lambda expressions, you can do this:

 void thisFunction( class A a) { //some other code auto code = [&] (int i) { // your code here }; for (int i = ax; i != 0; --i) code(i); for (int i = ay; i != 0; --i) code(i); for (int i = az; i != 0; --i) code(i); } 
+2
source

You can use macro

 #define ITERATE(_IN) \ {\ for (int _I = (_IN); _I != 0; --_I){\ code \ }\ }\ 

Then you have access to local variables, and you need to manage only one copy of the code. So your code becomes

 void thisFunction( class A a){ //some other code ITERATE(ax) ITERATE(ay) ITERATE(az) //some other code } 

This can become difficult to debug, so first make sure it works well before you make a macro of your code

+1
source

Not really. Surely loops will work for different numbers of iterations?

The best thing you can do is split as many internal components as possible, for example, into the function that you suggested. Alternatively, looping out of function.

In order not to go into the load of variables yourself, you can consider the possibility of transferring them to a class or structure.

Update . Secondly, David Rodriguez’s proposal is a very good solution!

0
source

All Articles