I think I know what your getting at, but there is no way to tell. I mean, you won't know what you want your loop to do. However, it is possible to write a loop in a function and then call the function 'within' the function twice like such:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
void anotherLoop()
{
int x;
while(x < 5) // this is just for the example this could of course be anything
{
// Do Something
}
}
void myLoop()
{
int x;
while(x < = 20)
{
anotherLoop();
}
}
Voila! Loop within a loop using functions. However, this will get tricky whenever you want to try and add more advanced custom values, but that is what arguments are for :)
I'm not sure I follow, you can write a function that contains a loop and just pass in some parameters to control the loop, but you would still need to provide the body to execute whatever you want to do with the loop. Not sure if this answers your question or if I'm misunderstanding.
Transform for(int i = 0; i < n; ++i) action();
into f(int i) {if (i < n) { action(); f(i + 1); }}
Start with f(0). This will consume more memory (from the stack) if the compiler does not employ tail call optimization. Tail call optimization essentially is to deduce the loop version from the recursive version during compilation. Also, you may have to pass all sorts of additional parameters (such as n) if they are not globals.
For nested loops, simply substitute action() with g(0), where g is analogous to f above.
This all Sounds helpful, what i want to do is create a nested loop containing at least 24 loops. and then outside the loop i want to use it to recursively check different values of matrices
for example:
I want to check if s1 * a1*a2*a3...*a24 - a1*a2*a3*...*a24*s2=0, what the loop does is assign to the values aN 4 different matrices and thus this will check all possible combinations of the aN
#include <iostream>
int main()
{
usingnamespace std;
// Some number split into digits
char digits[4] = {0};
unsignedint pos;
while (1)
{
// Print the number contained in "digits"
for (pos = sizeof(digits); pos > 0; )
{
cout << (int)digits[--pos];
}
cout << endl;
// Increment the number contained in "digits"
for (pos = 0; pos < sizeof(digits); ++pos)
{
if (digits[pos] < 9) break; else { digits[pos] = 0; }
}
// Did the number in "digits" overflow?
if (pos < sizeof(digits)) { ++digits[pos]; } elsebreak;
}
}
Simeonz I am not sure how I would implement that for matrices.
ultifinitus it might not have to be a nested loop but I have to verify every possible combination of these aN, so actually since there are 24 matrices that all can take either of 4 different forms then the loop will run over 96 iterations. and the way I am doing it now is writing a loop in a loop in a loop etc. manually but this gets very long and has the possibility of making mistakes or missing a certain combination.