views:

1530

answers:

6

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.

+7  A: 

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
    }
}
Chris Hynes
I'm sorry the code snippet that I first added isn't very accurate. There's more code to skip. Using break won't help.I've updated the code snippet to be more accurate.
Amr
Though I like your answer as it solves the problem without adding in extra logic; I don't think that it is a good solution. I would not know how, in the OP's case, to do this, but structuring the code in such a way that this crazy logic is not needed in the first place would be the best solution.
nlaq
Too bad you can't up vote comments... +1 @Nelson too.
John Baughman
I definitely would suggest rethinking the problem to work around the logic in a for. But I also think its good to know what the high level constructs like foreach actually do -- and this method does solve the question at hand.
Chris Hynes
+2  A: 

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();
    }
}
Mitch Wheat
A: 

You can use "break;" to exit the innermost while or foreach.

Andrew Robinson
+1  A: 

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();
}
Henk Holterman
A: 

Use goto.

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

erikkallen
A: 

see here

http://java.pakcarid.com/default.aspx?sub=25&Sls=25

lois
This link doesn't lead anywhere.
Amr