C Foreach Break and Continue Parent Loop

  • Remove From My Forums

 locked

C# Syntax: Breaking out of two nested foreach loops

  • Question

  • Is there a neat and tidy way to break out of two nested foreach loops in C#?

    Here is my scenario:

    // Get a list of integers

    List < int > list1 = new List < int >();

    list1.AddRange( new int [] { 1 , 4 , 8 , 9 , 14 });

    // Get another list of integers

    List < int > list2 = new List < int >();

    list2.AddRange( new int [] {2, 3, 5, 8, 11, 12});

    // Return the first match found by comparing the elements in the lists

    int firstMatch = -1;

    foreach ( int outerItem in list1)

    {

    foreach ( int innerItem in list2)

    {

    if (outerItem == innerItem)

    {

    firstMatch = innerItem;

    break ; /* I actually want to break out of BOTH loops, not

    just this inner one. */

    }

    }

    }

    // Write out the result

    Console .WriteLine(firstMatch.ToString());

    I have a solution that works fine, but I want to know if there's a neater way to do it than this....

    // Get a list of integers

    List < int > list1 = new List < int >();

    list1.AddRange( new int [] { 1 , 4 , 8 , 9 , 14 });

    // Get another list of integers

    List < int > list2 = new List < int >();

    list2.AddRange( new int [] {2, 3, 5, 8, 11, 12});

    // Return the first match found by comparing the elements in the lists

    int firstMatch = -1;

    foreach ( int outerItem in list1)

    {

    foreach ( int innerItem in list2)

    {

    if (outerItem == innerItem)

    {

    firstMatch = innerItem;

    break ; /* Just break out of the inner loop and have the outer loop perform a check on the result before it continues. */

    }

    }

    if (firstMatch != -1)

    {

    break ; /* Check whether or not the first loop got a result, by comparing the firstMatch variable to its default value. In more complicated scenarios, I could use a boolean variable to flag whether or not a match had been found yet */

    }

    }

    // Write out the result

    Console .WriteLine(firstMatch.ToString());

    If anybody knows of a snippet or statement in C# that I could use to make this neater, please could you let me know?

    Many thanks in advance.

Answers

  • Predicate delegates to the rescue!

    Code Snippet

    foreach ( int outerItem in list1) {

        firstMatch = list2.Find( delegate ( int i) {

            return i == ( int )outerItem;

        });

        if (firstMatch != 0) {

            break ;

        }

    }

  • There is no way of using break to break out of multiple loops. The break command will allways just apply to the inner most loop.
    I would probably do the same thing that your doing, but you could also use "goto".

    Code Snippet

    // Get a list of integers

    List < int > list1 = new List < int >();

    list1.AddRange( new int [] { 1 , 4 , 8 , 9 , 14 });

    // Get another list of integers

    List < int > list2 = new List < int >();

    list2.AddRange( new int [] {2, 3, 5, 8, 11, 12});

    // Return the first match found by comparing the elements in the lists

    int firstMatch = -1;

    foreach ( int outerItem in list1)

    {

    foreach ( int innerItem in list2)

    {

    if (outerItem == innerItem)

    {

    goto BreakLoops;

    }

    }

    }

    BreakLoops:

    //Code continuse here

  • Your welcome

    But if your using C# 3.0 you could also use linq and just skip the loops

    something like this

    // Get a list of integers

    List < int > list1 = new List < int >();

    list1.AddRange( new int [] { 1, 4, 9, 14 });

    // Get another list of integers

    List < int > list2 = new List < int >();

    list2.AddRange( new int [] { 2, 3, 5, 8, 11, 12 });

    int firstMatch = list1.FirstOrDefault(a => a == list2.Where(b => b == a).Select(c => c).FirstOrDefault() );

    // Write out the result

    Console .WriteLine(firstMatch.ToString());

  • You could put just the search logic into a function:

    public int FirstMatch(IEnumerable<int> set1, IEnumerable<int> set2)
    {
        foreach (int n1 in set1)
        {
            foreach (int n2 in set2)
            {
                if (n1 == n2)
                {
                    return n1;
                }
            }
        }

        return -1; // No matches.
    }

    This isn't really structured, since it has a return in the middle of a nested loop; but for simple short things like this it is probably acceptable, and it is easy to understand the code.

williamsloggeme.blogspot.com

Source: https://social.msdn.microsoft.com/Forums/vstudio/en-US/0acf5ec6-cf68-4a6c-a83e-dcf3694b6dc3/c-syntax-breaking-out-of-two-nested-foreach-loops?forum=csharpgeneral

0 Response to "C Foreach Break and Continue Parent Loop"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel