0

Is there a way to use a loop to create more nested loops? E.g. Doing this

for (int i = 0; i < iterations; i++)
            {
                //Do stuff
                for (int ii = 0; ii < iterations; ii++)
                {
                    // Do stuff
                    for (int iii = 0; iii < iterations; iii++)
                    {
                        //Do Stuff
                    }
                }
            }

but allowing the user to change how many nested loops you want during run-time.

This allows me to know the depth I am on, while also resetting the "i" counter back to 0, allowing me to do the exact same thing over again, but with a different depth value.

  • 1
    why didn't you ask at Stack Overflow? meta.stackexchange.com/a/129632/165773 – gnat Jun 28 '15 at 7:06
  • @gnat Thought about that, and since this question is more a thought experiment, I thought it would be better to ask it here – Prime_Aqasix Jun 28 '15 at 7:12
  • The biggest question I have is: "Why do you want nested loops?" - You could do the same number of iterations using simple multiplication iterations and an argument. We'd need to see what stuff you need to do to be able to give a really good answer. – Magus Jun 29 '15 at 15:09
  • @Magus The main thing for this question is for me to learn a way to do nested loops, but for the small program is working on. I asked the main question on stack overflow – Prime_Aqasix Jun 30 '15 at 5:18
  • Couldn't you manipulate the main loops counter as a master loop for whatever iterations you want to work with? Same effect. – Canadian Coder Jun 30 '15 at 16:42
6

There is a way! You can solve such problems with recursion (https://en.wikipedia.org/wiki/Recursion_%28computer_science%29).

In your case it would look something like this (pseudocode):

function doStuff(int depth) {
    if(depth > USER_DEFINED_DEPTH)
        return;
    //Do stuff
    doStuff(depth + 1);
}

doStuff(0);
  • Another (uglier) way is to use self modifying code. A simple example could be written with shell scripts. Script A would write Script B, then execute Script B. (Not a recommended practice - debugging could be a nightmare). – Dan Pichelman Jun 29 '15 at 1:21
  • Suggesting JS recursion in a C# question might not be the best idea, since you risk blowing the stack. – Magus Jun 29 '15 at 15:06
1

You've gotten some good answers here and on SO to your questions, but you're missing a non-recursive solution. Here's how I went about trying to solve the problem (Note that it is currently slightly broken!):

private static IEnumerable<string> GetPaths(int maxDepth, int iterations, string root)
{
    var depths = Enumerable.Range(0, maxDepth);
    var widths = Enumerable.Range(0, iterations);

    var thing = depths.Select(x => Path.Combine(
        depths
            .Reverse()
            .Skip(x + 1)
            .Select(y => y.ToString())
        .Reverse()
        .ToArray()));

    return thing.SelectMany(x => widths.Select(y => Path.Combine(root, x, y.ToString())));
}

What this essentially does is create all the sets of combinations by joining collections, and then combines them into paths. The result is that every depth is covered, without any chance of blowing the stack. There is a bug currently: the variable thing (which should be renamed, should you take this route) contains (with an input of 10, 10, "temp" the paths:

0\1\2\3\4\5\6\7\8
0\1\2\3\4\5\6\7
0\1\2\3\4\5\6
0\1\2\3\4\5
0\1\2\3\4
0\1\2\3
0\1\2
0\1
0
string.Empty

I'll leave it to you to figure out how to fix it, but it's very simple.

  • Note that if you don't want iterations to be passed in separately, you can probably get this down to just two arguments, and use maxDepths for widths as well, or even just have the single depths collection and use that in the final SelectMany. – Magus Jun 30 '15 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.