Simon.S
Simon.S

Reputation: 107

Nesting loop to get one of each

I'm trying to make a look to print each of every value once:

Something like this.

Lets pretend that the object letters contains "one,two ...ten"

And then there is the object numbers that contains "1,2,3,4...10"

Now if I want the loop to print:

One 
1 
Two
2 
Three
3

How would the loop look like?

I tried it like this:

     foreach (var i in letters)
{
       Console.WriteLine(i);

     foreach(var a in numbers)
        {
            Console.WriteLine(a);
        }
}

But this returns:

One 
1
2
Two
1
2
Three
1
2

And that result isn't what I want..

How can I nest the loops to make it print the way I want it?

Upvotes: 1

Views: 176

Answers (7)

Ray P.
Ray P.

Reputation: 1

What's happening is that for every time your outside loop runs, the inside one runs twice.

That's because your loops are nested, there's no getting around this. If you absolutely must use nested loops for this, you'd have to add a check whether your number has been printed yet

Something like:

foreach(var i in letters)
{
    Console.WriteLine(i);

    foreach(var a in numbers)
    {
        if (a.isPrinted) //if it has been printed already
        {
            continue; //skip ahead
        }
        else
        {
            Console.WriteLine(a.number);
            a.isPrinted = true;
            break; //stop and jump out of the foreach loop
        }
    }
}

This also means that each number is actually an object that holds the bool isPrinted and int number
I wouldn't doing it like that, it's ridiculously inefficient.
You should do what others have already suggested.

Upvotes: 0

David Watts
David Watts

Reputation: 2289

Assuming your Numbers and Letters are collections that derive from IEnumerable, you could do something like this:

 var zipped = letters.Zip(numbers, Tuple.Create);

 foreach (var tuple in zipped)
 {
     Console.WriteLine(tuple.Item1);
     Console.WriteLine(tuple.Item2);
 }

Upvotes: 2

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726599

Don't do two nested loops, they are for enumerating over all possible pairs from two collections.

Instead, make a loop on the index, and use it for both collections, or use LINQ's Zip method:

foreach (var pair in letters.Zip(numbers, (l, n) => new {Letter=l, Number=n})) {
    Console.WriteLine("{0} - {1}", pair.Letter, pair.Number);
}

Upvotes: 3

Maarten
Maarten

Reputation: 22945

Maybe you can use IEnumerable<T>.Zip(...), see here, to make combinations.

var data = letters.Zip(numbers, (l, n) => new { letter = l, number = n})
foreach (var item in data) {
    Console.Writeline(item.letter);
    Console.Writeline(item.number);
}

Upvotes: 6

ChrisF
ChrisF

Reputation: 137148

You need a single loop to iterate over both lists:

for (int index = 0; index < letters.Count; index++)
{
    Console.WriteLine(letters[index]);
    Console.WriteLine(numbers[index]);
}

This presupposes that your lists are the same length. If they're not you'd have to set the upper limit to the length of the shorter list.

for (int index = 0; index < Math.Min(letters.Count, numbers.Count); index++)

Upvotes: 1

nhouser9
nhouser9

Reputation: 6780

You're close - the second loop should not be within the first, but you should use one loop to iterate over both arrays. Try:

for (int i = 0; i < letters.size(); i++) {
 Console.WriteLine(letters.getItem(i));
 Console.WriteLine(numbers.getItem(i));
}

Note that this assumes a size() method to return the number of items and a getItem() method to return a specific item from the object.

Upvotes: 0

rashfmnb
rashfmnb

Reputation: 10538

use forloop insted of foreach use it like this

for (int i=0;i<letters.length;i++)
{
  Console.WriteLine(letters[i]);
  Console.WriteLine(numbers[i]);
}

Upvotes: 3

Related Questions