Adan
Adan

Reputation: 473

C#: How to invoke multiple 'for loops' efficiently

Using C# as my language i need to create multiple for loops that utilize arrays with strings. I might even be using 10's of for loops in my code but i know there is a better way of running multiple for loops without writing 50 for loops manually, what could be the shortcut,

** what i want to accomplish:
i want to print array[] in a series of possibilities, all the possibilities of array[], for example: array[0], array[0] array[1], array[0] array[1] array[2], etc. i want a little program that prints all the combinations in an array

here's my inefficient code:

string[] array = new string[] { "and", "nand", "not", "or", "nor", "xor" };

        for (int i = 0; i < array.Length; i++) 
        {
            string test = array[i];

            Debug.WriteLine(array[i]);
        }

        for (int i = 0; i < array.Length; i++)
        {
            string test = array[i];

            Debug.WriteLine(array[i]);
        }

        for (int i = 0; i < array.Length; i++)
        {
            string test = array[i];

            Debug.WriteLine(array[i]);
        }

How do i shorten this so that i run x number of 'for loops'? in this case there are 3 for loops that need to be turned into 1 big for loop? Do for loop x number of times.

Upvotes: 0

Views: 138

Answers (5)

displayName
displayName

Reputation: 14389

I think this is what you want.

string[] array = new string[] { "and", "nand", "not", "or", "nor", "xor" };

for (var i = 0; i < array.Length; i++)
{
    for (var j = 0; j < i;  j++)
    {
        Debug.Write(array[j]);
    }
    Debug.WriteLine();
}

This will print:

and
and nand
and nand not
...

Upvotes: 0

Caverna
Caverna

Reputation: 450

Following only the information from your post, this is the code that you want

public void Strange(string[] array)
{
    Debug.Assert(array.Length > 1);
    for (var i = 1; i < array.Length; i++)
        foreach (var item in array.Take(i))
            Debug.WriteLine(item);
}

But it won't provide combinations like array[0], array[2], array[3], etc. (jumping array[1]).

Upvotes: -1

Jodrell
Jodrell

Reputation: 35716

The code in your question would be more efficiently written as

using System.Linq;

var strings = new[] { "and", "nand", "not", "or", "nor", "xor" };

foreach (var s in strings.Repeat(3).SelectMany(s => s)) 
{
    Debug.WriteLine(s);
}

if the loops don't interact and the actual order of execution is not important, and if the activity is complicated enough you could do something like this,

var loops = Enumerable.Range(1, 3).Select(i =>
   {
       foreach (var s in strings)
       {
           Debug.WriteLine(s);
       }

       Task.FromResult(true);
   });

Task.WhenAll(loops).Wait();

Upvotes: 0

Hayden
Hayden

Reputation: 2988

Use nested for loops:

int n = 3; // how many times you want to run it for
for (int j = 0; j < n; j++) {
    for (int i = 0; i < array.Length; i++) 
     {
         string test = array[i];

         Debug.WriteLine(array[i]);
     }
}

To improve the code further, you don't even need string test = array[i]; and you can use a foreach loop like Blindy's answer

Upvotes: 2

Blindy
Blindy

Reputation: 67380

You mean like:

for(int repeat=0; repeat<3; ++repeat)
    for(int i=0; i<array.Length; ++i)  
        Debug.WriteLine(array[i]);

Or even better, no clunky indexing:

for(int repeat=0; repeat<3; ++repeat)
    foreach(var line in array)
        Debug.WriteLine(line);

Upvotes: 2

Related Questions