Looped Functions

Hi

Is it possible to write a loop as a function, and then is it possible to use two of such loop functions to create a double loop.

My thinking is that if such is possible then I would not have to manually create a nested loop over and over.
Last edited on
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 :)

Hope this helps.
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.

Regards
You kinda sound like you're talking about recursion...kinda.

http://www.cplusplus.com/forum/articles/2231/
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
create a nested loop containing at least 24 loops

wow! why? I would bet there's a better way!
@angelliefling
I am not sure what you want, but the number of counters over which you iterate does not have to be the same as the number of loops.

See this example:
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
29
30
31
#include <iostream>

int main()
{
    using namespace std;

    // Some number split into digits
    char digits[4] = {0};

    unsigned int 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]; } else break;
    }
}

This could be implemented with 4 loops, but it could also be avoided.

Regards
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.
Topic archived. No new replies allowed.