29

I have the following code:

foreach(// Some condition here)
{
    while (// Some condition here)
    {
        foreach (// Some condition here)
        {
             if (// Condition again)
             {
                  //Do some code
             }
             if (// Condition again)
             {
                 //Stop the first foreach then go back to first foreach
             }
        }
    }
}

What I want to do is when I hit the 2nd if statement on the last foreach loop is to return on the first foreach loop.

Note: If the 2nd if statement is not true, it should continue the last foreach loop until the condition is not true.

Thanks in advance!

4
  • 6
    Put in a method and return Commented Jul 11, 2013 at 10:19
  • Looks to me like a recursive function.. Commented Jul 11, 2013 at 10:20
  • Will not break; help? Commented Jul 11, 2013 at 10:21
  • @rapsalands break will only exit the current loop, in this case the innermost foreach. OP wants to exit both inner loops, that is the innermost foreach and while. Commented Jul 11, 2013 at 10:26

6 Answers 6

38

The only way to this directly is with a goto.

Another (better) option is to restructure until the problem goes away. For instance by putting the inner code (while + foreach) in a method and use return to get back.

Sign up to request clarification or add additional context in comments.

9 Comments

Just as a note, my old university lecturer categorically stated he would fail anyone using goto :)
I think I should do the second option where I should put the while and foreach on a new method. Thanks for the suggestion!
@Sayse Then that lecturer was an idiot. As a simple example, the idiomatic way to do cleanup in C uses gotos. Without fail new programmers on various C mailing list try to argue against it because of bias against goto and then someone has to explain why the idiom exists.
I don't like goto's. When teaching students, it is a good approach to avoid using them. The point is to have them learn to use proper looping constructs and to think about program flow. After some years of practice, developers learn a style and know when the use of a goto is appropriate. Calling a teacher names isn't helpful either.
Thinking about it, we had a C language teacher in the early days that failed anyone using the return statement mid-function), which made even uglier code, but definitely taught us about logic. I think i can safely say he died of a heart attack when C# came with the yield statement.
|
17

Something like this:

resetLoop = false;
for(// Some condition here)
{
    while (// Some condition here)
    {
        foreach (// Some condition here)
        {
             if (// Condition again)
             {
                  //Do some code
             }
             if (// Condition again)
             {
                 //Stop the first foreach then go back to first foreach
                 resetLoop = true;
                 break;
             }
        }
        if (resetLoop) break;
    }
    if (resetLoop) {
        // Reset for loop to beginning
        // For example i = 0;
    }
}

1 Comment

I posted the same code, first in best served tho, credit to you. but for the love of all that is holy, preface your code with //Here be dragons so that newbie coders do not think this is the right way to code
12

Noone has mentioned it (Henk mentioned briefly) yet but the best approach would be to move your loops into its own method and use return

public ReturnType Loop(args)
{
foreach outerloop
    foreach innerLoop
       if(Condition)
          return;
}

Comments

6

As i see you accepted the answer in which the person refers you goto statement, where in modern programming and in expert opinion goto is a killer, we called it a killer in programming which have some certain reasons, which i will not discuss it over here at this point, but the solution of your question is very simple, you can use a Boolean flag in this kind of scenario like i will demonstrate it in my example:

foreach(// Some condition here)
{
    //solution
    bool breakme = false;

    while (// Some condition here)
    {
        foreach (// Some condition here)
        {
            if (// Condition again)
            {
                //Do some code
            }
            if (// Condition again)
            {
                //Stop the first foreach then go back to first foreach
                breakme = true;
                break;
            }
        }
    }
    if(breakme)
    {
        break;
    }
}

simple and plain. :)

2 Comments

Thanks for the answer. However, if you read my comment on that answer, I followed his second suggestion wherein just put it on a new method.
:) my pleasure... i didn't read your comment, and that is ok, i told you my suggestion, now up to you... :)
4

As said before me, I also recommend to re-factor the code and see if it is absolutely necessary to have 3 loops nested one in another. If it is, and I assume there is some logic involve, you should consider splitting into sub functions (as suggested)

For a simple code solution:

foreach(// Some condition here)
{
    var broke = false;
    while (// Some condition here)
    {
        foreach (// Some condition here)
        {
             if (// Condition again)
             {
                  //Do some code
             }
             if (// Condition again)
             {
                 //Stop the first foreach then go back to first foreach
                 broke = true;
                 break;
             }
        }
        if (broke) break;
        // continue your while loop
    }
}

Comments

3
var broken = false;
foreach(// Some condition here)
{
    broken = false;
    while (!broken && // Some condition here)
    {
        foreach (// Some condition here)
        {
             if (// Condition again)
             {
                 //Do some code
             }
             if (// Condition again)
             {
                 //Stop the first foreach then go back to first foreach
                 broken = true;
                 break;
             }
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.