Continue in while inside foreach

21,825

Solution 1

The following should do the trick

foreach (string objectName in this.ObjectNames)
{
    // Line to jump to when this.MoveToNextObject is true.
    this.ExecuteSomeCode();
    while (this.boolValue)
    {
        if (this.MoveToNextObject())
        {
            // What should go here to jump to next object.
            break;
        }
    }
    if (! this.boolValue) continue; // continue foreach

    this.ExecuteSomeOtherCode();
}

Solution 2

Use the break keyword. That will exit the while loop and continue execution outside it. Since you don't have anything after the while, it would loop around to the next item in the foreach loop.

Actually, looking at your example more closely, you actually want to be able to advance the for loop without exiting the while. You can't do this with a foreach loop, but you can break down a foreach loop to what it actually automates. In .NET, a foreach loop is actually rendered as a .GetEnumerator() call on the IEnumerable object (which your this.ObjectNames object is).

The foreach loop is basically this:

IEnumerator enumerator = this.ObjectNames.GetEnumerator();

while (enumerator.MoveNext())
{
    string objectName = (string)enumerator.Value;

    // your code inside the foreach loop would be here
}

Once you have this structure, you can call enumerator.MoveNext() within your while loop to advance to the next element. So your code would become:

IEnumerator enumerator = this.ObjectNames.GetEnumerator();

while (enumerator.MoveNext())
{
    while (this.ResumeWhileLoop())
    {
        if (this.MoveToNextObject())
        {
            // advance the loop
            if (!enumerator.MoveNext())
                // if false, there are no more items, so exit
                return;
        }

        // do your stuff
    }
}

Solution 3

The break; keyword will exit a loop:

foreach (string objectName in this.ObjectNames)
{
    // Line to jump to when this.MoveToNextObject is true.
    while (this.boolValue)
    {
        // 'continue' would jump to here.
        if (this.MoveToNextObject())
        {
            break;
        }
        this.boolValue = this.ResumeWhileLoop();
    }
}

Solution 4

Use goto.

(I guess people will be mad with this response, but I definitely think it's more readable than all other options.)

Share:
21,825
Siegfried Puchbauer
Author by

Siegfried Puchbauer

Updated on July 09, 2022

Comments

  • Siegfried Puchbauer
    Siegfried Puchbauer almost 2 years

    In the following C# code snippet
    I have a 'while' loop inside a 'foreach' loop and I wish to jump to the next item in 'foreach' when a certain condition occurs.

    foreach (string objectName in this.ObjectNames)
    {
        // Line to jump to when this.MoveToNextObject is true.
        this.ExecuteSomeCode();
        while (this.boolValue)
        {
            // 'continue' would jump to here.
            this.ExecuteSomeMoreCode();
            if (this.MoveToNextObject())
            {
                // What should go here to jump to next object.
            }
            this.ExecuteEvenMoreCode();
            this.boolValue = this.ResumeWhileLoop();
        }
        this.ExecuteSomeOtherCode();
    }
    

    'continue' would jump to the beginning of the 'while' loop not the 'foreach' loop. Is there's a keyword to use here, or should I just use goto which I don't really like.