Add item after every nth step The purpose of the method is to add a list of items after every nth step (5 for example) where the input items are repeated if needed. I'm looking for an overall review and possible how to remove the use of the while and replace it with a for.

public static List&lt;string&gt; AddEveryNthStep(List&lt;string&gt; items, List&lt;string&gt; itemsToAdd, int nthStep)
{
    List&lt;string&gt; output = new List&lt;string&gt;();

    int itemIndex = 0;
    int itemsToAddIndex = 0;
    int index = 1;

    while (itemsToAddIndex &lt; itemsToAdd.Count)
    {
        output.Add(items[itemIndex++]);

        if (itemIndex == items.Count)
        {
            itemIndex = 0;
        }

        // We are at at point where we want to add a item
        if (index % nthStep == 0)
        {
            output.Add(itemsToAdd[itemsToAddIndex++]);
        }

        index++;
    }

    return output;
}


Usage:

List&lt;string&gt; items = new List&lt;string&gt; { "Item 1", "Item 2", "Item 3", "Item 4" };
List&lt;string&gt; itemsToAdd = new List&lt;string&gt; { "Item to add 1", "Item to add 2", "Item to add 3" };

Console.WriteLine(String.Join(Environment.NewLine, AddEveryNthStep(items, itemsToAdd, 5)));


Output:
Loops and Indexes

Why are you using a while loop? A for loop keeps track of indexes for you. Admittedly you do still need to track one index yourself, but you can eliminate one to loop processing:

int addIndex = 0;
for (int i = 0; i &lt; items.Count; i++) {
    output.Add(items[i]);

    if (i % nthStep == 0) {
        output.Add(itemsToAdd[addIndex++]);
    }

    if (i == (items.Count - 1) &amp;&amp; addIndex &lt; (itemsToAdd.Count - 1)) {
        i = 0;
    }
}


In words:


define an index for the items you're adding
loop through the items list, on every pass:


add the current item to the output list
if the current step is the nth step, add the next item to add and increase the index
if we've run out of items but have more left to add, reset the items index so we loop back through



Now you're only tracking and handling one index yourself, instead of the three that your while loop requires.