Loops inside loops

Posted by cozzy on Stack Overflow See other posts from Stack Overflow or by cozzy
Published on 2011-01-03T19:46:13Z Indexed on 2011/01/03 19:53 UTC
Read the original article Hit count: 491

Filed under:
|

Hi, I'd like to find easier way to write loops inside loops.
Here is example code of 3 levels of 'for' loops:

int level = 0;
int value = 0;
bool next = false;

for (int i0 = 0; i0 < 6; i0++)
{
    level = 0;
    value = i0;
    method();
    if (next)
    for (int i1 = 0; i1 < 6; i1++)
        {
            level = 1;
            value = i1;
            method();
            if (next)
            for (int i2 = 0; i2 < 6; i2++)
                {
                    level = 2;
                    value = i2;
                    method();
                    if (next)
                    {
                        //Do somethnig
                    }   
                }
        }
}



private void method()
    {
        //use int 'level' and 'value'
        //determine bool 'next'
    }

I wonder if it's possible to write the same thing different way. To set number of levels(number of loops) and loop repeats. In this case levels = 3; repeats = 6;. I need it because I am using more than 20 loops inside themselves and than the code is not comprehensible.

I hope my explanation was ok and thanks for help.

© Stack Overflow or respective owner

Related posts about c#

Related posts about loops